Skip to content

Commit e48302c

Browse files
authored
Merge pull request #38 from leemhoon00/main
Add 6 examples for Problem and Solution
2 parents 58c0d53 + fc9b815 commit e48302c

File tree

6 files changed

+397
-0
lines changed

6 files changed

+397
-0
lines changed

src/AbstractFactory/Book/index.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
interface Chair {
2+
sitOn(): void;
3+
}
4+
interface Sofa {
5+
lieOn(): void;
6+
}
7+
interface CoffeeTable {
8+
putCoffee(): void;
9+
}
10+
11+
interface FurnitureFactory {
12+
createChair(): Chair;
13+
createSofa(): Sofa;
14+
createCoffeeTable(): CoffeeTable;
15+
}
16+
17+
class ModernChair implements Chair {
18+
sitOn() {
19+
console.log("Sitting on a modern chair.");
20+
}
21+
}
22+
class ModernSofa implements Sofa {
23+
lieOn() {
24+
console.log("Lying on a modern sofa.");
25+
}
26+
}
27+
class ModernCoffeeTable implements CoffeeTable {
28+
putCoffee() {
29+
console.log("Placing coffee on a modern coffee table.");
30+
}
31+
}
32+
33+
class VictorianChair implements Chair {
34+
sitOn() {
35+
console.log("Sitting on a Victorian chair.");
36+
}
37+
}
38+
class VictorianSofa implements Sofa {
39+
lieOn() {
40+
console.log("Lying on a Victorian sofa.");
41+
}
42+
}
43+
class VictorianCoffeeTable implements CoffeeTable {
44+
putCoffee() {
45+
console.log("Placing coffee on a Victorian coffee table.");
46+
}
47+
}
48+
49+
class ModernFurnitureFactory implements FurnitureFactory {
50+
createChair(): Chair {
51+
return new ModernChair();
52+
}
53+
createSofa(): Sofa {
54+
return new ModernSofa();
55+
}
56+
createCoffeeTable(): CoffeeTable {
57+
return new ModernCoffeeTable();
58+
}
59+
}
60+
61+
class VictorianFurnitureFactory implements FurnitureFactory {
62+
createChair(): Chair {
63+
return new VictorianChair();
64+
}
65+
createSofa(): Sofa {
66+
return new VictorianSofa();
67+
}
68+
createCoffeeTable(): CoffeeTable {
69+
return new VictorianCoffeeTable();
70+
}
71+
}
72+
73+
function createFurniture(factory: FurnitureFactory) {
74+
const chair = factory.createChair();
75+
const sofa = factory.createSofa();
76+
const coffeeTable = factory.createCoffeeTable();
77+
78+
chair.sitOn();
79+
sofa.lieOn();
80+
coffeeTable.putCoffee();
81+
}
82+
83+
createFurniture(new ModernFurnitureFactory());
84+
createFurniture(new VictorianFurnitureFactory());

src/Bridge/Book/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface Color {
2+
getColor(): string;
3+
}
4+
interface Shape {
5+
getShape(): string;
6+
}
7+
8+
class Red implements Color {
9+
getColor(): string {
10+
return "red";
11+
}
12+
}
13+
class Circle implements Shape {
14+
getShape(): string {
15+
return "circle";
16+
}
17+
}
18+
19+
abstract class ShapeWithColor {
20+
constructor(protected color: Color, protected shape: Shape) {}
21+
abstract draw(): void;
22+
}
23+
24+
class RedCircle extends ShapeWithColor {
25+
draw(): void {
26+
console.log(`${this.color.getColor()} ${this.shape.getShape()}`);
27+
}
28+
}
29+
30+
const redCircle = new RedCircle(new Red(), new Circle());
31+
redCircle.draw();

