Skip to content

Commit

Permalink
feat(dp): move examples to readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoRVSN committed Aug 19, 2023
1 parent dd1cbc2 commit 24802a3
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 561 deletions.
133 changes: 133 additions & 0 deletions docs/design_patterns/behavioral/Observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,136 @@ ___

- When a change in a object require a change in another object
- When some objects needs to observe others in specific cases

## Example (Typescript)

```ts
/* eslint-disable */

/**
* The Subject interface declares a set of methods for managing subscribers.
*/
interface Subject {
// Attach an observer to the subject.
attach(observer: Observer): void;

// Detach an observer from the subject.
detach(observer: Observer): void;

// Notify all observers about an event.
notify(): void;
}

/**
* The Subject owns some important state and notifies observers when the state
* changes.
*/
class ConcreteSubject implements Subject {
/**
* @type {number} For the sake of simplicity, the Subject's state, essential
* to all subscribers, is stored in this variable.
*/
public state!: number;

/**
* @type {Observer[]} List of subscribers. In real life, the list of
* subscribers can be stored more comprehensively (categorized by event
* type, etc.).
*/
private observers: Observer[] = [];

/**
* The subscription management methods.
*/
public attach(observer: Observer): void {
const isExist = this.observers.some((obs) => obs === observer);
if (isExist) {
return console.log('Subject: Observer has been attached already.');
}

console.log('Subject: Attached an observer.');
this.observers.push(observer);
}

public detach(observer: Observer): void {
const observerIndex = this.observers.indexOf(observer);
if (observerIndex === -1) {
return console.log('Subject: Nonexistent observer.');
}

this.observers.splice(observerIndex, 1);
console.log('Subject: Detached an observer.');
}

/**
* Trigger an update in each subscriber.
*/
public notify(): void {
console.log('Subject: Notifying observers...');
for (const observer of this.observers) {
observer.update(this);
}
}

/**
* Usually, the subscription logic is only a fraction of what a Subject can
* really do. Subjects commonly hold some important business logic, that
* triggers a notification method whenever something important is about to
* happen (or after it).
*/
public someBusinessLogic(): void {
console.log('\nSubject: I\'m doing something important.');
this.state = Math.floor(Math.random() * (10 + 1));

console.log(`Subject: My state has just changed to: ${this.state}`);
this.notify();
}
}

/**
* The Observer interface declares the update method, used by subjects.
*/
interface Observer {
// Receive update from subject.
update(subject: Subject): void;
}

/**
* Concrete Observers react to the updates issued by the Subject they had been
* attached to.
*/
class ConcreteObserverA implements Observer {
public update(subject: Subject): void {
if (subject instanceof ConcreteSubject && subject.state < 3) {
console.log('ConcreteObserverA: Reacted to the event.');
}
}
}

class ConcreteObserverB implements Observer {
public update(subject: Subject): void {
if (subject instanceof ConcreteSubject && (subject.state === 0 || subject.state >= 2)) {
console.log('ConcreteObserverB: Reacted to the event.');
}
}
}

/**
* The client code.
*/

const subject = new ConcreteSubject();

const observer1 = new ConcreteObserverA();
subject.attach(observer1);

const observer2 = new ConcreteObserverB();
subject.attach(observer2);

subject.someBusinessLogic();
subject.someBusinessLogic();

subject.detach(observer2);

subject.someBusinessLogic();
```
128 changes: 0 additions & 128 deletions docs/design_patterns/behavioral/Observer/observer-example.ts

This file was deleted.

50 changes: 49 additions & 1 deletion docs/design_patterns/behavioral/Strategy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,52 @@ ___
- When we want to use variations of an algorithm and be able to switch one to another during runtime.
- When we have similar classes that differ the way they execute the behavior.
- When we want to have an isolate business logic
- When the class has a lot of conditional statement to change the behavior of the same algorithm
- When the class has a lot of conditional statement to change the behavior of the same algorithm

## Example (Typescript)

```ts
interface AnimalStrategy {
saySomething(): string;
}

class Animal {
// Keep a reference to the AnimalStrategy, but don't know the concrete class of the AnimalStrategy
private strategy: AnimalStrategy;

// Define strategy in constructor
constructor(strategy: AnimalStrategy) {
this.strategy = strategy;
}

// Let to change the strategy in runtime
public setStrategy(strategy: AnimalStrategy) {
this.strategy = strategy;
}

// The Animal class use the strategy to implement the method
public giveAHello(): string {
return this.strategy.saySomething();
}
}

// Classes implementing the AnimalStrategy to define the behavior that the class will use
class Dog implements AnimalStrategy {
public saySomething(): string {
return "Au au";
}
}

class Cat implements AnimalStrategy {
public saySomething(): string {
return "Meow";
}
}

// Define a strategy and keep it in runtime
const context = new Animal(new Dog());
console.log('Animal Strategy is a Dog: ', context.giveAHello());

context.setStrategy(new Cat());
console.log('Animal Strategy is a Cat: ', context.giveAHello());
```
43 changes: 0 additions & 43 deletions docs/design_patterns/behavioral/Strategy/strategy-example.ts

This file was deleted.

Loading

0 comments on commit 24802a3

Please sign in to comment.