forked from torokmark/design_patterns_in_typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
1,388 additions
and
1,158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
# Design Patterns in TypeScript # | ||
|
||
Here are the implementations of the following design patterns in TypeScript: | ||
|
||
### Creational ### | ||
|
||
* [Singleton](https://github.com/torokmark/design_patterns_in_typescript/tree/master/singleton) | ||
* [Abstract Factory](https://github.com/torokmark/design_patterns_in_typescript/tree/master/abstract_factory) | ||
* [Factory Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/factory_method) | ||
* [Builder](https://github.com/torokmark/design_patterns_in_typescript/tree/master/builder) | ||
* [Prototype](https://github.com/torokmark/design_patterns_in_typescript/tree/master/prototype) | ||
|
||
|
||
### Structural Patterns ### | ||
|
||
* [Adapter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/adapter) | ||
* [Bridge](https://github.com/torokmark/design_patterns_in_typescript/tree/master/bridge) | ||
* [Composite](https://github.com/torokmark/design_patterns_in_typescript/tree/master/composite) | ||
* [Decorator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/decorator) | ||
* [Facade](https://github.com/torokmark/design_patterns_in_typescript/tree/master/facade) | ||
* [Flyweight](https://github.com/torokmark/design_patterns_in_typescript/tree/master/flyweight) | ||
* [Proxy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/proxy) | ||
|
||
|
||
### Behavioral Patterns ### | ||
|
||
* [Chain of Responsibility](https://github.com/torokmark/design_patterns_in_typescript/tree/master/chain_of_responsibility) | ||
* [Command](https://github.com/torokmark/design_patterns_in_typescript/tree/master/command) | ||
* [Interpreter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/interpreter) | ||
* [Iterator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/iterator) | ||
* [Mediator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/mediator) | ||
* [Memento](https://github.com/torokmark/design_patterns_in_typescript/tree/master/memento) | ||
* [Observer](https://github.com/torokmark/design_patterns_in_typescript/tree/master/observer) | ||
* [State](https://github.com/torokmark/design_patterns_in_typescript/tree/master/state) | ||
* [Strategy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/strategy) | ||
* [Template Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/template_method) | ||
* [Visitor](https://github.com/torokmark/design_patterns_in_typescript/tree/master/visitor) | ||
|
||
## Compile the project | ||
|
||
``` | ||
$ git clone https://github.com/torokmark/design_patterns_in_typescript.git | ||
$ cd design_patterns_in_typescript | ||
$ tsc | ||
``` | ||
|
||
There is a new `tsconfig.json` file in the root direcotry which is responsible for the compiler options. | ||
As it is set the default target is Ecmascript5 now. | ||
Any additional options come here. | ||
|
||
To compile only one pattern, use the following command. | ||
|
||
``` | ||
$ cd design_patterns_in_typescript/visitor | ||
$ tsc --target ES5 visitor.ts | ||
``` | ||
> Here if there is no `--outFile` option then the compiler generates the js counterpart into the current directory. | ||
# Design Patterns in TypeScript # | ||
|
||
Here are the implementations of the following design patterns in TypeScript: | ||
|
||
### Creational ### | ||
|
||
* [Singleton](https://github.com/torokmark/design_patterns_in_typescript/tree/master/singleton) | ||
* [Abstract Factory](https://github.com/torokmark/design_patterns_in_typescript/tree/master/abstract_factory) | ||
* [Factory Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/factory_method) | ||
* [Builder](https://github.com/torokmark/design_patterns_in_typescript/tree/master/builder) | ||
* [Prototype](https://github.com/torokmark/design_patterns_in_typescript/tree/master/prototype) | ||
|
||
|
||
### Structural Patterns ### | ||
|
||
* [Adapter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/adapter) | ||
* [Bridge](https://github.com/torokmark/design_patterns_in_typescript/tree/master/bridge) | ||
* [Composite](https://github.com/torokmark/design_patterns_in_typescript/tree/master/composite) | ||
* [Decorator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/decorator) | ||
* [Facade](https://github.com/torokmark/design_patterns_in_typescript/tree/master/facade) | ||
* [Flyweight](https://github.com/torokmark/design_patterns_in_typescript/tree/master/flyweight) | ||
* [Proxy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/proxy) | ||
|
||
|
||
### Behavioral Patterns ### | ||
|
||
* [Chain of Responsibility](https://github.com/torokmark/design_patterns_in_typescript/tree/master/chain_of_responsibility) | ||
* [Command](https://github.com/torokmark/design_patterns_in_typescript/tree/master/command) | ||
* [Interpreter](https://github.com/torokmark/design_patterns_in_typescript/tree/master/interpreter) | ||
* [Iterator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/iterator) | ||
* [Mediator](https://github.com/torokmark/design_patterns_in_typescript/tree/master/mediator) | ||
* [Memento](https://github.com/torokmark/design_patterns_in_typescript/tree/master/memento) | ||
* [Observer](https://github.com/torokmark/design_patterns_in_typescript/tree/master/observer) | ||
* [State](https://github.com/torokmark/design_patterns_in_typescript/tree/master/state) | ||
* [Strategy](https://github.com/torokmark/design_patterns_in_typescript/tree/master/strategy) | ||
* [Template Method](https://github.com/torokmark/design_patterns_in_typescript/tree/master/template_method) | ||
* [Visitor](https://github.com/torokmark/design_patterns_in_typescript/tree/master/visitor) | ||
|
||
## Compile the project | ||
|
||
``` | ||
$ git clone https://github.com/torokmark/design_patterns_in_typescript.git | ||
$ cd design_patterns_in_typescript | ||
$ tsc | ||
``` | ||
|
||
There is a new `tsconfig.json` file in the root directory which is responsible for the compiler options. | ||
As it is set the default target is Ecmascript5 now. | ||
Any additional options come here. | ||
|
||
To compile only one pattern, use the following command. | ||
|
||
``` | ||
$ cd design_patterns_in_typescript/visitor | ||
$ tsc --target ES5 visitor.ts | ||
``` | ||
> Here if there is no `--outFile` option then the compiler generates the js counterpart into the current directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,20 @@ | ||
namespace AdapterPattern { | ||
|
||
export class Adaptee { | ||
public method(): void { | ||
console.log("`method` of Adaptee is being called"); | ||
} | ||
} | ||
|
||
export interface Target { | ||
call(): void; | ||
} | ||
|
||
export class Adapter implements Target { | ||
public call(): void { | ||
console.log("Adapter's `call` method is being called"); | ||
var adaptee: Adaptee = new Adaptee(); | ||
adaptee.method(); | ||
} | ||
} | ||
} | ||
|
||
(function main() { | ||
var adapter: AdapterPattern.Adapter = new AdapterPattern.Adapter(); | ||
adapter.call(); | ||
}()); | ||
namespace AdapterPattern { | ||
|
||
export class Adaptee { | ||
public method(): void { | ||
console.log("`method` of Adaptee is being called"); | ||
} | ||
} | ||
|
||
export interface Target { | ||
call(): void; | ||
} | ||
|
||
export class Adapter implements Target { | ||
public call(): void { | ||
console.log("Adapter's `call` method is being called"); | ||
var adaptee: Adaptee = new Adaptee(); | ||
adaptee.method(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference path="adapter.ts" /> | ||
namespace AdapterPattern { | ||
export namespace Demo { | ||
|
||
export function show() : void { | ||
var adapter: AdapterPattern.Adapter = new AdapterPattern.Adapter(); | ||
adapter.call(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,53 @@ | ||
namespace BridgePattern { | ||
|
||
export class Abstraction { | ||
implementor: Implementor; | ||
constructor(imp: Implementor) { | ||
this.implementor = imp; | ||
} | ||
|
||
public callIt(s: String): void { | ||
throw new Error("This method is abstract!"); | ||
} | ||
} | ||
|
||
export class RefinedAbstractionA extends Abstraction { | ||
constructor(imp: Implementor) { | ||
super(imp); | ||
} | ||
|
||
public callIt(s: String): void { | ||
console.log("This is RefinedAbstractionA"); | ||
this.implementor.callee(s); | ||
} | ||
} | ||
|
||
export class RefinedAbstractionB extends Abstraction { | ||
constructor(imp: Implementor) { | ||
super(imp); | ||
} | ||
|
||
public callIt(s: String): void { | ||
console.log("This is RefinedAbstractionB"); | ||
this.implementor.callee(s); | ||
} | ||
} | ||
|
||
export interface Implementor { | ||
callee(s: any): void; | ||
} | ||
|
||
export class ConcreteImplementorA implements Implementor { | ||
public callee(s: any) : void { | ||
console.log("`callee` of ConcreteImplementorA is being called."); | ||
console.log(s); | ||
} | ||
} | ||
|
||
export class ConcreteImplementorB implements Implementor { | ||
public callee(s: any) : void { | ||
console.log("`callee` of ConcreteImplementorB is being called."); | ||
console.log(s); | ||
} | ||
} | ||
} | ||
|
||
(function main() { | ||
var abstractionA: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionA(new BridgePattern.ConcreteImplementorA()); | ||
var abstractionB: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionB(new BridgePattern.ConcreteImplementorB()); | ||
abstractionA.callIt("abstractionA"); | ||
abstractionB.callIt("abstractionB"); | ||
}()); | ||
namespace BridgePattern { | ||
|
||
export class Abstraction { | ||
implementor: Implementor; | ||
constructor(imp: Implementor) { | ||
this.implementor = imp; | ||
} | ||
|
||
public callIt(s: String): void { | ||
throw new Error("This method is abstract!"); | ||
} | ||
} | ||
|
||
export class RefinedAbstractionA extends Abstraction { | ||
constructor(imp: Implementor) { | ||
super(imp); | ||
} | ||
|
||
public callIt(s: String): void { | ||
console.log("This is RefinedAbstractionA"); | ||
this.implementor.callee(s); | ||
} | ||
} | ||
|
||
export class RefinedAbstractionB extends Abstraction { | ||
constructor(imp: Implementor) { | ||
super(imp); | ||
} | ||
|
||
public callIt(s: String): void { | ||
console.log("This is RefinedAbstractionB"); | ||
this.implementor.callee(s); | ||
} | ||
} | ||
|
||
export interface Implementor { | ||
callee(s: any): void; | ||
} | ||
|
||
export class ConcreteImplementorA implements Implementor { | ||
public callee(s: any) : void { | ||
console.log("`callee` of ConcreteImplementorA is being called."); | ||
console.log(s); | ||
} | ||
} | ||
|
||
export class ConcreteImplementorB implements Implementor { | ||
public callee(s: any) : void { | ||
console.log("`callee` of ConcreteImplementorB is being called."); | ||
console.log(s); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path="bridge.ts" /> | ||
namespace BridgePattern { | ||
export namespace Demo { | ||
|
||
export function show() : void { | ||
var abstractionA: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionA(new BridgePattern.ConcreteImplementorA()); | ||
var abstractionB: BridgePattern.Abstraction = new BridgePattern.RefinedAbstractionB(new BridgePattern.ConcreteImplementorB()); | ||
|
||
abstractionA.callIt("abstractionA"); | ||
abstractionB.callIt("abstractionB"); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.