Skip to content

Commit 2b47187

Browse files
authored
Merge pull request #39 from leemhoon00/main
add 5 examples
2 parents e48302c + 83cc34f commit 2b47187

File tree

5 files changed

+324
-0
lines changed

5 files changed

+324
-0
lines changed

src/Memento/Book/index.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export interface Memento {
2+
getName(): string;
3+
getSnapshotDate(): Date;
4+
getText(): string;
5+
}
6+
7+
class Editor {
8+
private text: string;
9+
10+
constructor(private name: string) {
11+
this.text = "";
12+
}
13+
14+
makeSnapshot(): Memento {
15+
return new Snapshot(this.name, this.text);
16+
}
17+
18+
restore(memento: Memento) {
19+
this.text = memento.getText();
20+
}
21+
22+
editText(newText: string) {
23+
this.text = newText;
24+
}
25+
26+
displayText() {
27+
console.log("Current Text: " + this.text);
28+
}
29+
}
30+
31+
class Snapshot implements Memento {
32+
private name: string;
33+
private text: string;
34+
private date: Date;
35+
36+
constructor(name: string, text: string) {
37+
this.name = name;
38+
this.text = text;
39+
this.date = new Date();
40+
}
41+
42+
getName(): string {
43+
return this.name;
44+
}
45+
46+
getSnapshotDate(): Date {
47+
return this.date;
48+
}
49+
50+
getText(): string {
51+
return this.text;
52+
}
53+
}
54+
55+
const editor = new Editor("Document 1");
56+
57+
editor.editText("This is the initial text");
58+
editor.displayText();
59+
60+
const snapshot1 = editor.makeSnapshot();
61+
62+
editor.editText("Text after editing");
63+
editor.displayText();
64+
65+
editor.restore(snapshot1);
66+
editor.displayText();

src/State/Book/index.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
export interface State {
2+
render(): void;
3+
publish(): void;
4+
}
5+
6+
class Document {
7+
private state: State;
8+
9+
constructor() {
10+
this.state = new DraftState(this);
11+
}
12+
13+
changeState(state: State) {
14+
this.state = state;
15+
}
16+
17+
render() {
18+
this.state.render();
19+
}
20+
21+
publish() {
22+
this.state.publish();
23+
}
24+
}
25+
26+
class DraftState implements State {
27+
constructor(private document: Document) {}
28+
29+
render() {
30+
console.log("Rendering the document in Draft state");
31+
}
32+
33+
publish() {
34+
console.log("Moving the document to Moderation state");
35+
this.document.changeState(new ModerationState(this.document));
36+
}
37+
}
38+
39+
class ModerationState implements State {
40+
constructor(private document: Document) {}
41+
42+
render() {
43+
console.log("Rendering the document in Moderation state");
44+
}
45+
46+
publish() {
47+
console.log("Making the document public in Published state");
48+
this.document.changeState(new PublishedState(this.document));
49+
}
50+
}
51+
52+
class PublishedState implements State {
53+
constructor(private document: Document) {}
54+
55+
render() {
56+
console.log("Rendering the document in Published state");
57+
}
58+
59+
publish() {
60+
console.log("The document is already in Published state. Nothing to do.");
61+
}
62+
}
63+
64+
const document = new Document();
65+
66+
document.render();
67+
document.publish();
68+
document.publish();
69+
document.render();
70+
document.publish();

src/Strategy/Book/index.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export interface RouteStrategy {
2+
buildRoute(origin: string, destination: string): string;
3+
}
4+
5+
class RoadStrategy implements RouteStrategy {
6+
buildRoute(origin: string, destination: string): string {
7+
return `Road route from ${origin} to ${destination}`;
8+
}
9+
}
10+
11+
class WalkingStrategy implements RouteStrategy {
12+
buildRoute(origin: string, destination: string): string {
13+
return `Walking route from ${origin} to ${destination}`;
14+
}
15+
}
16+
17+
class PublicTransportStrategy implements RouteStrategy {
18+
buildRoute(origin: string, destination: string): string {
19+
return `Public transport route from ${origin} to ${destination}`;
20+
}
21+
}
22+
23+
class Navigator {
24+
private routeStrategy: RouteStrategy;
25+
26+
setRouteStrategy(strategy: RouteStrategy) {
27+
this.routeStrategy = strategy;
28+
}
29+
30+
buildRoute(origin: string, destination: string): string {
31+
if (this.routeStrategy) {
32+
return this.routeStrategy.buildRoute(origin, destination);
33+
} else {
34+
return "No route strategy set.";
35+
}
36+
}
37+
}
38+
39+
const navigator = new Navigator();
40+
41+
navigator.setRouteStrategy(new RoadStrategy());
42+
console.log(navigator.buildRoute("City A", "City B"));
43+
44+
navigator.setRouteStrategy(new WalkingStrategy());
45+
console.log(navigator.buildRoute("Park X", "Museum Y"));
46+
47+
navigator.setRouteStrategy(new PublicTransportStrategy());
48+
console.log(navigator.buildRoute("Station P", "Station Q"));

