Implementing the Strategy Pattern for my specific issue

89 views Asked by At

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

1

There are 1 answers

0
Matthew Sant On BEST ANSWER

You're on the right track. Each Cook should have a list of Preparation interfaces. 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 as PutApronOnPreparation that can have a String member called description that can also be set from the constructor. This is what it would look like:

class PutApronOnPreparation implements Preparation {
    private final String description;

    public PutApronOnPreparation(String description) {
        this.description = description;
    }

    @Override
    public getDescription() {
        return description;
    }
}

Now, if you have a Cook instance that has been initialised with a list of these Preparation implementations, its prepare() method can iterate over the list and call getDescription() for each one of them.

Finally, to search through a number of Cook instances you can have another method such as hasPreparation(...) that can take a string argument and which iterates over the Preparation list and checks if the getDescription() 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.