Presentation on theme: "Exercises Set 2 Design Patterns. 2 ReadyLifting Up Lifting Down stepin() up() stepout() down() Lift floor // lift position stepin() stepout() up() down()"— Presentation transcript:
Exercises Set 2 Design Patterns
2 ReadyLifting Up Lifting Down stepin() up() stepout() down() Lift floor // lift position stepin() stepout() up() down() Here is a state machine of the class Lift. Apply the State Pattern to implement the machine. You don’t need to provide full implementation of the transitions; you just need to show how the pattern can be applied to this situation. Name some important advantages of using the pattern to solve this problem. stepout()
3 The grade that you get for a course may consists of multiple grade-components. Each component can be a concrete grade (which is a non-negative value), or it can also be composite (that is, it consists of further components). Apply the Composite Pattern to implement grades. Show how you subsequently can implement an operation to check if a given grade is well formed. A grade is well formed if none of the concrete grades beneath it is negative. Suppose you now extend your grades such that each grade component can now have a weight. Show how you can implement a function to calculate the total weighted grade. Name one important advantage of using this Composite Pattern. Now consider the model of Course to the right. The operation there is supposed to calculate the final grade, if given a grade structure (as you implemented above). However, we actually want to have a collection of algorithms to calculate the final grade. Apply the Strategy Pattern to make this possible. Why do you bother to use the pattern here? Course calculateFinalGrade(grade)
4 Button name : String do() OnOff state doOff() ButtonDecorator To the left we have a model for Button. With decorators we can add features to a button. E.g. we have a to turn a button to a on/off button. That is, it has two states: on or off. When on the “on” state, do() will do the original do(), but flip the state to “off”. When on the “off” state, do() will actually do doOff(), and flip the state back to “on” again. The Delay feature will cause the button to be disabled for a specified duration after do() is called. Then it becomes enabled again. Anyway, provide an iterator (by applying the Iterator Pattern) to iterate over all decorators of a given button. What is the point of using the Iterator Pattern here? Toon moveForward() moveBackward() turnRight() turnLeft() equipedItem : Item equip(item) fire() To the left you see a model of toons/characters in a game. The game runs on a server. Players play through their game clients. Through the client a player sends commands to drive her toon. The commands are as shown in the model to the left. Apply the Command Pattern in this situation. What would be your benefit for using this pattern? Delay delay … (pls also read the note in the note page)
5 MOB Name : String Description : String show() run() Game showMOB(name : String) spawn() Here you see a class MOB (Mobile/Monster) for a game. The game has two operations shown to the left. One is to show how a mob looks like to a player (e.g. as part of a help system), and the other is to spawn a random type of mob in the game. Both operations will need a way to create a mob. Provide this through the Factory Method pattern. What is the benefit of using this pattern here? RabidDuck Tiger GiantSpider We now want to offer a number of ‘themes’ of this game. We have the standard one, the techno-version, and the prehistoric-version. They differ only in the visualization and the behavior of each mob-type. E.g. a prehistoric tiger looks much bigger, whereas a techno tiger is metallic and can do few tricks that a standard tiger cannot do. Extend your factory to facilitate the creation of different kinds of MOBs from different Game-themes. Now provide abstract factories to create mobs of different themes. In what way is the purpose of abstract factories different then the factories that you had before/above ?
6 Monster itemEquiped : Item + moveForward() + moveBackward() + turnRight() + turnLeft() + equip(item) + fire() Mob - itemslot : Item + walk() + run() + turnR() + turnL() + equip(item,itemslot) + fire() The game company HeroQuest (HQ) has bought a smaller company Basher. The latter has a game product called MonsterBash. HQ would reuse the monsters in that game for its own game. The class Monster is shown above. In HQ’s own game, monsters are represented by the class Mob above. To facilitate this integration, write an adapter (that is, apply the Adapter Pattern) to turn a Monster into a Mob. For each of the patterns below, describe a situation where you want to apply it, and not the others. Adapter pattern Proxy pattern Facade pattern