src/TemplateMethod/Book/index.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
abstract class DataMiner {
2+
// Template method: Define the overall data mining process
3+
mine(path: string): void {
4+
this.openFile(path);
5+
this.extractData();
6+
this.parseData();
7+
this.analyzeData();
8+
this.sendReport();
9+
this.closeFile();
10+
}
11+
12+
// Abstract methods: Subclasses must implement these
13+
abstract openFile(path: string): void;
14+
abstract extractData(): void;
15+
abstract closeFile(): void;
16+
abstract parseData(): void;
17+
18+
// Optional methods: Default implementations are provided and can be overridden
19+
analyzeData() {
20+
console.log("Analyzing data... (Default implementation)");
21+
}
22+
23+
sendReport() {
24+
console.log("Sending report... (Default implementation)");
25+
}
26+
}
27+
28+
class DocDataMiner extends DataMiner {
29+
openFile(path: string) {
30+
console.log(`Opening DOC file: ${path}`);
31+
}
32+
33+
extractData() {
34+
console.log("Extracting data from DOC file");
35+
}
36+
37+
closeFile() {
38+
console.log("Closing DOC file");
39+
}
40+
41+
parseData() {
42+
console.log("Parsing data from DOC file");
43+
}
44+
}
45+
46+
class CSVDataMiner extends DataMiner {
47+
openFile(path: string) {
48+
console.log(`Opening CSV file: ${path}`);
49+
}
50+
51+
extractData() {
52+
console.log("Extracting data from CSV file");
53+
}
54+
55+
closeFile() {
56+
console.log("Closing CSV file");
57+
}
58+
59+
parseData() {
60+
console.log("Parsing data from CSV file");
61+
}
62+
}
63+
64+
class PDFDataMiner extends DataMiner {
65+
openFile(path: string) {
66+
console.log(`Opening PDF file: ${path}`);
67+
}
68+
69+
extractData() {
70+
console.log("Extracting data from PDF file");
71+
}
72+
73+
closeFile() {
74+
console.log("Closing PDF file");
75+
}
76+
77+
parseData() {
78+
console.log("Parsing data from PDF file");
79+
}
80+
}
81+
82+
// Client code
83+
const docDataMiner = new DocDataMiner();
84+
const csvDataMiner = new CSVDataMiner();
85+
const pdfDataMiner = new PDFDataMiner();
86+
87+
docDataMiner.mine("data.doc");
88+
csvDataMiner.mine("data.csv");
89+
pdfDataMiner.mine("data.pdf");

src/Visitor/Book/index.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export interface Visitor {
2+
doForCity(city: City): void;
3+
doForIndustry(industry: Industry): void;
4+
doForSightSeeing(sightSeeing: SightSeeing): void;
5+
}
6+
7+
abstract class Node {
8+
abstract accept(visitor: Visitor): void;
9+
}
10+
11+
class City extends Node {
12+
accept(visitor: Visitor) {
13+
visitor.doForCity(this);
14+
}
15+
}
16+
17+
class Industry extends Node {
18+
accept(visitor: Visitor) {
19+
visitor.doForIndustry(this);
20+
}
21+
}
22+
23+
class SightSeeing extends Node {
24+
accept(visitor: Visitor) {
25+
visitor.doForSightSeeing(this);
26+
}
27+
}
28+
29+
class ExportVisitor implements Visitor {
30+
doForCity(city: City) {
31+
console.log(`Exporting City node to XML: ${city}`);
32+
}
33+
34+
doForIndustry(industry: Industry) {
35+
console.log(`Exporting Industry node to XML: ${industry}`);
36+
}
37+
38+
doForSightSeeing(sightSeeing: SightSeeing) {
39+
console.log(`Exporting SightSeeing node to XML: ${sightSeeing}`);
40+
}
41+
}
42+
43+
const graph: Node[] = [];
44+
45+
graph.push(new City());
46+
graph.push(new Industry());
47+
graph.push(new SightSeeing());
48+
49+
const xmlExportVisitor = new ExportVisitor();
50+
51+
graph.forEach((node) => node.accept(xmlExportVisitor));

0 commit comments

Comments
 (0)