Skip to topic | Skip to bottom
Minfo.SeLab6r1.3 - 27 Nov 2012 - 23:05 - KrikavaFiliptopic end

Start of topic | Skip to actions

Lab 6 : Decomposition and Design Patterns


In this lab we will further explore the new decomposition of the creature simulator and enhance it by employing some design patterns. Create a new project based on the latest simulator code This time it is ready project that you can just import using the File -> Import... -> General -> Existing Project into Workspace. The project itself is in the TP6 directory in the archive. Following the last lab, this code incorporates a new decomposition that has some advantages over the old one.

Exercise 0: Warm Up

Get yourself familiar with the current code. Have a look at the tp6.umlcd.png file from the attachment table at the ned of the page for a UML diagram.

Exercise 1: Fixing the simulator (Observer pattern)

You have noticed that there is a Launcher class that contain the main method. You can use it to run the simulator. If you do, you will quickly find out that it does not really work. Your task is therefore to try to fix it. Hint: remember the observer pattern.

Exercise 2: Fixing the resize (Observer pattern)

There is one more problem with the application. Try to resize the window. Again, your task is to fix it. Hint: look at the JPanel#addComponentListener()

Exercise 3: Factory for the creatures (Factory pattern and Abstract factory pattern)

Now adding one creature at the time might be slightly annoying so you should try to make a factory CreatureFactory that can create a set of creatures at once. It should be given the dimension of the simulation plane, the number of creature to create. Let's start by having all creatures black (or any other color to your liking). Think about what you will have to do in order to create a different creature types? Hint: apply the factory pattern recursively - a factory of a factory. Actually the top level factory will then become an abstract factory. You can even mix a singleton pattern in here. Where will it fit?

Exercise 4: Color strategies (Strategy pattern)

Regardless what color you have picked up, after a while it will get a bit boring, so it would be nice if the factory can also be more colorful. For this it should take yet another parameter a reference to an instance of a IColorPicker that will have one method getNextColor(). Then implement different strategies like one that only gets back one color, one that iterates over the color cube like in the old createCreature method, etc.

If you think about the interface, you might realize that it is kind of iterator, thus you could also implement it using simply an Iterator interface. What would be the advantage/disadvantage?
to top

I Attachment sort Action Size Date Who Comment
tp6.umlcd.png manage 310.9 K 20 Nov 2012 - 22:25 KrikavaFilip manage 1390.3 K 20 Nov 2012 - 22:28 KrikavaFilip manage 22.9 K 21 Nov 2012 - 09:24 KrikavaFilip This time it is only the sources (src dir)

You are here: Minfo > SoftEng1213 > SeLab6

to top

Copyright © 1999-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WIKIDeptinfo? Send feedback