Skip to content

Commit

Permalink
add iterator docs
Browse files Browse the repository at this point in the history
fixed identation to 2
add link on main doc page
  • Loading branch information
DiegoYungh committed Apr 14, 2016
1 parent 0205d88 commit a3df51e
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 105 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
8 changes: 4 additions & 4 deletions src/Behavioral/ChainOfResponsibility/AbstractPurchasePower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions src/Behavioral/ChainOfResponsibility/ChainPatternDemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Behavioral/ChainOfResponsibility/DirectorPurchasePower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Behavioral/ChainOfResponsibility/ManagerPurchasePower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/Behavioral/ChainOfResponsibility/PurchaseRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 9 additions & 9 deletions src/Behavioral/Command/CommandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import {LightOffCommand} from './LightOffCommand';

export class CommandFactory {

private commands: Array<CommandInterface> = new Array();
private commands:Array<CommandInterface> = new Array();

static init(): CommandFactory {
static init():CommandFactory {
var cf = new CommandFactory();
cf.addCommand(new LightOnCommand());
cf.addCommand(new LightOffCommand());

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<string> {
return this.commands.map(function(command: CommandInterface) {
public listCommands():Array<string> {
return this.commands.map(function (command:CommandInterface) {
return command.getName();
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/Behavioral/Command/CommandInterface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface CommandInterface {

getName(): string;
getName():string;

apply(): void;
apply():void;

isExecuted(): boolean;
isExecuted():boolean;
}
2 changes: 1 addition & 1 deletion src/Behavioral/Command/CommandPatternDemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 4 additions & 4 deletions src/Behavioral/Command/LightOffCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
8 changes: 4 additions & 4 deletions src/Behavioral/Command/LightOnCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
20 changes: 10 additions & 10 deletions src/Behavioral/Interpreter/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExpressionInterface> = new Array();
var expressionStack:Array<ExpressionInterface> = 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));
Expand All @@ -29,7 +29,7 @@ export class Evaluator implements ExpressionInterface {
this.syntaxTree = expressionStack.pop();
}

public interpret(context: Map<string, ExpressionInterface>): number {
public interpret(context:Map<string, ExpressionInterface>):number {
return this.syntaxTree.interpret(context);
}
}
2 changes: 1 addition & 1 deletion src/Behavioral/Interpreter/ExpressionInterface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface ExpressionInterface {
interpret(variables: Map<string, ExpressionInterface>): number;
interpret(variables:Map<string, ExpressionInterface>):number;
}
6 changes: 3 additions & 3 deletions src/Behavioral/Interpreter/InterpreterPatternDemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Num> = new Map<string, Num>();
let expression:string = 'w x z - +';
let evaluator:ExpressionInterface = new Evaluator(expression);
let variables:Map<string, Num> = new Map<string, Num>();
variables.set('w', new Num(52));
variables.set('x', new Num(25));
variables.set('z', new Num(25));
Expand Down
5 changes: 3 additions & 2 deletions src/Behavioral/Interpreter/Minus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
public interpret(variables:Map<string, ExpressionInterface>):number {
return this.leftOperand.interpret(variables) - this.rightOperand.interpret(variables);
}
}
5 changes: 3 additions & 2 deletions src/Behavioral/Interpreter/Number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
public interpret(variables:Map<string, ExpressionInterface>):number {
return this.number;
}
}
5 changes: 3 additions & 2 deletions src/Behavioral/Interpreter/Plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
public interpret(variables:Map<string, ExpressionInterface>):number {
return this.leftOperand.interpret(variables) + this.rightOperand.interpret(variables);
}
}
5 changes: 3 additions & 2 deletions src/Behavioral/Interpreter/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExpressionInterface>): number {
public interpret(variables:Map<string, ExpressionInterface>):number {
return null === variables.get(this.name) ? 0 : variables.get(this.name).interpret(variables);
}
}
40 changes: 20 additions & 20 deletions src/Behavioral/Iterator/DemoIterator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import {Iterator} from './IteratorInterface';

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

constructor (items: Array<T>) {
this.items = items;
this.index = 0;
}
constructor(items:Array<T>) {
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;
}
}
6 changes: 3 additions & 3 deletions src/Behavioral/Iterator/IteratorInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Iterator<T> {
current_item(): T;
hasNext(): boolean;
next(): T;
currentItem():T;
hasNext():boolean;
next():T;
}
Loading

0 comments on commit a3df51e

Please sign in to comment.