Assignment 4: Design Patterns
Give a summary description of six design patterns that you choose
from the following design patterns: Adapter, Bridge, Builder, Composite,
Decorator, Flyweight, Interpreter, Iterator, Mediator, Memento, Observer,
Prototype, Proxy, Template Method. In your summaries say:
Write a short summary for each of the six patterns, about half a page
for each pattern (rather less than more). Do not add diagrams, and
do not try to give a complete description of the patterns as found in the
books. Rather think of how you would explain the essential ideas of these
patterns in a few sentences to a colleague while riding the elevator.
what kind of problem(s) you can solve with that pattern and when you use
it, maybe with a short example
how the pattern works, what the basic idea of the pattern is
what the main advantage and what the the main disadvantage is of using
Credit card problem:
You have a file that contains credit card records. Each record contains
a field for the card number, the expiration date, and the name of the card
holder. In your system you have the following class structure for the credit
a class CreditCard,
classes VisaCC, MasterCC, AmExCC that are
all subclasses of CreditCard,
you assume more subclasses for other credit
card types will be added later on.
You now have to design the method(s) (and maybe additional classes)
that reads a record from the file, verifies that the credit card
number is a possible account number, and creates an instance of the appropriate
credit card class. What design patterns could you use for that?
Important details: Credit card numbers cannot exceed 19 digits, including
a single check digit in the rightmost position. The exact algorithm for
calculating the check digit as defined in ISO 2894/ANSI 4.13 is not important
for this assignment. You can also determine the card issuer based on the
credit card number:
||First digit is a 5, second digit is in range 1 through 5 inclusive.
Only valid lenght of number is 16 digits.
||First digit is a 4. Length is either 13 or 16 digits.
||First digit is a 3 and second digit a 4 or 7. Length is 15 digits.
||First four digits are 6011. Length is 16 digits.
- Describe what is the primary problem you try to solve.
- Describe what are the secondary problems you try to solve (if there
- Describe what design pattern(s) you use how (use plain text and/or
- Describe the consequences of using this/these pattern(s).
Hint: you face here (at least) two problems, one has to do with how
you figure out what kind of card a specific record is about, the other
one with how you create the appropriate objects. Look at behavioural patterns
and at creational patterns.
In assignment 4b you develop a design for the game minesweeper.
You can find minesweeper here for Windows. You
can download and play it, the help explains the rules how to play.
This is an assignment about design patterns, so the goal is of course to
use some of the patterns you read about. Probably the best way to
do it is to first develop a design, and then to go through the design and
check where you could improve the design by using some of the design patterns.
Also check where you already automatically have used a design pattern.
Keep in mind that in many situations you use several design patterns together.
Assume that in the future somebody could extend the system to accomodate
some additional rules, or could choose another user-interface, e.g. ,for
ascii-terminals or an output-/input-device for blind people, or could introduce
a mode where two persons play against each other, or ... You should
not model these things, but try to make the design as maintainable and
reusable as possible.
Think in small objects: each field might be an object, you might have subclasses
of fields (mined fields, save fields), you might have classes for
the different kind of covers (plain cover, question mark cover, flag
Distinguish between the visual things and the effective domain specific
things (what concepts would remain if your connection to the computer were
not a graphical user interface?)
If you are not sure if you should include a certain pattern or not, include
it and mention in the drawbacks what makes you doubt if the design pattern
is really appropriate.
Model of the minesweeper:
Class diagram showing essential classes, associations with names, important
attributes, important operations available to other classes. Add descriptions
etc. where necessary to make the diagram easily understandable for another
person while keeping it as short as possible.
For each GoF-design pattern (or your own special design mechanisms)
If you find more than three patterns or interesting mechanisms, then document
the most interesting three in above manner, and just list the other ones.
Use class-, object- or interaction-diagrams to show how you used the pattern,
if adequate also short textual description how you used the pattern.
List advantages of using the pattern (e.g., "gives clean solution to.....",
"enhances maintenability because....."). This should also describe the
problem you have solved by using the pattern and the positive consequences
of using the pattern.
List drawbacks and negative consequences of using the pattern (e.g., "overkill
in this situation").
4a has to be done individually. 4b and 4c can be done
in teams of two, and you can just hand in one documentation per
team. Design patterns are a tool to discuss designs, so I encourage you
to discuss your solutions together also with other teams.