Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
020dada
init
Arthur-Ming Aug 1, 2023
e8680c3
add solution for abstractFactory
Arthur-Ming Aug 1, 2023
0d5fa97
add solution for factoryMethod
Arthur-Ming Aug 2, 2023
e63d8f2
add solution for builder
Arthur-Ming Aug 2, 2023
aef258e
prettier
Arthur-Ming Aug 4, 2023
031cf59
add solution for prototype
Arthur-Ming Aug 4, 2023
7c5f693
add solution for singleton
Arthur-Ming Aug 5, 2023
4b0538f
add solution for adapter
Arthur-Ming Aug 5, 2023
3d44cec
add solution for bridge
Arthur-Ming Aug 5, 2023
a163c34
add solution for composite
Arthur-Ming Aug 5, 2023
6789bfd
add solution for decorator
Arthur-Ming Aug 5, 2023
cfebbb0
add solution for facade
Arthur-Ming Aug 5, 2023
97c6a35
add solution for flyweight
Arthur-Ming Aug 5, 2023
f858f73
add solution for proxy
Arthur-Ming Aug 5, 2023
8d27deb
add solution for chainOfResponsibility
Arthur-Ming Aug 6, 2023
6b885a5
add solution for command
Arthur-Ming Aug 7, 2023
a10db47
add solution for iterator
Arthur-Ming Aug 7, 2023
b7316a2
add solution for mediator
Arthur-Ming Aug 12, 2023
2f0665e
add alt solution for mediator
Arthur-Ming Aug 12, 2023
1bd213d
add solution for memento
Arthur-Ming Aug 13, 2023
e2edea9
Merge branch 'SArchieEdu:main' into main
Arthur-Ming Aug 13, 2023
552ef5c
add solution for observer
Arthur-Ming Aug 13, 2023
2a4666e
Merge branch 'main' of https://github.com/Arthur-Ming/js-patterns
Arthur-Ming Aug 13, 2023
724179d
add solution for state
Arthur-Ming Aug 19, 2023
0cc60f1
add solution for strategy
Arthur-Ming Aug 19, 2023
43f930c
add solution for templateMethod
Arthur-Ming Aug 20, 2023
c41ea4a
add solution for visitor
Arthur-Ming Aug 20, 2023
b2aad9c
add solution for abstractFactory
Arthur-Ming Aug 20, 2023
a954121
refactor factoryMethod
Arthur-Ming Aug 20, 2023
55d4af8
refactor composite
Arthur-Ming Aug 20, 2023
2e793a6
refactor facade
Arthur-Ming Aug 20, 2023
429b85a
refactor chainOfResponsibility
Arthur-Ming Aug 20, 2023
ef17cd6
refactor command
Arthur-Ming Aug 20, 2023
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
23,333 changes: 23,333 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/patterns/01-abstractFactory/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ createInput
createTable
*/

