Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions src/patterns/01-abstractFactory/example.js

This file was deleted.

42 changes: 35 additions & 7 deletions src/patterns/01-abstractFactory/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
export class SuccessButton {}
export class ErrorButton {}
export class SuccessNotification {}
export class ErrorNotification {}
export class SuccessButton {
create() {
return '<div class="button button__success">Button</div>'
}
}

export class ErrorButton {
create() {
return '<div class="button button__error">Button</div>'
}
}

export class SuccessNotification {
create() {
return '<div class="notify notify__success">Notification</div>'
}
}

export class ErrorNotification {
create() {
return '<div class="notify notify__error">Notification</div>'
}
}

export class SuccessControl {
create (type = '') {
// todo: implement logic
if (type === 'notification')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше через switch, case так расширять проще

return new SuccessNotification()
else
return new SuccessButton()
}
}

export class ErrorControl {
create (type = '') {
// todo: implement logic
if (type === 'notification')
return new ErrorNotification()
else
return new ErrorButton()
}
}

export default class ControlsFactory {
getFactory (factoryType = '') {
// todo: implement logic
if (factoryType === 'error')
return new ErrorControl()
else
return new SuccessControl()
}
}
29 changes: 0 additions & 29 deletions src/patterns/02-builder/example.js

This file was deleted.

12 changes: 8 additions & 4 deletions src/patterns/02-builder/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
export default class RequestBuilder {
constructor(url = '') {
this.url = new URL(url);
this.url = new URL(url)
}

addPagination (start, end) {
// todo: implement logic
this.url.searchParams.set('start', start)
this.url.searchParams.set('end', end)
}

addSort (sort, order) {
// todo: implement logic
this.url.searchParams.set('sort', sort)
this.url.searchParams.set('order', order)
}

addFilter (filter, filterLte, filterGte) {
// todo: implement logic
this.url.searchParams.set('filter', filter)
this.url.searchParams.set('filter_lte', filterLte)
this.url.searchParams.set('filter_gte', filterGte)
}
}
37 changes: 0 additions & 37 deletions src/patterns/03-factoryMethod/example.js

This file was deleted.

24 changes: 19 additions & 5 deletions src/patterns/03-factoryMethod/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@ class Input {
element = {};

create () {
// Abstract method
return this.element
}
}

export class TextInput extends Input {
// todo: implement logic
create() {
this.element.type = 'text'
super.create()
}
}

export class NumberInput extends Input {
// todo: implement logic
create() {
this.element.type = 'number'
super.create()
}
}

export class EmailInput extends Input {
// todo: implement logic
create() {
this.element.type = 'email'
super.create()
}
}

export const inputFactory = (type = '') => {
// todo: implement logic
if (type === 'number')
return new NumberInput()
else if (type === 'email')
return new EmailInput()

return new TextInput()
};
20 changes: 0 additions & 20 deletions src/patterns/04-prototype/examples.js

This file was deleted.

19 changes: 13 additions & 6 deletions src/patterns/04-prototype/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
class Monster {
// todo: implement logic
constructor(settings = {}) {
this.settings = settings
}
}

export default class Location {
monsters = [];
monsters = []

constructor(name = '', monstersCount = 0, monstersSettings = {}) {
this.name = name;
// todo: implement logic
this.name = name
this.monstersCount = monstersCount
this.monstersSettings = monstersSettings

this.initMonsters()
}

initMonsters () {
// todo: implement logic
for (let i = 0; i < this.monstersCount; i++) {
this.monsters.push(new Monster(this.monstersSettings));
}
}

clone () {
// todo: implement logic
return { ...new Location() }
}
}
19 changes: 0 additions & 19 deletions src/patterns/05-singleton/example.js

This file was deleted.

8 changes: 7 additions & 1 deletion src/patterns/05-singleton/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export default class Singleton {
static #instance;

constructor() {
// todo: implement logic
if (Singleton.#instance) {
return Singleton.#instance;
}

Singleton.#instance = this;
}
}
2 changes: 1 addition & 1 deletion src/patterns/05-singleton/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Singleton from './index.js';

describe('patterns/singleton', () => {
it('should return the same instance', () => {
expect(new Singleton() === new Singleton()). toBeTruthy();
expect(new Singleton() === new Singleton()).toBeTruthy();
});
});
11 changes: 6 additions & 5 deletions src/patterns/06-adapter/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
export const getArea = shape => {
return shape.width * shape.height;
return shape.width * shape.height
};

export class Square {
constructor(size) {
this.size = size;
this.size = size
}
}

export class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
this.width = width
this.height = height
}
}

export class Adapter {
constructor(shape) {
// todo: add implementation
this.width = shape.width ?? shape.size
this.height = shape.height ?? shape.size
}
}

Loading