diff --git a/design-patterns/Chain of Responisibility-Node JS/README.md b/design-patterns/Chain of Responisibility-Node JS/README.md new file mode 100644 index 0000000..f0eb513 --- /dev/null +++ b/design-patterns/Chain of Responisibility-Node JS/README.md @@ -0,0 +1,20 @@ +# Chain of Respoinsibility Pattern + +1. The Chain of Responsibility pattern provides a chain of loosely coupled objects one of which can satisfy a request. This pattern is essentially a linear search for an object that can handle a particular request. + +2. It is like event-bubbling in which an event propagates through a series of nested controls one of which may choose to handle the event. The Chain of Responsiblity patterns is related to the Chaining Pattern which is frequently used in JavaScript. + +3. In this example, we create a class CumulativeAddition, which can be instantiated with an optional initialsum. It has a method add that adds the passed value to the sum attribute of the object and returns the object itself to allow chaining of add method calls. + + +##### JavaScript example + +![](UML.png) + + +###### Output +``` +12 +35 +108 +``` \ No newline at end of file diff --git a/design-patterns/Chain of Responisibility-Node JS/UML.png b/design-patterns/Chain of Responisibility-Node JS/UML.png new file mode 100644 index 0000000..d429a92 Binary files /dev/null and b/design-patterns/Chain of Responisibility-Node JS/UML.png differ diff --git a/design-patterns/Chain of Responisibility-Node JS/chainofresponsibility.js b/design-patterns/Chain of Responisibility-Node JS/chainofresponsibility.js new file mode 100644 index 0000000..770250e --- /dev/null +++ b/design-patterns/Chain of Responisibility-Node JS/chainofresponsibility.js @@ -0,0 +1,22 @@ +class CumulativeAddition { + constructor(intialsum = 0) { + + this.sum = intialsum; + } + + add(number) { + this.sum = this.sum + number; + return this; + } + +} + +const sum1 = new CumulativeAddition(); +console.log(sum1.add(10).add(2).sum); + +const sum2 = new CumulativeAddition(5); +console.log(sum2.add(10).add(20).sum); + +const sum3 = new CumulativeAddition(1); +console.log(sum3.add(98).add(1).add(8).sum); + diff --git a/design-patterns/Command Pattern-Node JS/README.md b/design-patterns/Command Pattern-Node JS/README.md new file mode 100644 index 0000000..c617f62 --- /dev/null +++ b/design-patterns/Command Pattern-Node JS/README.md @@ -0,0 +1,17 @@ +# Command Pattern + +1. The Command pattern encapsulates actions as objects. Command objects allow for loosely coupled systems by separating the objects that issue a request from the objects that actually process the request. These requests are called events and the code that processes the requests are called event handlers. + +2. It aims to encapsulate method invocation, requests, or operations into a single object and gives us the ability to both parameterize and pass method calls around that can be executed at our discretion. In addition, it enables us to decouple objects invoking the action from the objects that implement them, giving us a greater degree of overall flexibility in swapping out concrete classes (objects). + +###### UML + +![](UML.png) + + +3. In this example, we have a class called Math that has multiple methods and a Command class that encapsulates commands that are to be executed on its subject, i.e. an object of the Math class. The Command class also keeps track of all the commands executed, which can be used to extend its functionality to include undo and redo type operations. + +# output + +['square', 'cube'] + diff --git a/design-patterns/Command Pattern-Node JS/UML.png b/design-patterns/Command Pattern-Node JS/UML.png new file mode 100644 index 0000000..987a275 Binary files /dev/null and b/design-patterns/Command Pattern-Node JS/UML.png differ diff --git a/design-patterns/Command Pattern-Node JS/command.js b/design-patterns/Command Pattern-Node JS/command.js new file mode 100644 index 0000000..d725e94 --- /dev/null +++ b/design-patterns/Command Pattern-Node JS/command.js @@ -0,0 +1,30 @@ +class Math { + constructor(num) { + this._num = num; + } + square() { + return this._num ** 2; + } + + cube() { + return this._num ** 3; + } +} + +class Command { + constructor(subject) { + this._subject = subject; + this.commandsExecuted = []; + } + execute(command) { + this.commandsExecuted.push(command); + return this._subject[command](); + } +} + +let x = new Command(new Math(5)); +x.execute('square'); +x.execute('cube'); + +console.log(x.commandsExecuted); + diff --git a/design-patterns/Iterator Pattern-Node JS/Iterator.js b/design-patterns/Iterator Pattern-Node JS/Iterator.js new file mode 100644 index 0000000..1652703 --- /dev/null +++ b/design-patterns/Iterator Pattern-Node JS/Iterator.js @@ -0,0 +1,25 @@ +const items = [630, false ,"Design", 4.1] + +function Iterator(items) +{ + this.items = items + this.index = 0 +} + +Iterator.prototype = { + hasNext: function() + { + return this.index < this.items.length + }, + next: function() + { + return this.items[this.index++] + } +} + +const iter = new Iterator(items) + +while(iter.hasNext() == true) + console.log(iter.next()) + + \ No newline at end of file diff --git a/design-patterns/Iterator Pattern-Node JS/README.md b/design-patterns/Iterator Pattern-Node JS/README.md new file mode 100644 index 0000000..f3a7b04 --- /dev/null +++ b/design-patterns/Iterator Pattern-Node JS/README.md @@ -0,0 +1,23 @@ +# Iterator Design Pattern + +1. The iterator pattern allows us to loop over some collection of objects its a common programming task to iterater over a set of objects may be stored as arrays tree or a graphs. + +2. In addition to traverse the collection of items you may need to access the items in the collection in a certain way or order from first to last or last to first depth first or breath first it its a tree or a graph or skip every two elements or skip every three elements ot anything + +3. The iterator pattern allows you to write the rules on how to traverse some collectio in this example we will +create an array to iterate. + +###### UML +![](UML.png) + +In this example we see a class called math and contains two functions square and cube that returns square of a +number and cube of a number. there is a command class that will records all the commands that are executed so +here we will execute the commands square and cube and when we print the commands we can see that we executed square and cube functions. + +###### output +``` +630 +false +Design +4.1 +``` diff --git a/design-patterns/Iterator Pattern-Node JS/UML.png b/design-patterns/Iterator Pattern-Node JS/UML.png new file mode 100644 index 0000000..c66639d Binary files /dev/null and b/design-patterns/Iterator Pattern-Node JS/UML.png differ diff --git a/design-patterns/State Pattern-Node JS/README.md b/design-patterns/State Pattern-Node JS/README.md new file mode 100644 index 0000000..209af06 --- /dev/null +++ b/design-patterns/State Pattern-Node JS/README.md @@ -0,0 +1,22 @@ +# State Design Pattern + +State pattern is one of the behavioral design pattern. State design pattern is used when an Object changes its behavior based on its internal state. + +1. It is a behavioural design pattern that allows an object to alter its behaviour based on changes to its internal state. The object returned by a state pattern class seems to change its class. + +2. It provides state-specific logic to a limited set of objects in which each object type represents a particular state. + +3. We will take a simple example of a Software Development stages to understand this pattern. The Stoftware Development class changes the object it returns based on its internal state, which is an object of testing, Commiting, or Coding class. + +##### UML +![](UML.jpg) + +###### Output +``` +Development Coding +Staging Commits +Production Testing +Development Coding +Staging Commits +Production Testing +``` \ No newline at end of file diff --git a/design-patterns/State Pattern-Node JS/UML.jpg b/design-patterns/State Pattern-Node JS/UML.jpg new file mode 100644 index 0000000..832da6a Binary files /dev/null and b/design-patterns/State Pattern-Node JS/UML.jpg differ diff --git a/design-patterns/State Pattern-Node JS/state.js b/design-patterns/State Pattern-Node JS/state.js new file mode 100644 index 0000000..d21b68d --- /dev/null +++ b/design-patterns/State Pattern-Node JS/state.js @@ -0,0 +1,74 @@ +class SDLCstages { + constructor() { + this.states = [new Dev(), new Staging(), new Prod()]; + this.current = this.states[0]; + } + + change() { + const totalStates = this.states.length; + let currentIndex = this.states.findIndex(light => light === this.current); + if (currentIndex + 1 < totalStates) this.current = this.states[currentIndex + 1]; + else this.current = this.states[0]; + } + + sign() { + return this.current.sign(); + } +} + +class States { + constructor(light) { + this.light = light; + } +} + +class Prod extends States { + constructor() { + super('Testing'); + } + + sign() { + return 'Production Testing'; + } +} + +class Staging extends States { + constructor() { + super('Commiting'); + } + + sign() { + return 'Staging Commits'; + } +} + +class Dev extends States { + constructor() { + super('Coding'); + } + + sign() { + return 'Development Coding'; + } +} + + +const product = new SDLCstages(); + +console.log(product.sign()); // 'Development Coding' +product.change(); + +console.log(product.sign()); // 'Staging commits' +product.change(); + +console.log(product.sign()); // 'Production Testing' +product.change(); + +console.log(product.sign()); // 'Development Coding' +product.change(); + +console.log(product.sign()); // 'Staging commits' +product.change(); + +console.log(product.sign()); // 'Production Testing' +