1 / 10

Exercises Set 2 Design Patterns, solution

Exercises Set 2 Design Patterns, solution. Ready. Lift floor stepin () stepout () up() down(). State stepin () stepout () up() down(). Lifting. 1. LiftingUp. LiftingDown. I will only give pseudo code below!. class Lift { int floor State currentstate ;

zona
Télécharger la présentation

Exercises Set 2 Design Patterns, solution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercises Set 2Design Patterns, solution

  2. Ready Lift floor stepin() stepout() up() down() State stepin() stepout() up() down() Lifting 1 LiftingUp LiftingDown I will only give pseudo code below! class Lift { int floor State currentstate ; File() { currentstate = new Ready(this) ; floor=0 } stepin() { currentstate.stepin() ; } stepout() { currentstate.stepout() ; } etc } class Ready extends State { Ready (Lift g) { super(g) } stepin() { f.currentstate = new Lifting(this) } } 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 -- } } 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() { } ... }

  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++ } } stepout() Ready In highest floor stepout() [else] Lifting Up up() down() [floor < MAX-1] Lifting 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. class ConcreteGrade { Float val ; ... // constructor isWellFormed() { return val >= 0 } calcFinal() { return val ; } } * components Grade weight isWellFormed() : bool calcFinal() : Float class CompositeGrade { List<Grade> 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 } } CompositeGrade ConcreteGrade val : Float

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

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

  7. Toon moveForward() moveBackward() turnRight() turnLeft() equipedItem equip(item) fire() class MoveForward { MoveForward(Toon t) { toon = t } execute() { toon.moveForward() } undo() { toon.moveBackward() } } GameClient MoveForward toon <<use>> MoveBackward Command execute() undo() TurnRight class Fire { Fire (Toon t) { toon = t } execute() { toon.fire() } undo() { throw exeception … can’t undo fire } } TurnLeft Fire Equip itemToEquip previousItem 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. 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 } Game showMOB(name : String) spawn() <<use>> MOBFactory createMob(type) : MOB MOB Name : String Description : String show() run() TechnoMOBFactory <<use>> for standard MOBfac : createMob(type) { if (type.equals(RABIDDUCK)) return new RabidDuck() else if (type.equals(TIGER)) return new Tiger() else return new GiantSpider() } PrehistoryRabidDuck RabidDuck TechnoRabidDuck Tiger GiantSpider

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

  10. 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 itemEquiped : Item + moveForward() + moveBackward() + turnRight() + turnLeft() + equip(item) + fire() Mob - itemslot : Item[] + walk() + run() + turnR() + turnL() + equip(item,itemslot) + fire() monster MonsterAdapter MonsterAdapter(monster)

More Related