Skip to content

Commit

Permalink
Add demos
Browse files Browse the repository at this point in the history
  • Loading branch information
torokmark committed Oct 22, 2016
1 parent bfef359 commit 996f569
Show file tree
Hide file tree
Showing 42 changed files with 1,388 additions and 1,158 deletions.
114 changes: 57 additions & 57 deletions README.md
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.
45 changes: 20 additions & 25 deletions adapter/adapter.ts
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();
}
}
}
10 changes: 10 additions & 0 deletions adapter/demo.ts
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();
}
}
}
113 changes: 53 additions & 60 deletions bridge/bridge.ts
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);
}
}
}
14 changes: 14 additions & 0 deletions bridge/demo.ts
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");
}
}
}

Loading

0 comments on commit 996f569

Please sign in to comment.