From 0205d88b2e7025d256934780cd490ca479448494 Mon Sep 17 00:00:00 2001 From: Diego Yungh Date: Thu, 14 Apr 2016 11:46:57 -0300 Subject: [PATCH 1/3] add Iterator Behavioral pattern --- src/Behavioral/Iterator/ContainerInterface.ts | 5 +++ src/Behavioral/Iterator/DemoIterator.ts | 30 +++++++++++++++++ src/Behavioral/Iterator/DemoRepository.ts | 14 ++++++++ src/Behavioral/Iterator/IteratorInterface.ts | 5 +++ .../Iterator/IteratorPatternDemo.spec.ts | 32 +++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100755 src/Behavioral/Iterator/ContainerInterface.ts create mode 100755 src/Behavioral/Iterator/DemoIterator.ts create mode 100755 src/Behavioral/Iterator/DemoRepository.ts create mode 100755 src/Behavioral/Iterator/IteratorInterface.ts create mode 100755 src/Behavioral/Iterator/IteratorPatternDemo.spec.ts diff --git a/src/Behavioral/Iterator/ContainerInterface.ts b/src/Behavioral/Iterator/ContainerInterface.ts new file mode 100755 index 0000000..6c2541d --- /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..6d045e7 --- /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 current_item () { + 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..882366b --- /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..15d7f8b --- /dev/null +++ b/src/Behavioral/Iterator/IteratorInterface.ts @@ -0,0 +1,5 @@ +export interface Iterator { + current_item(): 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..d862488 --- /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.current_item(); + + expect(result).toEqual(1); + }); + + it('Should finish on last', () => { + while (iterator.hasNext() !== false) { + iterator.next(); + } + + let result = iterator.current_item(); + + expect(result).toEqual(10); + }); + }); +} From a3df51e585f0ee2b638d590f5a5f2136d7018e2b Mon Sep 17 00:00:00 2001 From: Diego Yungh Date: Thu, 14 Apr 2016 13:03:22 -0300 Subject: [PATCH 2/3] add iterator docs fixed identation to 2 add link on main doc page --- README.md | 3 +- .../AbstractPurchasePower.ts | 8 ++-- .../ChainPatternDemo.spec.ts | 8 ++-- .../DirectorPurchasePower.ts | 4 +- .../ManagerPurchasePower.ts | 4 +- .../PresidentPurchasePower.ts | 4 +- .../ChainOfResponsibility/PurchaseRequest.ts | 11 ++--- src/Behavioral/Command/CommandFactory.ts | 18 ++++----- src/Behavioral/Command/CommandInterface.ts | 6 +-- .../Command/CommandPatternDemo.spec.ts | 2 +- src/Behavioral/Command/LightOffCommand.ts | 8 ++-- src/Behavioral/Command/LightOnCommand.ts | 8 ++-- src/Behavioral/Interpreter/Evaluator.ts | 20 +++++----- .../Interpreter/ExpressionInterface.ts | 2 +- .../InterpreterPatternDemo.spec.ts | 6 +-- src/Behavioral/Interpreter/Minus.ts | 5 ++- src/Behavioral/Interpreter/Number.ts | 5 ++- src/Behavioral/Interpreter/Plus.ts | 5 ++- src/Behavioral/Interpreter/Variable.ts | 5 ++- src/Behavioral/Iterator/DemoIterator.ts | 40 +++++++++---------- src/Behavioral/Iterator/IteratorInterface.ts | 6 +-- .../Iterator/IteratorPatternDemo.spec.ts | 38 +++++++++--------- src/Behavioral/Iterator/index.md | 38 ++++++++++++++++++ 23 files changed, 149 insertions(+), 105 deletions(-) create mode 100755 src/Behavioral/Iterator/index.md 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/AbstractPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts index 56c4662..01c5eb9 100644 --- a/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts @@ -2,13 +2,13 @@ import {PurchaseRequest} from './PurchaseRequest'; export abstract class AbstractPurchasePower { - protected static BASE: number = 500.00; + protected static BASE:number = 500.00; - protected successor: AbstractPurchasePower = null; + protected successor:AbstractPurchasePower = null; - public setSuccessor(successor: AbstractPurchasePower): void { + public setSuccessor(successor:AbstractPurchasePower):void { this.successor = successor; } - public abstract processRequest(request: PurchaseRequest): void; + public abstract processRequest(request:PurchaseRequest):void; } diff --git a/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts b/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts index 410df3f..8bf068f 100755 --- a/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts +++ b/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts @@ -12,9 +12,9 @@ import {PurchaseRequest} from './PurchaseRequest'; function getInitiator() { - let manager: AbstractPurchasePower = new ManagerPurchasePower(); - let director: AbstractPurchasePower = new DirectorPurchasePower(); - let president: AbstractPurchasePower = new PresidentPurchasePower(); + let manager:AbstractPurchasePower = new ManagerPurchasePower(); + let director:AbstractPurchasePower = new DirectorPurchasePower(); + let president:AbstractPurchasePower = new PresidentPurchasePower(); manager.setSuccessor(director); director.setSuccessor(president); @@ -25,7 +25,7 @@ function getInitiator() { export function main() { describe('ChainOfResponsibility', () => { - let initiator: AbstractPurchasePower = getInitiator(); + let initiator:AbstractPurchasePower = getInitiator(); it('Should be approved by manager', () => { let request = new PurchaseRequest(8000); diff --git a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts index 39f0d24..13c636b 100644 --- a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts @@ -6,8 +6,8 @@ export class DirectorPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 40; - public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= DirectorPurchasePower.ALLOWABLE) { + public processRequest(request:PurchaseRequest):void { + 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..7f72406 100644 --- a/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts @@ -5,8 +5,8 @@ export class ManagerPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 20; - public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= ManagerPurchasePower.ALLOWABLE) { + public processRequest(request:PurchaseRequest):void { + 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..4130bef 100644 --- a/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts @@ -5,8 +5,8 @@ export class PresidentPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 60; - public processRequest(request: PurchaseRequest): void { - if(request.getAmount() <= PresidentPurchasePower.ALLOWABLE) { + public processRequest(request:PurchaseRequest):void { + if (request.getAmount() <= PresidentPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } } diff --git a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts index 95514c9..12d7ee4 100644 --- a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts +++ b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts @@ -2,19 +2,20 @@ import {AbstractPurchasePower} from './AbstractPurchasePower'; export class PurchaseRequest { - protected approvedBy: AbstractPurchasePower = null; + protected approvedBy:AbstractPurchasePower = null; - public constructor(protected amount: number) {} + public constructor(protected amount:number) { + } - public getAmount(): number { + public getAmount():number { return this.amount; } - public setApprovedBy(approvedBy: AbstractPurchasePower): void { + public setApprovedBy(approvedBy:AbstractPurchasePower):void { this.approvedBy = approvedBy; } - public getApprovedBy(): AbstractPurchasePower { + public getApprovedBy():AbstractPurchasePower { return this.approvedBy; } } diff --git a/src/Behavioral/Command/CommandFactory.ts b/src/Behavioral/Command/CommandFactory.ts index c6459b1..defd467 100644 --- a/src/Behavioral/Command/CommandFactory.ts +++ b/src/Behavioral/Command/CommandFactory.ts @@ -4,9 +4,9 @@ import {LightOffCommand} from './LightOffCommand'; export class CommandFactory { - private commands: Array = new Array(); + private commands:Array = new Array(); - static init(): CommandFactory { + static init():CommandFactory { var cf = new CommandFactory(); cf.addCommand(new LightOnCommand()); cf.addCommand(new LightOffCommand()); @@ -14,28 +14,28 @@ export class CommandFactory { return cf; } - public addCommand(command: CommandInterface): void { + public addCommand(command:CommandInterface):void { this.commands.push(command); } - public getCommand(name: string): CommandInterface { - return this.commands.find(function(command: CommandInterface) { + public getCommand(name:string):CommandInterface { + return this.commands.find(function (command:CommandInterface) { return command.getName() === name; }); } - public execute(name: string): void { + 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)); } command.apply(); } - public listCommands(): Array { - return this.commands.map(function(command: CommandInterface) { + public listCommands():Array { + return this.commands.map(function (command:CommandInterface) { return command.getName(); }); } diff --git a/src/Behavioral/Command/CommandInterface.ts b/src/Behavioral/Command/CommandInterface.ts index c22eadd..a58932c 100644 --- a/src/Behavioral/Command/CommandInterface.ts +++ b/src/Behavioral/Command/CommandInterface.ts @@ -1,8 +1,8 @@ export interface CommandInterface { - getName(): string; + getName():string; - apply(): void; + apply():void; - isExecuted(): boolean; + isExecuted():boolean; } diff --git a/src/Behavioral/Command/CommandPatternDemo.spec.ts b/src/Behavioral/Command/CommandPatternDemo.spec.ts index 8e53fe7..a8b01aa 100755 --- a/src/Behavioral/Command/CommandPatternDemo.spec.ts +++ b/src/Behavioral/Command/CommandPatternDemo.spec.ts @@ -8,7 +8,7 @@ import {CommandFactory} from './CommandFactory'; export function main() { describe('Command', () => { - let cf: CommandFactory = CommandFactory.init(); + let cf:CommandFactory = CommandFactory.init(); it('Should not find command.', () => { expect(function () { diff --git a/src/Behavioral/Command/LightOffCommand.ts b/src/Behavioral/Command/LightOffCommand.ts index 3647e94..d44d3d5 100644 --- a/src/Behavioral/Command/LightOffCommand.ts +++ b/src/Behavioral/Command/LightOffCommand.ts @@ -2,17 +2,17 @@ import {CommandInterface} from './CommandInterface'; export class LightOffCommand implements CommandInterface { - private executed: boolean = false; + private executed:boolean = false; - public apply(): void { + public apply():void { this.executed = true; } - public isExecuted(): boolean { + public isExecuted():boolean { return this.executed; } - public getName(): string { + public getName():string { return 'Light Off'; } } diff --git a/src/Behavioral/Command/LightOnCommand.ts b/src/Behavioral/Command/LightOnCommand.ts index 7af5853..8669e49 100644 --- a/src/Behavioral/Command/LightOnCommand.ts +++ b/src/Behavioral/Command/LightOnCommand.ts @@ -2,17 +2,17 @@ import {CommandInterface} from './CommandInterface'; export class LightOnCommand implements CommandInterface { - private executed: boolean = false; + private executed:boolean = false; - public apply(): void { + public apply():void { this.executed = true; } - public isExecuted(): boolean { + public isExecuted():boolean { return this.executed; } - public getName(): string { + public getName():string { return 'Light On'; } } diff --git a/src/Behavioral/Interpreter/Evaluator.ts b/src/Behavioral/Interpreter/Evaluator.ts index 70bf27a..155123f 100644 --- a/src/Behavioral/Interpreter/Evaluator.ts +++ b/src/Behavioral/Interpreter/Evaluator.ts @@ -5,21 +5,21 @@ import {Variable} from './Variable'; export class Evaluator implements ExpressionInterface { - private syntaxTree: ExpressionInterface; + private syntaxTree:ExpressionInterface; - public constructor(expression: string) { + public constructor(expression:string) { - var expressionStack: Array = new Array(); + var expressionStack:Array = new Array(); - expression.split(' ').forEach(function(token: string){ - if(token === '+') { - var subExpression: ExpressionInterface = new Plus(expressionStack.pop(), expressionStack.pop()); + expression.split(' ').forEach(function (token:string) { + if (token === '+') { + var subExpression:ExpressionInterface = new Plus(expressionStack.pop(), expressionStack.pop()); expressionStack.push(subExpression); } else if (token === '-') { - var right: ExpressionInterface = expressionStack.pop(); - var left: ExpressionInterface = expressionStack.pop(); + var right:ExpressionInterface = expressionStack.pop(); + var left:ExpressionInterface = expressionStack.pop(); - var subExpression: ExpressionInterface = new Minus(left, right); + var subExpression:ExpressionInterface = new Minus(left, right); expressionStack.push(subExpression); } else { expressionStack.push(new Variable(token)); @@ -29,7 +29,7 @@ export class Evaluator implements ExpressionInterface { this.syntaxTree = expressionStack.pop(); } - public interpret(context: Map): number { + public interpret(context:Map):number { return this.syntaxTree.interpret(context); } } diff --git a/src/Behavioral/Interpreter/ExpressionInterface.ts b/src/Behavioral/Interpreter/ExpressionInterface.ts index dba9aa6..9e700a5 100644 --- a/src/Behavioral/Interpreter/ExpressionInterface.ts +++ b/src/Behavioral/Interpreter/ExpressionInterface.ts @@ -1,3 +1,3 @@ export interface ExpressionInterface { - interpret(variables: Map): number; + interpret(variables:Map):number; } diff --git a/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts b/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts index 9ab1a0a..4bc7c9c 100755 --- a/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts +++ b/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts @@ -10,9 +10,9 @@ import {Number as Num} from './Number'; export function main() { describe('Interpreter', () => { - let expression: string = 'w x z - +'; - let evaluator: ExpressionInterface = new Evaluator(expression); - let variables: Map = new Map(); + let expression:string = 'w x z - +'; + let evaluator:ExpressionInterface = new Evaluator(expression); + let variables:Map = new Map(); variables.set('w', new Num(52)); variables.set('x', new Num(25)); variables.set('z', new Num(25)); diff --git a/src/Behavioral/Interpreter/Minus.ts b/src/Behavioral/Interpreter/Minus.ts index 706626a..2a8f552 100644 --- a/src/Behavioral/Interpreter/Minus.ts +++ b/src/Behavioral/Interpreter/Minus.ts @@ -2,9 +2,10 @@ 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 { + 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..011599c 100644 --- a/src/Behavioral/Interpreter/Number.ts +++ b/src/Behavioral/Interpreter/Number.ts @@ -2,9 +2,10 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Number implements ExpressionInterface { - public constructor(private number: number) {} + public constructor(private number:number) { + } - public interpret(variables: Map): 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..5d0559f 100644 --- a/src/Behavioral/Interpreter/Plus.ts +++ b/src/Behavioral/Interpreter/Plus.ts @@ -2,9 +2,10 @@ 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 { + 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..967d735 100644 --- a/src/Behavioral/Interpreter/Variable.ts +++ b/src/Behavioral/Interpreter/Variable.ts @@ -2,9 +2,10 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Variable implements ExpressionInterface { - public constructor(private name: string) {} + public constructor(private name:string) { + } - public interpret(variables: Map): number { + public interpret(variables:Map):number { return null === variables.get(this.name) ? 0 : variables.get(this.name).interpret(variables); } } diff --git a/src/Behavioral/Iterator/DemoIterator.ts b/src/Behavioral/Iterator/DemoIterator.ts index 6d045e7..c5877fb 100755 --- a/src/Behavioral/Iterator/DemoIterator.ts +++ b/src/Behavioral/Iterator/DemoIterator.ts @@ -1,30 +1,30 @@ import {Iterator} from './IteratorInterface'; export class DemoIterator implements Iterator { - private index: number; - private items: Array; + private index:number; + private items:Array; - constructor (items: Array) { - this.items = items; - this.index = 0; - } + constructor(items:Array) { + this.items = items; + this.index = 0; + } - public current_item () { - return this.items[this.index]; - } + public currentItem() { + return this.items[this.index]; + } - public hasNext () { - return this.index < this.items.length - 1; - } + public hasNext() { + return this.index < this.items.length - 1; + } - public next () { - let item: any = null; + public next() { + let item:any = null; - if(this.hasNext() === true) { - this.index = this.index + 1; - item = this.items[this.index]; - } - - return item; + if (this.hasNext() === true) { + this.index = this.index + 1; + item = this.items[this.index]; } + + return item; + } } diff --git a/src/Behavioral/Iterator/IteratorInterface.ts b/src/Behavioral/Iterator/IteratorInterface.ts index 15d7f8b..1b1ad46 100755 --- a/src/Behavioral/Iterator/IteratorInterface.ts +++ b/src/Behavioral/Iterator/IteratorInterface.ts @@ -1,5 +1,5 @@ export interface Iterator { - current_item(): T; - hasNext(): boolean; - next(): T; + currentItem():T; + hasNext():boolean; + next():T; } diff --git a/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts index d862488..94e00c3 100755 --- a/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts +++ b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts @@ -1,32 +1,32 @@ import { - describe, - it, - expect, + describe, + it, + expect, } from 'angular2/testing'; import {DemoRepository} from './DemoRepository'; export function main() { - describe('Iterator', () => { + describe('Iterator', () => { - let repository:DemoRepository = new DemoRepository( - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - ); - let iterator = repository.getIterator(); + 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.current_item(); + it('Should start on first', () => { + let result = iterator.currentItem(); - expect(result).toEqual(1); - }); + expect(result).toEqual(1); + }); - it('Should finish on last', () => { - while (iterator.hasNext() !== false) { - iterator.next(); - } + it('Should finish on last', () => { + while (iterator.hasNext() !== false) { + iterator.next(); + } - let result = iterator.current_item(); + let result = iterator.currentItem(); - expect(result).toEqual(10); - }); + 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 From d13942a02b119f17e433cd2e430125fa9f669c55 Mon Sep 17 00:00:00 2001 From: Diego Yungh Date: Thu, 14 Apr 2016 13:11:04 -0300 Subject: [PATCH 3/3] fix style issue with space after property name and before type definition --- .../AbstractPurchasePower.ts | 8 ++++---- .../ChainPatternDemo.spec.ts | 8 ++++---- .../DirectorPurchasePower.ts | 2 +- .../ManagerPurchasePower.ts | 2 +- .../PresidentPurchasePower.ts | 2 +- .../ChainOfResponsibility/PurchaseRequest.ts | 10 +++++----- src/Behavioral/Command/CommandFactory.ts | 16 ++++++++-------- src/Behavioral/Command/CommandInterface.ts | 6 +++--- .../Command/CommandPatternDemo.spec.ts | 2 +- src/Behavioral/Command/LightOffCommand.ts | 8 ++++---- src/Behavioral/Command/LightOnCommand.ts | 8 ++++---- src/Behavioral/Interpreter/Evaluator.ts | 18 +++++++++--------- .../Interpreter/ExpressionInterface.ts | 2 +- .../Interpreter/InterpreterPatternDemo.spec.ts | 6 +++--- src/Behavioral/Interpreter/Minus.ts | 4 ++-- src/Behavioral/Interpreter/Number.ts | 4 ++-- src/Behavioral/Interpreter/Plus.ts | 4 ++-- src/Behavioral/Interpreter/Variable.ts | 4 ++-- src/Behavioral/Iterator/ContainerInterface.ts | 4 ++-- src/Behavioral/Iterator/DemoIterator.ts | 8 ++++---- src/Behavioral/Iterator/DemoRepository.ts | 2 +- src/Behavioral/Iterator/IteratorInterface.ts | 6 +++--- .../Iterator/IteratorPatternDemo.spec.ts | 2 +- 23 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts index 01c5eb9..56c4662 100644 --- a/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts @@ -2,13 +2,13 @@ import {PurchaseRequest} from './PurchaseRequest'; export abstract class AbstractPurchasePower { - protected static BASE:number = 500.00; + protected static BASE: number = 500.00; - protected successor:AbstractPurchasePower = null; + protected successor: AbstractPurchasePower = null; - public setSuccessor(successor:AbstractPurchasePower):void { + public setSuccessor(successor: AbstractPurchasePower): void { this.successor = successor; } - public abstract processRequest(request:PurchaseRequest):void; + public abstract processRequest(request: PurchaseRequest): void; } diff --git a/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts b/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts index 8bf068f..410df3f 100755 --- a/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts +++ b/src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts @@ -12,9 +12,9 @@ import {PurchaseRequest} from './PurchaseRequest'; function getInitiator() { - let manager:AbstractPurchasePower = new ManagerPurchasePower(); - let director:AbstractPurchasePower = new DirectorPurchasePower(); - let president:AbstractPurchasePower = new PresidentPurchasePower(); + let manager: AbstractPurchasePower = new ManagerPurchasePower(); + let director: AbstractPurchasePower = new DirectorPurchasePower(); + let president: AbstractPurchasePower = new PresidentPurchasePower(); manager.setSuccessor(director); director.setSuccessor(president); @@ -25,7 +25,7 @@ function getInitiator() { export function main() { describe('ChainOfResponsibility', () => { - let initiator:AbstractPurchasePower = getInitiator(); + let initiator: AbstractPurchasePower = getInitiator(); it('Should be approved by manager', () => { let request = new PurchaseRequest(8000); diff --git a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts index 13c636b..d530d53 100644 --- a/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts @@ -6,7 +6,7 @@ export class DirectorPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 40; - public processRequest(request:PurchaseRequest):void { + public processRequest(request: PurchaseRequest): void { if (request.getAmount() <= DirectorPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } else if (this.successor !== null) { diff --git a/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts index 7f72406..c0da80c 100644 --- a/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts @@ -5,7 +5,7 @@ export class ManagerPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 20; - public processRequest(request:PurchaseRequest):void { + public processRequest(request: PurchaseRequest): void { if (request.getAmount() <= ManagerPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } else if (this.successor !== null) { diff --git a/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts b/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts index 4130bef..ec2cb31 100644 --- a/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts +++ b/src/Behavioral/ChainOfResponsibility/PresidentPurchasePower.ts @@ -5,7 +5,7 @@ export class PresidentPurchasePower extends AbstractPurchasePower { private static ALLOWABLE = AbstractPurchasePower.BASE * 60; - public processRequest(request:PurchaseRequest):void { + public processRequest(request: PurchaseRequest): void { if (request.getAmount() <= PresidentPurchasePower.ALLOWABLE) { request.setApprovedBy(this); } diff --git a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts index 12d7ee4..f58afeb 100644 --- a/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts +++ b/src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts @@ -2,20 +2,20 @@ import {AbstractPurchasePower} from './AbstractPurchasePower'; export class PurchaseRequest { - protected approvedBy:AbstractPurchasePower = null; + protected approvedBy: AbstractPurchasePower = null; - public constructor(protected amount:number) { + public constructor(protected amount: number) { } - public getAmount():number { + public getAmount(): number { return this.amount; } - public setApprovedBy(approvedBy:AbstractPurchasePower):void { + public setApprovedBy(approvedBy: AbstractPurchasePower): void { this.approvedBy = approvedBy; } - public getApprovedBy():AbstractPurchasePower { + public getApprovedBy(): AbstractPurchasePower { return this.approvedBy; } } diff --git a/src/Behavioral/Command/CommandFactory.ts b/src/Behavioral/Command/CommandFactory.ts index defd467..a878127 100644 --- a/src/Behavioral/Command/CommandFactory.ts +++ b/src/Behavioral/Command/CommandFactory.ts @@ -4,9 +4,9 @@ import {LightOffCommand} from './LightOffCommand'; export class CommandFactory { - private commands:Array = new Array(); + private commands: Array = new Array(); - static init():CommandFactory { + static init(): CommandFactory { var cf = new CommandFactory(); cf.addCommand(new LightOnCommand()); cf.addCommand(new LightOffCommand()); @@ -14,17 +14,17 @@ export class CommandFactory { return cf; } - public addCommand(command:CommandInterface):void { + public addCommand(command: CommandInterface): void { this.commands.push(command); } - public getCommand(name:string):CommandInterface { - return this.commands.find(function (command:CommandInterface) { + public getCommand(name: string): CommandInterface { + return this.commands.find(function (command: CommandInterface) { return command.getName() === name; }); } - public execute(name:string):void { + public execute(name: string): void { var command = this.getCommand(name); if (command === undefined) { @@ -34,8 +34,8 @@ export class CommandFactory { command.apply(); } - public listCommands():Array { - return this.commands.map(function (command:CommandInterface) { + public listCommands(): Array { + return this.commands.map(function (command: CommandInterface) { return command.getName(); }); } diff --git a/src/Behavioral/Command/CommandInterface.ts b/src/Behavioral/Command/CommandInterface.ts index a58932c..c22eadd 100644 --- a/src/Behavioral/Command/CommandInterface.ts +++ b/src/Behavioral/Command/CommandInterface.ts @@ -1,8 +1,8 @@ export interface CommandInterface { - getName():string; + getName(): string; - apply():void; + apply(): void; - isExecuted():boolean; + isExecuted(): boolean; } diff --git a/src/Behavioral/Command/CommandPatternDemo.spec.ts b/src/Behavioral/Command/CommandPatternDemo.spec.ts index a8b01aa..8e53fe7 100755 --- a/src/Behavioral/Command/CommandPatternDemo.spec.ts +++ b/src/Behavioral/Command/CommandPatternDemo.spec.ts @@ -8,7 +8,7 @@ import {CommandFactory} from './CommandFactory'; export function main() { describe('Command', () => { - let cf:CommandFactory = CommandFactory.init(); + let cf: CommandFactory = CommandFactory.init(); it('Should not find command.', () => { expect(function () { diff --git a/src/Behavioral/Command/LightOffCommand.ts b/src/Behavioral/Command/LightOffCommand.ts index d44d3d5..3647e94 100644 --- a/src/Behavioral/Command/LightOffCommand.ts +++ b/src/Behavioral/Command/LightOffCommand.ts @@ -2,17 +2,17 @@ import {CommandInterface} from './CommandInterface'; export class LightOffCommand implements CommandInterface { - private executed:boolean = false; + private executed: boolean = false; - public apply():void { + public apply(): void { this.executed = true; } - public isExecuted():boolean { + public isExecuted(): boolean { return this.executed; } - public getName():string { + public getName(): string { return 'Light Off'; } } diff --git a/src/Behavioral/Command/LightOnCommand.ts b/src/Behavioral/Command/LightOnCommand.ts index 8669e49..7af5853 100644 --- a/src/Behavioral/Command/LightOnCommand.ts +++ b/src/Behavioral/Command/LightOnCommand.ts @@ -2,17 +2,17 @@ import {CommandInterface} from './CommandInterface'; export class LightOnCommand implements CommandInterface { - private executed:boolean = false; + private executed: boolean = false; - public apply():void { + public apply(): void { this.executed = true; } - public isExecuted():boolean { + public isExecuted(): boolean { return this.executed; } - public getName():string { + public getName(): string { return 'Light On'; } } diff --git a/src/Behavioral/Interpreter/Evaluator.ts b/src/Behavioral/Interpreter/Evaluator.ts index 155123f..458f2bd 100644 --- a/src/Behavioral/Interpreter/Evaluator.ts +++ b/src/Behavioral/Interpreter/Evaluator.ts @@ -5,21 +5,21 @@ import {Variable} from './Variable'; export class Evaluator implements ExpressionInterface { - private syntaxTree:ExpressionInterface; + private syntaxTree: ExpressionInterface; - public constructor(expression:string) { + public constructor(expression: string) { - var expressionStack:Array = new Array(); + var expressionStack: Array = new Array(); - expression.split(' ').forEach(function (token:string) { + expression.split(' ').forEach(function (token: string) { if (token === '+') { - var subExpression:ExpressionInterface = new Plus(expressionStack.pop(), expressionStack.pop()); + var subExpression: ExpressionInterface = new Plus(expressionStack.pop(), expressionStack.pop()); expressionStack.push(subExpression); } else if (token === '-') { - var right:ExpressionInterface = expressionStack.pop(); - var left:ExpressionInterface = expressionStack.pop(); + var right: ExpressionInterface = expressionStack.pop(); + var left: ExpressionInterface = expressionStack.pop(); - var subExpression:ExpressionInterface = new Minus(left, right); + var subExpression: ExpressionInterface = new Minus(left, right); expressionStack.push(subExpression); } else { expressionStack.push(new Variable(token)); @@ -29,7 +29,7 @@ export class Evaluator implements ExpressionInterface { this.syntaxTree = expressionStack.pop(); } - public interpret(context:Map):number { + public interpret(context: Map): number { return this.syntaxTree.interpret(context); } } diff --git a/src/Behavioral/Interpreter/ExpressionInterface.ts b/src/Behavioral/Interpreter/ExpressionInterface.ts index 9e700a5..dba9aa6 100644 --- a/src/Behavioral/Interpreter/ExpressionInterface.ts +++ b/src/Behavioral/Interpreter/ExpressionInterface.ts @@ -1,3 +1,3 @@ export interface ExpressionInterface { - interpret(variables:Map):number; + interpret(variables: Map): number; } diff --git a/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts b/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts index 4bc7c9c..9ab1a0a 100755 --- a/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts +++ b/src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts @@ -10,9 +10,9 @@ import {Number as Num} from './Number'; export function main() { describe('Interpreter', () => { - let expression:string = 'w x z - +'; - let evaluator:ExpressionInterface = new Evaluator(expression); - let variables:Map = new Map(); + let expression: string = 'w x z - +'; + let evaluator: ExpressionInterface = new Evaluator(expression); + let variables: Map = new Map(); variables.set('w', new Num(52)); variables.set('x', new Num(25)); variables.set('z', new Num(25)); diff --git a/src/Behavioral/Interpreter/Minus.ts b/src/Behavioral/Interpreter/Minus.ts index 2a8f552..dfd6b13 100644 --- a/src/Behavioral/Interpreter/Minus.ts +++ b/src/Behavioral/Interpreter/Minus.ts @@ -2,10 +2,10 @@ 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 { + 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 011599c..d67ff52 100644 --- a/src/Behavioral/Interpreter/Number.ts +++ b/src/Behavioral/Interpreter/Number.ts @@ -2,10 +2,10 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Number implements ExpressionInterface { - public constructor(private number:number) { + public constructor(private number: number) { } - public interpret(variables:Map):number { + public interpret(variables: Map): number { return this.number; } } diff --git a/src/Behavioral/Interpreter/Plus.ts b/src/Behavioral/Interpreter/Plus.ts index 5d0559f..a820151 100644 --- a/src/Behavioral/Interpreter/Plus.ts +++ b/src/Behavioral/Interpreter/Plus.ts @@ -2,10 +2,10 @@ 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 { + 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 967d735..d21da20 100644 --- a/src/Behavioral/Interpreter/Variable.ts +++ b/src/Behavioral/Interpreter/Variable.ts @@ -2,10 +2,10 @@ import {ExpressionInterface} from './ExpressionInterface'; export class Variable implements ExpressionInterface { - public constructor(private name:string) { + public constructor(private name: string) { } - public interpret(variables:Map):number { + 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 index 6c2541d..3f7f6ed 100755 --- a/src/Behavioral/Iterator/ContainerInterface.ts +++ b/src/Behavioral/Iterator/ContainerInterface.ts @@ -1,5 +1,5 @@ import {Iterator} from './IteratorInterface'; -export interface Container { - getIterator(): Iterator; +export interface Container { + getIterator(): Iterator; } diff --git a/src/Behavioral/Iterator/DemoIterator.ts b/src/Behavioral/Iterator/DemoIterator.ts index c5877fb..a5824cf 100755 --- a/src/Behavioral/Iterator/DemoIterator.ts +++ b/src/Behavioral/Iterator/DemoIterator.ts @@ -1,10 +1,10 @@ import {Iterator} from './IteratorInterface'; export class DemoIterator implements Iterator { - private index:number; - private items:Array; + private index: number; + private items: Array; - constructor(items:Array) { + constructor(items: Array) { this.items = items; this.index = 0; } @@ -18,7 +18,7 @@ export class DemoIterator implements Iterator { } public next() { - let item:any = null; + let item: any = null; if (this.hasNext() === true) { this.index = this.index + 1; diff --git a/src/Behavioral/Iterator/DemoRepository.ts b/src/Behavioral/Iterator/DemoRepository.ts index 882366b..90d0c3c 100755 --- a/src/Behavioral/Iterator/DemoRepository.ts +++ b/src/Behavioral/Iterator/DemoRepository.ts @@ -4,7 +4,7 @@ import {DemoIterator} from './DemoIterator'; export class DemoRepository implements Container { private items: Array; - public constructor (items: Array) { + public constructor(items: Array) { this.items = items; } diff --git a/src/Behavioral/Iterator/IteratorInterface.ts b/src/Behavioral/Iterator/IteratorInterface.ts index 1b1ad46..ecacfed 100755 --- a/src/Behavioral/Iterator/IteratorInterface.ts +++ b/src/Behavioral/Iterator/IteratorInterface.ts @@ -1,5 +1,5 @@ export interface Iterator { - currentItem():T; - hasNext():boolean; - next():T; + currentItem(): T; + hasNext(): boolean; + next(): T; } diff --git a/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts index 94e00c3..fd78862 100755 --- a/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts +++ b/src/Behavioral/Iterator/IteratorPatternDemo.spec.ts @@ -8,7 +8,7 @@ import {DemoRepository} from './DemoRepository'; export function main() { describe('Iterator', () => { - let repository:DemoRepository = new DemoRepository( + let repository: DemoRepository = new DemoRepository( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ); let iterator = repository.getIterator();