diff --git a/README.md b/README.md index d8c34f4..858a840 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,12 @@ The patterns can be structured in roughly three different categories. Please cli ## [Structural](Structural) - ## [Behavioral](Behavioral) * [ChainOfResponsibilities](/src/Behavioral/ChainOfResponsibility/index.md) * [Command](/src/Behavioral/Command/index.md) +* [Interpreter](/src/Behavioral/Interpreter/index.md) +* [Iterator](/src/Behavioral/Iterator/index.md) ## Contribute diff --git a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts index 39f0d24..d530d53 100644 --- a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts @@ -7,7 +7,7 @@ export class DirectorPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 40; public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= DirectorPurchasePower.ALLOWABLE) { + if (request.getAmount() <= DirectorPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } else if (this.successor !== null) { this.successor.processRequest(request); diff --git a/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts index 76b9946..c0da80c 100644 --- a/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts @@ -6,7 +6,7 @@ export class ManagerPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 20; public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= ManagerPurchasePower.ALLOWABLE) { + if (request.getAmount() <= ManagerPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } else if (this.successor !== null) { this.successor.processRequest(request); diff --git a/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts index f58a7e4..ec2cb31 100644 --- a/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts @@ -6,7 +6,7 @@ export class PresidentPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 60; public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= PresidentPurchasePower.ALLOWABLE) { + if (request.getAmount() <= PresidentPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } } diff --git a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts index 95514c9..f58afeb 100644 --- a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts +++ b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts @@ -4,7 +4,8 @@ export class PurchaseRequest { protected approvedBy: AbstractPurchasePower = null; - public constructor(protected amount: number) {} + public constructor(protected amount: number) { + } public getAmount(): number { return this.amount; diff --git a/src/Behavioral/Command/CommandFactory.ts b/src/Behavioral/Command/CommandFactory.ts index c6459b1..a878127 100644 --- a/src/Behavioral/Command/CommandFactory.ts +++ b/src/Behavioral/Command/CommandFactory.ts @@ -19,7 +19,7 @@ export class CommandFactory { } public getCommand(name: string): CommandInterface { - return this.commands.find(function(command: CommandInterface) { + return this.commands.find(function (command: CommandInterface) { return command.getName() === name; }); } @@ -27,7 +27,7 @@ export class CommandFactory { public execute(name: string): void { var command = this.getCommand(name); - if(command === undefined) { + if (command === undefined) { throw new Error('Command %name% not found'.replace('%name%', name)); } @@ -35,7 +35,7 @@ export class CommandFactory { } public listCommands(): Array { - return this.commands.map(function(command: CommandInterface) { + return this.commands.map(function (command: CommandInterface) { return command.getName(); }); } diff --git a/src/Behavioral/Interpreter/Evaluator.ts b/src/Behavioral/Interpreter/Evaluator.ts index 70bf27a..458f2bd 100644 --- a/src/Behavioral/Interpreter/Evaluator.ts +++ b/src/Behavioral/Interpreter/Evaluator.ts @@ -11,8 +11,8 @@ export class Evaluator implements ExpressionInterface { var expressionStack: Array = new Array(); - expression.split(' ').forEach(function(token: string){ - if(token === '+') { + expression.split(' ').forEach(function (token: string) { + if (token === '+') { var subExpression: ExpressionInterface = new Plus(expressionStack.pop(), expressionStack.pop()); expressionStack.push(subExpression); } else if (token === '-') { diff --git a/src/Behavioral/Interpreter/Minus.ts b/src/Behavioral/Interpreter/Minus.ts index 706626a..dfd6b13 100644 --- a/src/Behavioral/Interpreter/Minus.ts +++ b/src/Behavioral/Interpreter/Minus.ts @@ -2,7 +2,8 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Minus implements ExpressionInterface { - public constructor(private leftOperand: ExpressionInterface, private rightOperand: ExpressionInterface) {} + public constructor(private leftOperand: ExpressionInterface, private rightOperand: ExpressionInterface) { + } public interpret(variables: Map): number { return this.leftOperand.interpret(variables) - this.rightOperand.interpret(variables); diff --git a/src/Behavioral/Interpreter/Number.ts b/src/Behavioral/Interpreter/Number.ts index 9626466..d67ff52 100644 --- a/src/Behavioral/Interpreter/Number.ts +++ b/src/Behavioral/Interpreter/Number.ts @@ -2,7 +2,8 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Number implements ExpressionInterface { - public constructor(private number: number) {} + public constructor(private number: number) { + } public interpret(variables: Map): number { return this.number; diff --git a/src/Behavioral/Interpreter/Plus.ts b/src/Behavioral/Interpreter/Plus.ts index f654930..a820151 100644 --- a/src/Behavioral/Interpreter/Plus.ts +++ b/src/Behavioral/Interpreter/Plus.ts @@ -2,7 +2,8 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Plus implements ExpressionInterface { - public constructor(private leftOperand: ExpressionInterface, private rightOperand: ExpressionInterface) {} + public constructor(private leftOperand: ExpressionInterface, private rightOperand: ExpressionInterface) { + } public interpret(variables: Map): number { return this.leftOperand.interpret(variables) + this.rightOperand.interpret(variables); diff --git a/src/Behavioral/Interpreter/Variable.ts b/src/Behavioral/Interpreter/Variable.ts index 7fce9fc..d21da20 100644 --- a/src/Behavioral/Interpreter/Variable.ts +++ b/src/Behavioral/Interpreter/Variable.ts @@ -2,7 +2,8 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Variable implements ExpressionInterface { - public constructor(private name: string) {} + public constructor(private name: string) { + } public interpret(variables: Map): number { return null === variables.get(this.name) ? 0 : variables.get(this.name).interpret(variables); diff --git a/src/Behavioral/Iterator/ContainerInterface.ts b/src/Behavioral/Iterator/ContainerInterface.ts new file mode 100755 index 0000000..3f7f6ed --- /dev/null +++ b/src/Behavioral/Iterator/ContainerInterface.ts @@ -0,0 +1,5 @@ +import {Iterator} from './IteratorInterface'; + +export interface Container { + getIterator(): Iterator; +} diff --git a/src/Behavioral/Iterator/DemoIterator.ts b/src/Behavioral/Iterator/DemoIterator.ts new file mode 100755 index 0000000..a5824cf --- /dev/null +++ b/src/Behavioral/Iterator/DemoIterator.ts @@ -0,0 +1,30 @@ +import {Iterator} from './IteratorInterface'; + +export class DemoIterator implements Iterator { + private index: number; + private items: Array; + + constructor(items: Array) { + this.items = items; + this.index = 0; + } + + public currentItem() { + return this.items[this.index]; + } + + public hasNext() { + return this.index < this.items.length - 1; + } + + public next() { + let item: any = null; + + if (this.hasNext() === true) { + this.index = this.index + 1; + item = this.items[this.index]; + } + + return item; + } +} diff --git a/src/Behavioral/Iterator/DemoRepository.ts b/src/Behavioral/Iterator/DemoRepository.ts new file mode 100755 index 0000000..90d0c3c --- /dev/null +++ b/src/Behavioral/Iterator/DemoRepository.ts @@ -0,0 +1,14 @@ +import {Container} from './ContainerInterface'; +import {DemoIterator} from './DemoIterator'; + +export class DemoRepository implements Container { + private items: Array; + + public constructor(items: Array) { + this.items = items; + } + + public getIterator() { + return new DemoIterator(this.items); + } +} diff --git a/src/Behavioral/Iterator/IteratorInterface.ts b/src/Behavioral/Iterator/IteratorInterface.ts new file mode 100755 index 0000000..ecacfed --- /dev/null +++ b/src/Behavioral/Iterator/IteratorInterface.ts @@ -0,0 +1,5 @@ +export interface Iterator { + currentItem(): T; + hasNext(): boolean; + next(): T; +} diff --git a/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts new file mode 100755 index 0000000..fd78862 --- /dev/null +++ b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts @@ -0,0 +1,32 @@ +import { + describe, + it, + expect, +} from 'angular2/testing'; +import {DemoRepository} from './DemoRepository'; + +export function main() { + describe('Iterator', () => { + + let repository: DemoRepository = new DemoRepository( + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + ); + let iterator = repository.getIterator(); + + it('Should start on first', () => { + let result = iterator.currentItem(); + + expect(result).toEqual(1); + }); + + it('Should finish on last', () => { + while (iterator.hasNext() !== false) { + iterator.next(); + } + + let result = iterator.currentItem(); + + expect(result).toEqual(10); + }); + }); +} diff --git a/src/Behavioral/Iterator/index.md b/src/Behavioral/Iterator/index.md new file mode 100755 index 0000000..9885c34 --- /dev/null +++ b/src/Behavioral/Iterator/index.md @@ -0,0 +1,38 @@ +`Iterator` +========= + +Description: +------------ + +Iterator pattern is very commonly used design pattern in Java and .Net programming environment. This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation. + +Iterator pattern falls under behavioral pattern category. + +Iterator example +![Iterator Sample](http://www.tutorialspoint.com/design_pattern/images/iterator_pattern_uml_diagram.jpg) + +Implementation: +--------------- + +We're going to create a Iterator interface which narrates navigation method and a Container interface which retruns the iterator . Concrete classes implementing the Container interface will be responsible to implement Iterator interface and use it + +IteratorPatternDemo, our demo class will use NamesRepository, a concrete class implementation to print a Names stored as a collection in NamesRepository. + +Files: +------ + +[ContainerInterface](ContainerInterface.ts) + +[IteratorInterface](IteratorInterface.ts) + +[DemoIterator](DemoIterator.ts) + +[DemoRepository](DemoRepository.ts) + +[Demo](IteratorPatternDemo.spec.ts) + +## Running tests + +```bash +npm test +``` \ No newline at end of file