dimanche 10 juillet 2016

Using strategy pattern with simple factory

I've been learning about design patterns and I think I'm a little bit stuck with an exercise where I do not posses any answer-pages to.

I already used the strategy pattern to create this: http://ift.tt/29MJjGr (minus the setBehavior functions in duck)

I need to improve this design with simple factory because at the moment, the 2 types of behaviors are instantiated via following code (shown via example):

    public MallardDuck()
    { 
    setQuackBehavior(new Quack());
    setFlyBehavior(new FlyWithWings());
    }

This is not correct because we need to program to an interface and not to an implementation.

What I thought to do is making 2 separate factories that each handle a specific behavior but when I made the design I started doubting my solution. Is it normal to do this sort of thing with simple factory?

Could anyone push me in the correct direction? :]

Aucun commentaire:

Enregistrer un commentaire