interface ComponentFactory {
/* interface ComponentFactory {
createButton(): Button;
createInput(): Input;
createTable(): Table;
Expand Down Expand Up @@ -63,4 +63,4 @@ function Component() {
return <div>
{currentFabric.createButton()}
</div>
}
} */
37 changes: 29 additions & 8 deletions src/patterns/01-abstractFactory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,41 @@ export class ErrorButton {}
export class SuccessNotification {}
export class ErrorNotification {}

export class SuccessControl {
create (type = '') {
// todo: implement logic
class Control {
types = {};

create(type = "") {
const ControlType = this.types[type];
if (!ControlType) {
throw new Error("Control type is not defined");
}
return new ControlType();
}
}

export class SuccessControl extends Control {
constructor() {
super();
this.types.button = SuccessButton;
this.types.notification = SuccessNotification;
}
}

export class ErrorControl {
create (type = '') {
// todo: implement logic
export class ErrorControl extends Control {
constructor() {
super();
this.types.button = ErrorButton;
this.types.notification = ErrorNotification;
}
}

export default class ControlsFactory {
getFactory (factoryType = '') {
// todo: implement logic
getFactory(factoryType = "") {
if (factoryType === "success") {
return new SuccessControl();
}
if (factoryType === "error") {
return new ErrorControl();
}
}
}
3 changes: 2 additions & 1 deletion src/patterns/02-builder/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const playerConfig = {
/* const playerConfig = {
config: {},

addAdConfig() {
Expand Down Expand Up @@ -27,3 +27,4 @@ if (isDrmNeeded) {
}


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

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

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

addFilter (filter, filterLte, filterGte) {
// todo: implement logic
addFilter(filter, filterLte, filterGte) {
this.url.searchParams.set("filter", filter);
this.url.searchParams.set("filter_lte", filterLte);
this.url.searchParams.set("filter_gte", filterGte);
return this.url;
}
}
4 changes: 2 additions & 2 deletions src/patterns/03-factoryMethod/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

/*
class FabricNode {
draw() {
return <FabricNode/>
Expand Down Expand Up @@ -34,4 +34,4 @@ function paint(node) {


}

*/
30 changes: 23 additions & 7 deletions src/patterns/03-factoryMethod/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
class Input {
element = {};

create () {
// Abstract method
create() {
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
export const inputFactory = (type = "") => {
const inputs = {
text: TextInput,
number: NumberInput,
email: EmailInput,
};

const Input = inputs[type];
return new Input();
};
4 changes: 2 additions & 2 deletions src/patterns/04-prototype/examples.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const storageNode = {
/* const storageNode = {
id: 'asdasdasdas',
level: 100,
quality: {
Expand All @@ -17,4 +17,4 @@ const storageNode = {
id: nanoid(),
}
}
}
} */
23 changes: 16 additions & 7 deletions src/patterns/04-prototype/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
class Monster {
// todo: implement logic
settings = {};
constructor(settings) {
this.settings = settings;
}
}

export default class Location {
monsters = [];

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

initMonsters () {
// todo: implement logic
initMonsters() {
this.monsters = Array.from(Array(this.monstersCount), () => {
return new Monster(this.monstersSettings);
});
}

clone () {
// todo: implement logic
clone() {
return new Location(this.name, this.monstersCount, {
...this.monstersSettings,
});
}
}
20 changes: 13 additions & 7 deletions src/patterns/04-prototype/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import Location from './index.js';
import Location from "./index.js";

describe('patterns/prototype', () => {
it('should be able to initialize monsters in the location', () => {
describe("patterns/prototype", () => {
it("should be able to initialize monsters in the location", () => {
const location = new Location("dryHills", 3, { type: "undead" });

expect(location.monsters.length).toBe(3);
expect(location.monsters.every(monster => monster.settings.type === 'undead')). toBeTruthy();
expect(
location.monsters.every((monster) => monster.settings.type === "undead")
).toBeTruthy();
});

it('should be able to clone a location and change monsters type', () => {
it("should be able to clone a location and change monsters type", () => {
const location = new Location("dryHills", 3, { type: "undead" });
const newLocation = location.clone();

newLocation.name = "laveCavern";
newLocation.monstersSettings.type = "demon";

expect(newLocation.monsters.every(monster => monster.settings.type === 'demon')). toBeTruthy();
expect(location.monsters.every(monster => monster.settings.type === 'undead')). toBeTruthy();
expect(
newLocation.monsters.every((monster) => monster.settings.type === "demon")
).toBeTruthy();
expect(
location.monsters.every((monster) => monster.settings.type === "undead")
).toBeTruthy();
});
});
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 = null;

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

return Singleton.#instance;
}
}
6 changes: 3 additions & 3 deletions src/patterns/06-adapter/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const getArea = shape => {
export const getArea = (shape) => {
return shape.width * shape.height;
};

Expand All @@ -17,7 +17,7 @@ export class Rectangle {

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

4 changes: 2 additions & 2 deletions src/patterns/07-bridge/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Tv {
/* interface Tv {
getVolume(): number;
setVolume(): number;
//...
Expand Down Expand Up @@ -29,4 +29,4 @@ class Remote {
changeVolumeByStep(step = 10) {
this.device.setVolume(this.device.getVolume() + step)
}
}
} */
16 changes: 8 additions & 8 deletions src/patterns/07-bridge/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
export class RedColorsPallet {
constructor () {
this.color = 'red';
constructor() {
this.color = "red";
}
}

export class GreenColorsPallet {
constructor () {
this.color = 'green';
constructor() {
this.color = "green";
}
}

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

toString () {
toString() {
return `This is ${this.name} notification with ${this.colorsPallet.color} colors pallet`;
}
}

4 changes: 2 additions & 2 deletions src/patterns/08-composite/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Fabric {
/* class Fabric {
items;
draw() {
// logic
Expand Down Expand Up @@ -41,4 +41,4 @@ graph.items = [storage, fabric, node];
storage.items = [node1, node2];
fabric.items = [node3, node4]

graph.draw();
graph.draw(); */
9 changes: 4 additions & 5 deletions src/patterns/08-composite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export class Child {
this.value = value;
}

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

Expand All @@ -13,8 +13,7 @@ export class Parent {
this.values = values;
}

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

Loading