src/Builder/Book/index.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
class House {
2+
private walls: number;
3+
private doors: number;
4+
private windows: number;
5+
private roof: string;
6+
private swimmingPool: boolean;
7+
8+
setWalls(walls: number) {
9+
this.walls = walls;
10+
}
11+
12+
setDoors(doors: number) {
13+
this.doors = doors;
14+
}
15+
16+
setWindows(windows: number) {
17+
this.windows = windows;
18+
}
19+
20+
setRoof(roof: string) {
21+
this.roof = roof;
22+
}
23+
24+
setSwimmingPool(swimmingPool: boolean) {
25+
this.swimmingPool = swimmingPool;
26+
}
27+
28+
describeHouse() {
29+
console.log("House description:");
30+
console.log("Walls: ", this.walls);
31+
console.log("Doors: ", this.doors);
32+
console.log("Windows: ", this.windows);
33+
console.log("Roof: ", this.roof);
34+
console.log("Swimming pool: ", this.swimmingPool);
35+
}
36+
}
37+
38+
interface HouseBuilder {
39+
buildWalls(): void;
40+
buildDoors(): void;
41+
buildWindows(): void;
42+
buildRoof(): void;
43+
buildSwimmingPool(): void;
44+
getResult(): House;
45+
}
46+
47+
class SmallHouseBuilder implements HouseBuilder {
48+
private house: House;
49+
50+
constructor() {
51+
this.house = new House();
52+
}
53+
54+
buildWalls() {
55+
this.house.setWalls(4);
56+
}
57+
58+
buildDoors() {
59+
this.house.setDoors(1);
60+
}
61+
62+
buildWindows() {
63+
this.house.setWindows(1);
64+
}
65+
66+
buildRoof() {
67+
this.house.setRoof("wooden roof");
68+
}
69+
70+
buildSwimmingPool() {
71+
this.house.setSwimmingPool(false);
72+
}
73+
74+
getResult() {
75+
return this.house;
76+
}
77+
}
78+
79+
class LargeHouseBuilder implements HouseBuilder {
80+
private house: House;
81+
82+
constructor() {
83+
this.house = new House();
84+
}
85+
86+
buildWalls() {
87+
this.house.setWalls(12);
88+
}
89+
90+
buildDoors() {
91+
this.house.setDoors(5);
92+
}
93+
94+
buildWindows() {
95+
this.house.setWindows(4);
96+
}
97+
98+
buildRoof() {
99+
this.house.setRoof("concrete roof");
100+
}
101+
102+
buildSwimmingPool() {
103+
this.house.setSwimmingPool(true);
104+
}
105+
106+
getResult() {
107+
return this.house;
108+
}
109+
}
110+
111+
class HouseDirector {
112+
private builder: HouseBuilder;
113+
114+
constructor(builder: HouseBuilder) {
115+
this.builder = builder;
116+
}
117+
118+
constructHouse() {
119+
this.builder.buildWalls();
120+
this.builder.buildDoors();
121+
this.builder.buildWindows();
122+
this.builder.buildRoof();
123+
this.builder.buildSwimmingPool();
124+
}
125+
}
126+
127+
const smallBuilder = new SmallHouseBuilder();
128+
const largeBuilder = new LargeHouseBuilder();
129+
130+
const director1 = new HouseDirector(smallBuilder);
131+
director1.constructHouse();
132+
const smallHouse = smallBuilder.getResult();
133+
smallHouse.describeHouse();
134+
135+
const director2 = new HouseDirector(largeBuilder);
136+
director2.constructHouse();
137+
const largeHouse = largeBuilder.getResult();
138+
largeHouse.describeHouse();

src/Composite/Book/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export interface Component {
2+
getPrice(): number;
3+
}
4+
5+
class Product implements Component {
6+
constructor(private name: string, private price: number) {}
7+
8+
getPrice(): number {
9+
return this.price;
10+
}
11+
}
12+
13+
class Box implements Component {
14+
private items: Component[] = [];
15+
16+
constructor(private name: string, private packagingCost: number) {}
17+
18+
addItem(item: Component) {
19+
this.items.push(item);
20+
}
21+
removeItem(item: Component) {
22+
this.items = this.items.filter((i) => i !== item);
23+
}
24+
25+
getPrice(): number {
26+
return (
27+
this.packagingCost +
28+
this.items.reduce((acc, item) => acc + item.getPrice(), 0)
29+
);
30+
}
31+
}
32+
33+
const smallProduct = new Product("Small Product", 10);
34+
const smallBox = new Box("Small Box", 1);
35+
36+
smallBox.addItem(smallProduct);
37+
38+
const bigProduct = new Product("Big Product", 100);
39+
const bigBox = new Box("Big Box", 5);
40+
41+
bigBox.addItem(bigProduct);
42+
bigBox.addItem(smallBox);
43+
44+
console.log(bigBox.getPrice());

src/Decorator/Book/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export interface Notifier {
2+
send(message: string): void;
3+
}
4+
5+
class EmailNotifier implements Notifier {
6+
constructor(private emails: string[]) {}
7+
8+
send(message: string): void {
9+
console.log(`Sending email to ${this.emails}: ${message}`);
10+
}
11+
}
12+
13+
class SMSDecorator implements Notifier {
14+
constructor(private notifier: Notifier) {}
15+
16+
send(message: string): void {
17+
console.log(`Sending SMS: ${message}`);
18+
this.notifier.send(message);
19+
}
20+
}
21+
22+
class FacebookDecorator implements Notifier {
23+
constructor(private notifier: Notifier) {}
24+
25+
send(message: string): void {
26+
console.log(`Sending Facebook message: ${message}`);
27+
this.notifier.send(message);
28+
}
29+
}
30+
31+
class SlackDecorator implements Notifier {
32+
constructor(private notifier: Notifier) {}
33+
34+
send(message: string): void {
35+
console.log(`Sending Slack message: ${message}`);
36+
this.notifier.send(message);
37+
}
38+
}
39+
40+
const emailNotifier = new EmailNotifier([
41+
42+
43+
]);
44+
45+
const combinedNotifier = new SlackDecorator(
46+
new FacebookDecorator(new SMSDecorator(emailNotifier))
47+
);
48+
49+
combinedNotifier.send("Important message: House is on fire!");

0 commit comments

Comments
 (0)