Skip to content

Commit f35e26c

Browse files
committed
Add namespace for each pattern
1 parent bf70021 commit f35e26c

File tree

24 files changed

+816
-773
lines changed

24 files changed

+816
-773
lines changed
Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,87 @@
1-
interface AbstractProductA {
2-
methodA(): string;
3-
}
4-
interface AbstractProductB {
5-
methodB(): number;
6-
}
7-
8-
interface AbstractFactory {
9-
createProductA(param?: any) : AbstractProductA;
10-
createProductB() : AbstractProductB;
11-
}
12-
13-
14-
class ProductA1 implements AbstractProductA {
15-
methodA = () => {
16-
return "This is methodA of ProductA1";
1+
namespace AbstractFactoryPattern {
2+
export interface AbstractProductA {
3+
methodA(): string;
174
}
18-
}
19-
class ProductB1 implements AbstractProductB {
20-
methodB = () => {
21-
return 1;
5+
export interface AbstractProductB {
6+
methodB(): number;
227
}
23-
}
248

25-
class ProductA2 implements AbstractProductA {
26-
methodA = () => {
27-
return "This is methodA of ProductA2";
9+
export interface AbstractFactory {
10+
createProductA(param?: any) : AbstractProductA;
11+
createProductB() : AbstractProductB;
2812
}
29-
}
30-
class ProductB2 implements AbstractProductB {
31-
methodB = () => {
32-
return 2;
33-
}
34-
}
3513

3614

37-
class ConcreteFactory1 implements AbstractFactory {
38-
createProductA(param?: any) : AbstractProductA {
39-
return new ProductA1();
15+
export class ProductA1 implements AbstractProductA {
16+
methodA = () => {
17+
return "This is methodA of ProductA1";
18+
}
19+
}
20+
export class ProductB1 implements AbstractProductB {
21+
methodB = () => {
22+
return 1;
23+
}
4024
}
4125

42-
createProductB(param?: any) : AbstractProductB {
43-
return new ProductB1();
26+
export class ProductA2 implements AbstractProductA {
27+
methodA = () => {
28+
return "This is methodA of ProductA2";
29+
}
4430
}
45-
}
46-
class ConcreteFactory2 implements AbstractFactory {
47-
createProductA(param?: any) : AbstractProductA {
48-
return new ProductA2();
31+
export class ProductB2 implements AbstractProductB {
32+
methodB = () => {
33+
return 2;
34+
}
4935
}
5036

51-
createProductB(param?: any) : AbstractProductB {
52-
return new ProductB2();
37+
38+
export class ConcreteFactory1 implements AbstractFactory {
39+
createProductA(param?: any) : AbstractProductA {
40+
return new ProductA1();
41+
}
42+
43+
createProductB(param?: any) : AbstractProductB {
44+
return new ProductB1();
45+
}
46+
}
47+
export class ConcreteFactory2 implements AbstractFactory {
48+
createProductA(param?: any) : AbstractProductA {
49+
return new ProductA2();
50+
}
51+
52+
createProductB(param?: any) : AbstractProductB {
53+
return new ProductB2();
54+
}
5355
}
54-
}
5556

5657

57-
class Tester {
58-
private abstractProductA: AbstractProductA;
59-
private abstractProductB: AbstractProductB;
58+
export class Tester {
59+
private abstractProductA: AbstractProductA;
60+
private abstractProductB: AbstractProductB;
6061

61-
constructor(factory: AbstractFactory) {
62-
this.abstractProductA = factory.createProductA();
63-
this.abstractProductB = factory.createProductB();
64-
}
62+
constructor(factory: AbstractFactory) {
63+
this.abstractProductA = factory.createProductA();
64+
this.abstractProductB = factory.createProductB();
65+
}
6566

66-
public test(): void {
67-
console.log(this.abstractProductA.methodA());
68-
console.log(this.abstractProductB.methodB());
67+
public test(): void {
68+
console.log(this.abstractProductA.methodA());
69+
console.log(this.abstractProductB.methodB());
70+
}
6971
}
70-
}
72+
73+
}
7174

7275
(function main() {
7376

7477
// Abstract factory1
75-
var factory1: AbstractFactory = new ConcreteFactory1();
76-
var tester1: Tester = new Tester(factory1);
78+
var factory1: AbstractFactoryPattern.AbstractFactory = new AbstractFactoryPattern.ConcreteFactory1();
79+
var tester1: AbstractFactoryPattern.Tester = new AbstractFactoryPattern.Tester(factory1);
7780
tester1.test();
7881

7982
// Abstract factory2
80-
var factory2: AbstractFactory = new ConcreteFactory2();
81-
var tester2: Tester = new Tester(factory2);
83+
var factory2: AbstractFactoryPattern.AbstractFactory = new AbstractFactoryPattern.ConcreteFactory2();
84+
var tester2: AbstractFactoryPattern.Tester = new AbstractFactoryPattern.Tester(factory2);
8285
tester2.test();
8386

8487
}());

adapter/adapter.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
class Adaptee {
2-
public method(): void {
3-
console.log("`method` of Adaptee is being called");
4-
}
5-
}
1+
namespace AdapterPattern {
62

7-
interface Target {
8-
call(): void;
9-
}
3+
export class Adaptee {
4+
public method(): void {
5+
console.log("`method` of Adaptee is being called");
6+
}
7+
}
8+
9+
export interface Target {
10+
call(): void;
11+
}
1012

11-
class Adapter implements Target {
12-
public call(): void {
13-
console.log("Adapter's `call` method is being called");
14-
var adaptee: Adaptee = new Adaptee();
15-
adaptee.method();
16-
}
13+
export class Adapter implements Target {
14+
public call(): void {
15+
console.log("Adapter's `call` method is being called");
16+
var adaptee: Adaptee = new Adaptee();
17+
adaptee.method();
18+
}
19+
}
1720
}
1821

1922
(function main() {
20-
var adapter: Adapter = new Adapter();
21-
adapter.call();
23+
var adapter: AdapterPattern.Adapter = new AdapterPattern.Adapter();
24+
adapter.call();
2225
}());

bridge/bridge.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
1-
class Abstraction {
2-
implementor: Implementor;
3-
constructor(imp: Implementor) {
4-
this.implementor = imp;
5-
}
1+
namespace BridgePattern {
62

7-
public callIt(s: String): void {
8-
throw new Error("This method is abstract!");
9-
}
10-
}
3+
export class Abstraction {
4+
implementor: Implementor;
5+
constructor(imp: Implementor) {
6+
this.implementor = imp;
7+
}
118

12-
class RefinedAbstractionA extends Abstraction {
13-
constructor(imp: Implementor) {
14-
super(imp);
9+
public callIt(s: String): void {
10+
throw new Error("This method is abstract!");
11+
}
1512
}
1613

17-
public callIt(s: String): void {
18-
console.log("This is RefinedAbstractionA");
19-
this.implementor.callee(s);
20-
}
21-
}
14+
export class RefinedAbstractionA extends Abstraction {
15+
constructor(imp: Implementor) {
16+
super(imp);
17+
}
2218

23-
class RefinedAbstractionB extends Abstraction {
24-
constructor(imp: Implementor) {
25-
super(imp);
19+
public callIt(s: String): void {
20+
console.log("This is RefinedAbstractionA");
21+
this.implementor.callee(s);
22+
}
2623
}
2724

28-
public callIt(s: String): void {
29-
console.log("This is RefinedAbstractionB");
30-
this.implementor.callee(s);
25+
export class RefinedAbstractionB extends Abstraction {
26+
constructor(imp: Implementor) {
27+
super(imp);
28+
}
29+
30+
public callIt(s: String): void {
31+
console.log("This is RefinedAbstractionB");
32+
this.implementor.callee(s);
33+
}
3134
}
32-
}
3335

34-
interface Implementor {
35-
callee(s: any): void;
36-
}
36+
export interface Implementor {
37+
callee(s: any): void;
38+
}
3739

38-
class ConcreteImplementorA implements Implementor {
39-
public callee(s: any) : void {
40-
console.log("`callee` of ConcreteImplementorA is being called.");
41-
console.log(s);
40+
export class ConcreteImplementorA implements Implementor {
41+
public callee(s: any) : void {
42+
console.log("`callee` of ConcreteImplementorA is being called.");
43+
console.log(s);
44+
}
4245
}
43-
}
4446

45-
class ConcreteImplementorB implements Implementor {
46-
public callee(s: any) : void {
47-
console.log("`callee` of ConcreteImplementorB is being called.");
48-
console.log(s);
47+
export class ConcreteImplementorB implements Implementor {
48+
public callee(s: any) : void {
49+
console.log("`callee` of ConcreteImplementorB is being called.");
50+
console.log(s);
51+
}
4952
}
5053
}
5154

5255
(function main() {
53-
var abstractionA: Abstraction = new RefinedAbstractionA(new ConcreteImplementorA());
54-
var abstractionB: Abstraction = new RefinedAbstractionB(new ConcreteImplementorB());
56+
var abstractionA: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionA(new BridgePattern.ConcreteImplementorA());
57+
var abstractionB: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionB(new BridgePattern.ConcreteImplementorB());
5558
abstractionA.callIt("abstractionA");
5659
abstractionB.callIt("abstractionB");
5760
}());

builder/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Builder {
1+
namespace BuilderPattern {
22
export class UserBuilder {
33
private name: string;
44
private age: number;
@@ -69,7 +69,7 @@ module Builder {
6969
}
7070

7171
(function main() {
72-
var u: Builder.User = new Builder.UserBuilder("Jancsi")
72+
var u: BuilderPattern.User = new BuilderPattern.UserBuilder("Jancsi")
7373
.setAge(12)
7474
.setPhone("0123456789")
7575
.setAddress("asdf")

0 commit comments

Comments
 (0)