Skip to content

Add 6 examples for Problem and Solution #38

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

Merged
merged 6 commits into from
Oct 19, 2023
Merged
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
84 changes: 84 additions & 0 deletions src/AbstractFactory/Book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
interface Chair {
sitOn(): void;
}
interface Sofa {
lieOn(): void;
}
interface CoffeeTable {
putCoffee(): void;
}

interface FurnitureFactory {
createChair(): Chair;
createSofa(): Sofa;
createCoffeeTable(): CoffeeTable;
}

class ModernChair implements Chair {
sitOn() {
console.log("Sitting on a modern chair.");
}
}
class ModernSofa implements Sofa {
lieOn() {
console.log("Lying on a modern sofa.");
}
}
class ModernCoffeeTable implements CoffeeTable {
putCoffee() {
console.log("Placing coffee on a modern coffee table.");
}
}

class VictorianChair implements Chair {
sitOn() {
console.log("Sitting on a Victorian chair.");
}
}
class VictorianSofa implements Sofa {
lieOn() {
console.log("Lying on a Victorian sofa.");
}
}
class VictorianCoffeeTable implements CoffeeTable {
putCoffee() {
console.log("Placing coffee on a Victorian coffee table.");
}
}

class ModernFurnitureFactory implements FurnitureFactory {
createChair(): Chair {
return new ModernChair();
}
createSofa(): Sofa {
return new ModernSofa();
}
createCoffeeTable(): CoffeeTable {
return new ModernCoffeeTable();
}
}

class VictorianFurnitureFactory implements FurnitureFactory {
createChair(): Chair {
return new VictorianChair();
}
createSofa(): Sofa {
return new VictorianSofa();
}
createCoffeeTable(): CoffeeTable {
return new VictorianCoffeeTable();
}
}

function createFurniture(factory: FurnitureFactory) {
const chair = factory.createChair();
const sofa = factory.createSofa();
const coffeeTable = factory.createCoffeeTable();

chair.sitOn();
sofa.lieOn();
coffeeTable.putCoffee();
}

createFurniture(new ModernFurnitureFactory());
createFurniture(new VictorianFurnitureFactory());
31 changes: 31 additions & 0 deletions src/Bridge/Book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
interface Color {
getColor(): string;
}
interface Shape {
getShape(): string;
}

class Red implements Color {
getColor(): string {
return "red";
}
}
class Circle implements Shape {
getShape(): string {
return "circle";
}
}

abstract class ShapeWithColor {
constructor(protected color: Color, protected shape: Shape) {}
abstract draw(): void;
}

class RedCircle extends ShapeWithColor {
draw(): void {
console.log(`${this.color.getColor()} ${this.shape.getShape()}`);
}
}

const redCircle = new RedCircle(new Red(), new Circle());
redCircle.draw();
138 changes: 138 additions & 0 deletions src/Builder/Book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
class House {
private walls: number;
private doors: number;
private windows: number;
private roof: string;
private swimmingPool: boolean;

setWalls(walls: number) {
this.walls = walls;
}

setDoors(doors: number) {
this.doors = doors;
}

setWindows(windows: number) {
this.windows = windows;
}

setRoof(roof: string) {
this.roof = roof;
}

setSwimmingPool(swimmingPool: boolean) {
this.swimmingPool = swimmingPool;
}

describeHouse() {
console.log("House description:");
console.log("Walls: ", this.walls);
console.log("Doors: ", this.doors);
console.log("Windows: ", this.windows);
console.log("Roof: ", this.roof);
console.log("Swimming pool: ", this.swimmingPool);
}
}

interface HouseBuilder {
buildWalls(): void;
buildDoors(): void;
buildWindows(): void;
buildRoof(): void;
buildSwimmingPool(): void;
getResult(): House;
}

class SmallHouseBuilder implements HouseBuilder {
private house: House;

constructor() {
this.house = new House();
}

buildWalls() {
this.house.setWalls(4);
}

buildDoors() {
this.house.setDoors(1);
}

buildWindows() {
this.house.setWindows(1);
}

buildRoof() {
this.house.setRoof("wooden roof");
}

buildSwimmingPool() {
this.house.setSwimmingPool(false);
}

getResult() {
return this.house;
}
}

class LargeHouseBuilder implements HouseBuilder {
private house: House;

constructor() {
this.house = new House();
}

buildWalls() {
this.house.setWalls(12);
}

buildDoors() {
this.house.setDoors(5);
}

buildWindows() {
this.house.setWindows(4);
}

buildRoof() {
this.house.setRoof("concrete roof");
}

buildSwimmingPool() {
this.house.setSwimmingPool(true);
}

getResult() {
return this.house;
}
}

class HouseDirector {
private builder: HouseBuilder;

constructor(builder: HouseBuilder) {
this.builder = builder;
}

constructHouse() {
this.builder.buildWalls();
this.builder.buildDoors();
this.builder.buildWindows();
this.builder.buildRoof();
this.builder.buildSwimmingPool();
}
}

const smallBuilder = new SmallHouseBuilder();
const largeBuilder = new LargeHouseBuilder();

const director1 = new HouseDirector(smallBuilder);
director1.constructHouse();
const smallHouse = smallBuilder.getResult();
smallHouse.describeHouse();

const director2 = new HouseDirector(largeBuilder);
director2.constructHouse();
const largeHouse = largeBuilder.getResult();
largeHouse.describeHouse();
44 changes: 44 additions & 0 deletions src/Composite/Book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export interface Component {
getPrice(): number;
}

class Product implements Component {
constructor(private name: string, private price: number) {}

getPrice(): number {
return this.price;
}
}

class Box implements Component {
private items: Component[] = [];

constructor(private name: string, private packagingCost: number) {}

addItem(item: Component) {
this.items.push(item);
}
removeItem(item: Component) {
this.items = this.items.filter((i) => i !== item);
}

getPrice(): number {
return (
this.packagingCost +
this.items.reduce((acc, item) => acc + item.getPrice(), 0)
);
}
}

const smallProduct = new Product("Small Product", 10);
const smallBox = new Box("Small Box", 1);

smallBox.addItem(smallProduct);

const bigProduct = new Product("Big Product", 100);
const bigBox = new Box("Big Box", 5);

bigBox.addItem(bigProduct);
bigBox.addItem(smallBox);

console.log(bigBox.getPrice());
49 changes: 49 additions & 0 deletions src/Decorator/Book/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export interface Notifier {
send(message: string): void;
}

class EmailNotifier implements Notifier {
constructor(private emails: string[]) {}

send(message: string): void {
console.log(`Sending email to ${this.emails}: ${message}`);
}
}

class SMSDecorator implements Notifier {
constructor(private notifier: Notifier) {}

send(message: string): void {
console.log(`Sending SMS: ${message}`);
this.notifier.send(message);
}
}

class FacebookDecorator implements Notifier {
constructor(private notifier: Notifier) {}

send(message: string): void {
console.log(`Sending Facebook message: ${message}`);
this.notifier.send(message);
}
}

class SlackDecorator implements Notifier {
constructor(private notifier: Notifier) {}

send(message: string): void {
console.log(`Sending Slack message: ${message}`);
this.notifier.send(message);
}
}

const emailNotifier = new EmailNotifier([
"[email protected]",
"[email protected]",
]);

const combinedNotifier = new SlackDecorator(
new FacebookDecorator(new SMSDecorator(emailNotifier))
);

combinedNotifier.send("Important message: House is on fire!");
Loading