Skip to topic | Skip to bottom
Home
Minfo
Minfo.SeTD4y1314r1.2 - 01 Nov 2013 - 14:39 - PhilippeCollettopic end

Start of topic | Skip to actions

Lab 4: Design Patterns

Introduction

Download and create a project with this archive: tp4-start-en.zip This is an almost complete correction of the previous lab. Take time to check that all abstractions are present (from SeTD3y1314).

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?

TIP 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?

-- PhilippeCollet - 01 Nov 2013


to top


You are here: Minfo > SeTD4y1314

to top

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