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
372 changes: 370 additions & 2 deletions README.md

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions src/patterns/01-abstractFactory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,36 @@ export class ErrorNotification {}

export class SuccessControl {
create (type = '') {
// todo: implement logic
if (type === 'button') {
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 SuccessButton();
}

if (type === 'notification') {
return new SuccessNotification();
}
}
}

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

if (type === 'notification') {
return new ErrorNotification();
}
}
}

export default class ControlsFactory {
getFactory (factoryType = '') {
// todo: implement logic
if (factoryType === 'success') {
return new SuccessControl();
}

if (factoryType === 'error') {
return new ErrorControl();
}
}
}
16 changes: 13 additions & 3 deletions src/patterns/02-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ export default class RequestBuilder {
}

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

return this;
}

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

return this;
}

addFilter (filter, filterLte, filterGte) {
// todo: implement logic
this.url.searchParams.append('filter', filter);
this.url.searchParams.append('filter_lte', filterLte);
this.url.searchParams.append('filter_gte', filterGte);

return this;
}
}
20 changes: 16 additions & 4 deletions src/patterns/03-factoryMethod/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@ class Input {
}

export class TextInput extends Input {
// todo: implement logic
static create() {
return new TextInput();
}
}

export class NumberInput extends Input {
// todo: implement logic
static create() {
return new NumberInput();
}
}

export class EmailInput extends Input {
// todo: implement logic
static create() {
return new EmailInput();
}
}

export const inputFactory = (type = '') => {
// todo: implement logic
const inputs = {
text: TextInput,
number: NumberInput,
email: EmailInput,
}

return inputs[type].create();
};
24 changes: 20 additions & 4 deletions src/patterns/04-prototype/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
class Monster {
// todo: implement logic
constructor(settings) {
this.settings = settings;
}
}

export default class Location {
monsters = [];

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

get monstersSettings() {
return this._settings;
}

set monstersSettings(value) {
this._settings = value;
this.initMonsters();
}

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

clone () {
// todo: implement logic
return new Location(this.name, this.monstersCount, { ...this._settings });
}
}
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;
}
}
3 changes: 2 additions & 1 deletion src/patterns/06-adapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class Rectangle {

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

5 changes: 3 additions & 2 deletions src/patterns/07-bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export class GreenColorsPallet {
}

export class Notification {
constructor () {
// todo: add implementation
constructor (name, pallet) {
this.name = name;
this.colorsPallet = pallet;
}

toString () {
Expand Down
4 changes: 2 additions & 2 deletions src/patterns/08-composite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class Child {
}

getSum () {
// todo: add implementation
return this.value;
}
}

Expand All @@ -14,7 +14,7 @@ export class Parent {
}

getSum () {
// todo: add implementation
return this.values.reduce((acc, value) => acc + value.getSum(), 0);
}
}

17 changes: 15 additions & 2 deletions src/patterns/09-decorator/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
export class Milk {
price = 2;

// todo: add implementation
constructor (extra) {
this.extra = extra;
}

getPrice () {
return this.extra ? this.extra.getPrice() + this.price : this.price;
}
}

export class Sugar {
price = 1;

// todo: add implementation
// Смущает что получилось два одинаковых решения для Milk и для Sugar
Copy link
Owner

Choose a reason for hiding this comment

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

в этом задании это ок

constructor (extra) {
this.extra = extra;
}

getPrice () {
return this.extra ? this.extra.getPrice() + this.price : this.price;
}
}

export class Coffee {
Expand Down
10 changes: 9 additions & 1 deletion src/patterns/10-facade/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export class Game {
constructor() {
this.hero = new Hero();
this.location = new Location();
}
start () {
// todo: add implementation
this.hero.name = 'Barbarian';
Copy link
Owner

Choose a reason for hiding this comment

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

лучше через конструктов, тк там есть аткая возможность

this.location.name = 'darkForest';
this.location.addMonster(new Monster('demon'));
this.location.addMonster(new Monster('demon'));
this.location.addMonster(new Monster('undead'));
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/patterns/11-flyweight/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Flyweight {
// todo: add implementation
constructor(name, country, color) {
this.name = name;
this.country = country;
this.color = color;
}
}

export class ProductsStore {
Expand All @@ -9,10 +13,18 @@ export class ProductsStore {
addProduct (productData = {}) {
const productFlyweight = this.getOrCreateFlyweight(productData.info);

// todo: add implementation
this.products.push({
...productData,
info: productFlyweight,
});
}

getOrCreateFlyweight (info = {}) {
// todo: add implementation
const flyweightKey = `${info.name}${info.country}${info.color}`;
if (!this.flyweights[flyweightKey]) {
this.flyweights[flyweightKey] = info;
}

return this.flyweights[flyweightKey];
}
}
11 changes: 8 additions & 3 deletions src/patterns/12-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ export class User {
}

export class ProxyUser {
// Не понимаю зачем здесь это свойство
rights = [];
Copy link
Owner

Choose a reason for hiding this comment

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

это моя опечатка


constructor() {
// todo: add implementation
constructor(user) {
this.user = user;
}

write () {
// todo: add implementation
if (this.user.rights.includes('admin')) {
return this.user.write();
}

return "user does not have permissions to write";
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/patterns/13-chainOfResponsibility/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
class Handler {
name = '';
regExp = /./;
nextHandler;

setNext (handler) {
// todo: implement
this.nextHandler = handler;
return this.nextHandler;
}

next (data) {
// todo: implement
if (this.nextHandler) {
return this.nextHandler?.validate(data);
}
}

validate (data) {
// todo: implement
if (this.regExp.test(data)) {
/*
Непонятно для чего нужно свойство next, если можно записать короче:
Copy link
Owner

Choose a reason for hiding this comment

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

для единообразия работы с элементами цепочки

return this.nextHandler?.validate(data);
*/
return this.next(data);
}

return `Validation rule \"${this.name}\" didn\'t pass for string \"${data}\"`;
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/patterns/14-command/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
export class NextPage {
constructor(pagination) {
this.pagination = pagination;
}

execute () {
// todo: add implementation
this.pagination.nextPage();
}
}

export class PrevPage {
constructor(pagination) {
this.pagination = pagination;
}

execute () {
// todo: add implementation
this.pagination.prevPage();
}
}

Expand All @@ -24,10 +32,10 @@ export class Pagination {

export class Button {
constructor (command) {
// todo: add implementation
this.onClick = command;
}

click () {
// todo: add implementation
this.onClick.execute();
}
}
11 changes: 9 additions & 2 deletions src/patterns/15-iterator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ export class Range {
}

forEach (callback) {
// todo: add implementation
for(let i = this.start; i <= this.end; i++) {
callback(i);
}
}

getRange () {
// todo: add implementation
const array = [];
this.forEach((number) => {
array.push(number);
})

return array;
}
}

Loading