Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding chain command state Iterator design patterns #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions design-patterns/Chain of Responisibility-Node JS/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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);

17 changes: 17 additions & 0 deletions design-patterns/Command Pattern-Node JS/README.md
Original file line number Diff line number Diff line change
@@ -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']

Binary file added design-patterns/Command Pattern-Node JS/UML.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions design-patterns/Command Pattern-Node JS/command.js
Original file line number Diff line number Diff line change
@@ -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);

25 changes: 25 additions & 0 deletions design-patterns/Iterator Pattern-Node JS/Iterator.js
Original file line number Diff line number Diff line change
@@ -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())


23 changes: 23 additions & 0 deletions design-patterns/Iterator Pattern-Node JS/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Binary file added design-patterns/Iterator Pattern-Node JS/UML.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions design-patterns/State Pattern-Node JS/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Binary file added design-patterns/State Pattern-Node JS/UML.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions design-patterns/State Pattern-Node JS/state.js
Original file line number Diff line number Diff line change
@@ -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'