Chain-of-responsibility pattern in TypeScript

Topics: General
Oct 6, 2012 at 4:28 PM

see http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern, second example.

things i noticed while reimpementing it in TypeScript:

private final double ALLOWABLE = 10 * base; //java

This doesn't work in TypeScript, the base property is not yet visible. I tried it with a constructor, but was not sure. Would it be right to call super() as the first statement in the contructor?

And 'protected' would really help ;-)

class PurchasePower {
    public base : number = 500;
    public ALLOWABLE: number = 0;
    public successor: PurchasePower;
 
    setSuccessor (successor: PurchasePower): void { this.successor = successor; }
    processRequest (request: PurchaseRequest): void {}
    log(message: string): void {
        document.writeln("<p>" + message + "</p>");
    }
}

class ManagerPPower extends PurchasePower {
    processRequest(request: PurchaseRequest): void {
        if (request.amount < this.ALLOWABLE) {
            this.log("Manager will approve $" + request.amount);
        } else if (this.successor != null) {
            this.successor.processRequest(request);
        }
    }
}

class DirectorPPower extends PurchasePower {
    processRequest(request: PurchaseRequest): void {
        this.ALLOWABLE = 20 * this.base;
        if (request.amount < this.ALLOWABLE) {
            this.log("Director will approve $" + request.amount);
        } else if (this.successor != null) {
            this.successor.processRequest(request);
        }
    }
}

class VicePresidentPPower extends PurchasePower {
    processRequest(request: PurchaseRequest): void {
        this.ALLOWABLE = 40 * this.base;
        if (request.amount < this.ALLOWABLE) {
            this.log("VicePresident will approve $" + request.amount);
        } else if (this.successor != null) {
            this.successor.processRequest(request);
        }
    }
}

class PresidentPPower extends PurchasePower {
    processRequest(request: PurchaseRequest): void {
        this.ALLOWABLE = 60 * this.base;
        if (request.amount < this.ALLOWABLE) {
            this.log("President will approve $" + request.amount);
        } else {
            this.log("Your request for $" + request.amount + " needs a board meeting!");
        }
    }
}

class PurchaseRequest {
    public purchaseNumber: number;
    public amount: number;
    public purpose: string;
}

class CheckAuthority {
    static execute(): void {
        var manager = new ManagerPPower();
        var director = new DirectorPPower();
        var vp = new VicePresidentPPower();
        var president = new PresidentPPower();
        manager.setSuccessor(director);
        director.setSuccessor(vp);
        vp.setSuccessor(president);
 
        var request = new PurchaseRequest();
        request.purchaseNumber = 111;
        request.amount = 4999;
 
        manager.processRequest(request);
       
        request.amount = 9999;
        manager.processRequest(request);
       
        request.amount = 19999;
        manager.processRequest(request);
       
          request.amount = 29999;
        manager.processRequest(request);
       
        request.amount = 30000;
        manager.processRequest(request);
    }
}

CheckAuthority.execute();
Coordinator
Oct 8, 2012 at 4:28 PM

Ickman wrote up a way of simulating protected (http://typescript.codeplex.com/discussions/398325) that you might want to look at.