Skip to content

Commit edeb4ac

Browse files
committed
Add demo to factory method
1 parent 7a82472 commit edeb4ac

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

factory_method/demo.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="factoryMethod.ts" />
2+
3+
namespace FactoryMethodPattern {
4+
export namespace Demo {
5+
export function show() : void {
6+
var a: FactoryMethodPattern.AbstractProduct = FactoryMethodPattern.ProductFactory.createProduct("A");
7+
var b: FactoryMethodPattern.AbstractProduct = FactoryMethodPattern.ProductFactory.createProduct("B");
8+
9+
console.log(a.method());
10+
console.log(b.method());
11+
};
12+
}
13+
}
14+
15+
FactoryMethodPattern.Demo.show();

factory_method/factoryMethod.ts

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
1-
namespace FactoryMethodPattern {
2-
3-
export interface AbstractProduct {
4-
method(param?: any) : void;
5-
}
6-
7-
export class ConcreteProductA implements AbstractProduct {
8-
method = (param?: any) => {
9-
return "This is method of ConcreteProductA";
10-
}
11-
}
12-
13-
export class ConcreteProductB implements AbstractProduct {
14-
method = (param?: any) => {
15-
return "This is method of ConcreteProductB";
16-
}
17-
}
18-
19-
20-
export class ProductFactory {
21-
public static createProduct(type: string) : AbstractProduct {
22-
if (type === "A") {
23-
return new ConcreteProductA();
24-
} else if (type === "B") {
25-
return new ConcreteProductB();
26-
}
27-
28-
return null;
29-
}
30-
}
31-
}
32-
33-
(function main() {
34-
var a: FactoryMethodPattern.AbstractProduct = FactoryMethodPattern.ProductFactory.createProduct("A");
35-
var b: FactoryMethodPattern.AbstractProduct = FactoryMethodPattern.ProductFactory.createProduct("B");
36-
37-
console.log(a.method());
38-
console.log(b.method());
39-
}());
1+
namespace FactoryMethodPattern {
2+
3+
export interface AbstractProduct {
4+
method(param?: any) : void;
5+
}
6+
7+
export class ConcreteProductA implements AbstractProduct {
8+
method = (param?: any) => {
9+
return "Method of ConcreteProductA";
10+
}
11+
}
12+
13+
export class ConcreteProductB implements AbstractProduct {
14+
method = (param?: any) => {
15+
return "Method of ConcreteProductB";
16+
}
17+
}
18+
19+
20+
export class ProductFactory {
21+
public static createProduct(type: string) : AbstractProduct {
22+
if (type === "A") {
23+
return new ConcreteProductA();
24+
} else if (type === "B") {
25+
return new ConcreteProductB();
26+
}
27+
28+
return null;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)