Skip to content

Commit

Permalink
Merge pull request #3 from DiegoYungh/master
Browse files Browse the repository at this point in the history
add Iterator Behavioral pattern
  • Loading branch information
zender committed Apr 15, 2016
2 parents 0c5400f + d13942a commit b6ec7e3
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Behavioral/Command/CommandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ 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;
});
}

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<string> {
return this.commands.map(function(command: CommandInterface) {
return this.commands.map(function (command: CommandInterface) {
return command.getName();
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Behavioral/Interpreter/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class Evaluator implements ExpressionInterface {

var expressionStack: Array<ExpressionInterface> = 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 === '-') {
Expand Down
3 changes: 2 additions & 1 deletion src/Behavioral/Interpreter/Minus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
return this.leftOperand.interpret(variables) - this.rightOperand.interpret(variables);
Expand Down
3 changes: 2 additions & 1 deletion src/Behavioral/Interpreter/Number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
return this.number;
Expand Down
3 changes: 2 additions & 1 deletion src/Behavioral/Interpreter/Plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
return this.leftOperand.interpret(variables) + this.rightOperand.interpret(variables);
Expand Down
3 changes: 2 additions & 1 deletion src/Behavioral/Interpreter/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
return null === variables.get(this.name) ? 0 : variables.get(this.name).interpret(variables);
Expand Down
5 changes: 5 additions & 0 deletions src/Behavioral/Iterator/ContainerInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Iterator} from './IteratorInterface';

export interface Container <T> {
getIterator(): Iterator<T>;
}
30 changes: 30 additions & 0 deletions src/Behavioral/Iterator/DemoIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Iterator} from './IteratorInterface';

export class DemoIterator<T> implements Iterator<T> {
private index: number;
private items: Array<T>;

constructor(items: Array<T>) {
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;
}
}
14 changes: 14 additions & 0 deletions src/Behavioral/Iterator/DemoRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Container} from './ContainerInterface';
import {DemoIterator} from './DemoIterator';

export class DemoRepository<T> implements Container<T> {
private items: Array<T>;

public constructor(items: Array<T>) {
this.items = items;
}

public getIterator() {
return new DemoIterator(this.items);
}
}
5 changes: 5 additions & 0 deletions src/Behavioral/Iterator/IteratorInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Iterator<T> {
currentItem(): T;
hasNext(): boolean;
next(): T;
}
32 changes: 32 additions & 0 deletions src/Behavioral/Iterator/IteratorPatternDemo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
describe,
it,
expect,
} from 'angular2/testing';
import {DemoRepository} from './DemoRepository';

export function main() {
describe('Iterator', () => {

let repository: DemoRepository<number> = new DemoRepository<number>(
[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);
});
});
}
38 changes: 38 additions & 0 deletions src/Behavioral/Iterator/index.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit b6ec7e3

Please sign in to comment.