Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercises Set 2 Design Patterns, solution. 2 Lift floor stepin() stepout() up() down() Ready Lifting LiftingUp LiftingDown 1 class Lift { int floor State.

Similar presentations


Presentation on theme: "Exercises Set 2 Design Patterns, solution. 2 Lift floor stepin() stepout() up() down() Ready Lifting LiftingUp LiftingDown 1 class Lift { int floor State."— Presentation transcript:

1 Exercises Set 2 Design Patterns, solution

2 2 Lift floor stepin() stepout() up() down() Ready Lifting LiftingUp LiftingDown 1 class Lift { int floor State currentstate ; File() { currentstate = new Ready(this) ; floor=0 } stepin() { currentstate.stepin() ; } stepout() { currentstate.stepout() ; } etc } I will only give pseudo code below! class Ready extends State { Ready (Lift g) { super(g) } stepin() { f.currentstate = new Lifting(this) } } class State { Lift lift State (Lift g) { lift = g } // provide do-nothing as default behavior. // Alternatively, you can choose to throw an // exception as default. stepin() { } stepout() { }... } class Lifting extends State { Lifting(Lift g) { super(g) } stepout() { f.currentstate = new Ready(this) } up() { f.currentstate = new LiftingUp(this) ; lift.floor ++ } down() { f.currentstate = new LiftingDown(this) ; lift.floor -- } } State stepin() stepout() up() down()

3 3 class Lift { static READY = 0 static LIFTING = 1 static LIFTINGUP = 2 static LIFTINGDOWN = 3 int floor int state File() { currentstate = new Ready(this) ; floor=0 } stepin() { if (state==READY) state=LIFTING } stepout() { if (state!=READY) state=READY } up() { if (state==LIFTING) { state=LIFTINGUP; floor++ } else if (state==LIFTINGUP) floor++ } Lifting Up up() In highest floor [floor < MAX-1] [else] Ready stepout() Lifting Down down() Adding a new state will force us to fix Lift, in the operations op, down, and perhaps also stepout. Using the DP we only need to fix up of LiftingUp. The rest is extensional.

4 4 Grade weight isWellFormed() : bool calcFinal() : Float ConcreteGrade val : Float CompositeGrade * class ConcreteGrade { Float val ;... // constructor isWellFormed() { return val >= 0 } calcFinal() { return val ; } } class CompositeGrade { List components ;... // constructor isWellFormed() { for (Grade g : components) { if (! g.isWellFormed()) return false } return true } calcFinal() { for (Grade g : components) { totW += g.weight ; tot += g.,weight * g.calcFinal() ; } if (totW != 1.0) throw some exception ; return tot } components

5 5 Course GradingAlg calc(grade) StandardSum WeigthedSum WeigthedSumW ithOutoFail calc(grade) { return grade.sum() } calc(grade) { return grade.weigthedFinal() }

6 6 > Iterator next() : Object hasNext() : boolean ButtonDecoratorIterator class ButtonDecoratorIterator { ButtonDecorator ptr ButtonDecorator next ButtonDecoratorIterator(ButtonDecorator target) { ptr = null ; next = target} next() { ptr = next ; next = next.base ; return ptr ; } hasNext() { return (next instanceOf ButtonDecorator) } } Button name : String do() OnOff state doOff() ButtonDecorator iterator() : Iterator Delay delay … base

7 7 Toon moveForward() moveBackward() turnRight() turnLeft() equipedItem equip(item) fire() Command execute() undo() toon MoveForward MoveBackward TurnRight TurnLeft Equip itemToEquip previousItem Fire GameClient > class MoveForward { MoveForward(Toon t) { toon = t } execute() { toon.moveForward() } undo() { toon.moveBackward() } } class Fire { Fire (Toon t) { toon = t } execute() { toon.fire() } undo() { throw exeception … can’t undo fire } } class Equip { Equip (Toon t, Item i ) { itemToEquip = i ; previousItem = null } execute() { if (previousItem == null) previousItem = toon.equipedItem ; toon.equip(itemToEquip) } undo() { toon.equip(previousItem) } }

8 8 MOB Name : String Description : String show() run() RabidDuck Tiger GiantSpider MOBFactory createMob(type) : MOB Game showMOB(name : String) spawn() for standard MOBfac : createMob(type) { if (type.equals(RABIDDUCK)) return new RabidDuck() else if (type.equals(TIGER)) return new Tiger() else return new GiantSpider() } showMOB(name) { f = new MOBFactory() ; f.createMob(name).show() ; } spawn() { randomly decide Mob-type ; f = new MOBFactory() ; Mob m = f.createMob(type ); put m in the game } > TechnoRabidDuck TechnoMOBFactory PrehistoryRabidDuck

9 9 MOB Name : String Description : String show() run() RabidDuck Tiger GiantSpider MOBAbstractFactory createDuck(..) : RabidDuck createTiger(..) : Tiger createSpider(..) : GiantSpider class StandardMOBAbsFactory extends MOBAbstractFac { createDuck(type) { return new RabidDuck() } … } TechnoRabidDuck TechnoMOBAbsFactory PrehistoryRabidDuck StandardMOBAbsFactory PrehistoricMOBAbsFactory class TechnoMOBAbsFactory extends MOBAbstractFac { createDuck(type) { return new TechnoDuck() } … }

10 10 MonsterAdapter MonsterAdapter(monster) class MonsterAdapter extends Mob { Monster monster MonsterAdapter(m) { monster = m } walk() { monster.moveForward() } // run is just mapped to moveF run() { monster.moveForward() } turn() {monster.turnRight() } // ignore the k parameter equip(item,k) { monster.equip(item) } …. } monster Monster itemEquiped : Item + moveForward() + moveBackward() + turnRight() + turnLeft() + equip(item) + fire() Mob - itemslot : Item[] + walk() + run() + turnR() + turnL() + equip(item,itemslot) + fire()


Download ppt "Exercises Set 2 Design Patterns, solution. 2 Lift floor stepin() stepout() up() down() Ready Lifting LiftingUp LiftingDown 1 class Lift { int floor State."

Similar presentations


Ads by Google