For a school assignment I have a to implement the Strategy Pattern and I fully understand the pattern with the interface and the context that depending on what is asked gets the proper Strategy but I just can't seem to figure out how to implement it with my specific problem.
The problem:
Give the design of a solution (with a UML-classdiagram) which solves following problem: A system administers a few cooks (using a class cook for this). Some of these cooks need different things than others in order to prepare themselves for a cooking session, this way the cooks distinguish amongst others and are able to serve different settings. The system is used by a chain of restaurants that works with a lot of cooks. It is important to compose the right set of cooks based on a limited combination of preparations, it should be fairly easy to remove cooks of a certain type or add new ones. Your solution should be able to show different steps after calling the method prepare() on a cook. Some examples: cook Jeroen just needs to ‘put an apron on’, this is also what most others cooks do. But cook Christian needs to ‘put an apron on, take a collection of knifes and set some classical music on’. And the cooks Nini and Leo need to ‘open a beer and set some hardrock on’. Beware: some of the actions require interaction with other classes (they don’t have to be included explicitly in your solution), so it is not sufficient to just add a String-attribute ‘preparation’!
My guess is that the preperation is thet Strategy interface and the different kinds of preperations are ConcreteStrategies (per example found here: http://upload.wikimedia.org/wikipedia/commons/3/39/Strategy_Pattern_in_UML.png) and the cook is the context in this example
Hope you can shed some light on it for me (i'd prefer a hint towards te correct direction instead of a full solution)
Edit: Replaced my explenation of the problem and inserted the original text
You're on the right track. Each
Cookshould have a list ofPreparationinterfaces. I would recommend having a constructor for the cook that takes a list of these instead of a setter. The Preparation interface should have a way to obtain the description of the preparation e.g.getDescription(). One could then create an implementation such asPutApronOnPreparationthat can have a String member calleddescriptionthat can also be set from the constructor. This is what it would look like:Now, if you have a
Cookinstance that has been initialised with a list of thesePreparationimplementations, itsprepare()method can iterate over the list and callgetDescription()for each one of them.Finally, to search through a number of
Cookinstances you can have another method such ashasPreparation(...)that can take a string argument and which iterates over the Preparation list and checks if thegetDescription()value contains that string. Alternatively, it can take a Preparation as an argument and, given you have overridden the equals() method, you would match the objects directly.