mardi 12 septembre 2017

Designer Patter hybrid of State and Command?

I am looking for a design pattern that enables dynamic behaviour of a function based on the implementations of the abstract subjects it applies the behaviour to.

An abstract example:

    interface IPlace
    {    
    }

    class Garage implements IPlace
    {
    }

    class Backyard implements IPlace
    {
    }

    class Mover
    {
        void Move(IPlace source, IPlace destination);
    }

Usage example:

    void NotMain()
    {
        IPlace garage = new Garage();
        IPlace backyard = new Backyard();
        Mover mover = new Mover();
        mover.move(garage, garage);
        mover.move(garage, backyard);
        mover.move(backyard, garage);
        mover.move(backyard, backyard);
    }

Expected result:

Mover moved an item inside the garage
Mover moved an item from the garage to the backyard
Mover moved an item from the backyard to the garage because it's raining
Mover moved an item around in the backyard

I think the answer might be the Strategy pattern, but i am unsure of how the behaviour should be decided as there are n^2 different behaviours, where n is the number of implementations of the IPlace interface. Perhaps a factory that returns a MoveOperation object?

I am interested in the best practice solution to this kind of problem.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire