Skip to content

Commit 996f569

Browse files
committed
Add demos
1 parent bfef359 commit 996f569

File tree

42 files changed

+1388
-1158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1388
-1158
lines changed

README.md

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
# Design Patterns in TypeScript #
2-
3-
Here are the implementations of the following design patterns in TypeScript:
4-
5-
### Creational ###
6-
7-
* [Singleton](https://github.com/torokmark/design_patterns_in_typescript/tree/master/singleton)
8-
* [Abstract Factory](https://github.com/torokmark/design_patterns_in_typescript/tree/master/abstract_factory)
9-
* [Factory Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/factory_method)
10-
* [Builder](https://github.com/torokmark/design_patterns_in_typescript/tree/master/builder)
11-
* [Prototype](https://github.com/torokmark/design_patterns_in_typescript/tree/master/prototype)
12-
13-
14-
### Structural Patterns ###
15-
16-
* [Adapter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/adapter)
17-
* [Bridge](https://github.com/torokmark/design_patterns_in_typescript/tree/master/bridge)
18-
* [Composite](https://github.com/torokmark/design_patterns_in_typescript/tree/master/composite)
19-
* [Decorator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/decorator)
20-
* [Facade](https://github.com/torokmark/design_patterns_in_typescript/tree/master/facade)
21-
* [Flyweight](https://github.com/torokmark/design_patterns_in_typescript/tree/master/flyweight)
22-
* [Proxy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/proxy)
23-
24-
25-
### Behavioral Patterns ###
26-
27-
* [Chain of Responsibility](https://github.com/torokmark/design_patterns_in_typescript/tree/master/chain_of_responsibility)
28-
* [Command](https://github.com/torokmark/design_patterns_in_typescript/tree/master/command)
29-
* [Interpreter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/interpreter)
30-
* [Iterator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/iterator)
31-
* [Mediator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/mediator)
32-
* [Memento](https://github.com/torokmark/design_patterns_in_typescript/tree/master/memento)
33-
* [Observer](https://github.com/torokmark/design_patterns_in_typescript/tree/master/observer)
34-
* [State](https://github.com/torokmark/design_patterns_in_typescript/tree/master/state)
35-
* [Strategy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/strategy)
36-
* [Template Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/template_method)
37-
* [Visitor](https://github.com/torokmark/design_patterns_in_typescript/tree/master/visitor)
38-
39-
## Compile the project
40-
41-
```
42-
$ git clone https://github.com/torokmark/design_patterns_in_typescript.git
43-
$ cd design_patterns_in_typescript
44-
$ tsc
45-
```
46-
47-
There is a new `tsconfig.json` file in the root direcotry which is responsible for the compiler options.
48-
As it is set the default target is Ecmascript5 now.
49-
Any additional options come here.
50-
51-
To compile only one pattern, use the following command.
52-
53-
```
54-
$ cd design_patterns_in_typescript/visitor
55-
$ tsc --target ES5 visitor.ts
56-
```
57-
> Here if there is no `--outFile` option then the compiler generates the js counterpart into the current directory.
1+
# Design Patterns in TypeScript #
2+
3+
Here are the implementations of the following design patterns in TypeScript:
4+
5+
### Creational ###
6+
7+
* [Singleton](https://github.com/torokmark/design_patterns_in_typescript/tree/master/singleton)
8+
* [Abstract Factory](https://github.com/torokmark/design_patterns_in_typescript/tree/master/abstract_factory)
9+
* [Factory Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/factory_method)
10+
* [Builder](https://github.com/torokmark/design_patterns_in_typescript/tree/master/builder)
11+
* [Prototype](https://github.com/torokmark/design_patterns_in_typescript/tree/master/prototype)
12+
13+
14+
### Structural Patterns ###
15+
16+
* [Adapter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/adapter)
17+
* [Bridge](https://github.com/torokmark/design_patterns_in_typescript/tree/master/bridge)
18+
* [Composite](https://github.com/torokmark/design_patterns_in_typescript/tree/master/composite)
19+
* [Decorator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/decorator)
20+
* [Facade](https://github.com/torokmark/design_patterns_in_typescript/tree/master/facade)
21+
* [Flyweight](https://github.com/torokmark/design_patterns_in_typescript/tree/master/flyweight)
22+
* [Proxy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/proxy)
23+
24+
25+
### Behavioral Patterns ###
26+
27+
* [Chain of Responsibility](https://github.com/torokmark/design_patterns_in_typescript/tree/master/chain_of_responsibility)
28+
* [Command](https://github.com/torokmark/design_patterns_in_typescript/tree/master/command)
29+
* [Interpreter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/interpreter)
30+
* [Iterator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/iterator)
31+
* [Mediator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/mediator)
32+
* [Memento](https://github.com/torokmark/design_patterns_in_typescript/tree/master/memento)
33+
* [Observer](https://github.com/torokmark/design_patterns_in_typescript/tree/master/observer)
34+
* [State](https://github.com/torokmark/design_patterns_in_typescript/tree/master/state)
35+
* [Strategy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/strategy)
36+
* [Template Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/template_method)
37+
* [Visitor](https://github.com/torokmark/design_patterns_in_typescript/tree/master/visitor)
38+
39+
## Compile the project
40+
41+
```
42+
$ git clone https://github.com/torokmark/design_patterns_in_typescript.git
43+
$ cd design_patterns_in_typescript
44+
$ tsc
45+
```
46+
47+
There is a new `tsconfig.json` file in the root directory which is responsible for the compiler options.
48+
As it is set the default target is Ecmascript5 now.
49+
Any additional options come here.
50+
51+
To compile only one pattern, use the following command.
52+
53+
```
54+
$ cd design_patterns_in_typescript/visitor
55+
$ tsc --target ES5 visitor.ts
56+
```
57+
> Here if there is no `--outFile` option then the compiler generates the js counterpart into the current directory.

adapter/adapter.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
namespace AdapterPattern {
2-
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-
}
12-
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-
}
20-
}
21-
22-
(function main() {
23-
var adapter: AdapterPattern.Adapter = new AdapterPattern.Adapter();
24-
adapter.call();
25-
}());
1+
namespace AdapterPattern {
2+
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+
}
12+
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+
}
20+
}

adapter/demo.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="adapter.ts" />
2+
namespace AdapterPattern {
3+
export namespace Demo {
4+
5+
export function show() : void {
6+
var adapter: AdapterPattern.Adapter = new AdapterPattern.Adapter();
7+
adapter.call();
8+
}
9+
}
10+
}

bridge/bridge.ts

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,53 @@
1-
namespace BridgePattern {
2-
3-
export class Abstraction {
4-
implementor: Implementor;
5-
constructor(imp: Implementor) {
6-
this.implementor = imp;
7-
}
8-
9-
public callIt(s: String): void {
10-
throw new Error("This method is abstract!");
11-
}
12-
}
13-
14-
export class RefinedAbstractionA extends Abstraction {
15-
constructor(imp: Implementor) {
16-
super(imp);
17-
}
18-
19-
public callIt(s: String): void {
20-
console.log("This is RefinedAbstractionA");
21-
this.implementor.callee(s);
22-
}
23-
}
24-
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-
}
34-
}
35-
36-
export interface Implementor {
37-
callee(s: any): void;
38-
}
39-
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-
}
45-
}
46-
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-
}
52-
}
53-
}
54-
55-
(function main() {
56-
var abstractionA: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionA(new BridgePattern.ConcreteImplementorA());
57-
var abstractionB: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionB(new BridgePattern.ConcreteImplementorB());
58-
abstractionA.callIt("abstractionA");
59-
abstractionB.callIt("abstractionB");
60-
}());
1+
namespace BridgePattern {
2+
3+
export class Abstraction {
4+
implementor: Implementor;
5+
constructor(imp: Implementor) {
6+
this.implementor = imp;
7+
}
8+
9+
public callIt(s: String): void {
10+
throw new Error("This method is abstract!");
11+
}
12+
}
13+
14+
export class RefinedAbstractionA extends Abstraction {
15+
constructor(imp: Implementor) {
16+
super(imp);
17+
}
18+
19+
public callIt(s: String): void {
20+
console.log("This is RefinedAbstractionA");
21+
this.implementor.callee(s);
22+
}
23+
}
24+
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+
}
34+
}
35+
36+
export interface Implementor {
37+
callee(s: any): void;
38+
}
39+
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+
}
45+
}
46+
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+
}
52+
}
53+
}

bridge/demo.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="bridge.ts" />
2+
namespace BridgePattern {
3+
export namespace Demo {
4+
5+
export function show() : void {
6+
var abstractionA: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionA(new BridgePattern.ConcreteImplementorA());
7+
var abstractionB: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionB(new BridgePattern.ConcreteImplementorB());
8+
9+
abstractionA.callIt("abstractionA");
10+
abstractionB.callIt("abstractionB");
11+
}
12+
}
13+
}
14+

0 commit comments

Comments
 (0)