What kind of pattern design would apply?

81 views Asked by At

this statement asks me what kind of design pattern applied, firstly , tell me what to build toys, as they are only 3 types of play, and none is related eg with some color, would apply the factory method.

But apart it tells me that the Toys can be turned off and on, which makes me think that this would be a command pattern (or state).

I can mix 2 patterns in a final solution?

We want to build 3 kinds of toys : Pokemon , Barbie and Superman . They feature animation actions associated with their off and on , these actions are different for each type of toy. Explain what design pattern and would use the class diagram Perform final design .

thanks

2

There are 2 answers

0
sanderarts On

You can use the interface and implementation akash89 described except the ShowAnimation interface and its implementation is the Strategy Pattern. In the strategy pattern you'll have an interface with interchangeable implementations, in this case your toys.

The State Pattern assumes that the internal behaviour changes of the object when you call some method. This is not the case in question.

A normal Factory Pattern is more than enough. You'll just create objects with (in the case of akash89) the ShowAnimation interface on which you can call the on and off animations. (However I would change the interface to IToy because a toy factory would create toys, not ShowAnimations ;) )

An Abstract Factory will create factories itself. For example, you could have an abstract factory that will create a factory for boy-specific toys or a factory for girl-specific toys. Each factory will then create barbies, pokemons and/or a superman.

1
akash89 On

You can mainly take the approach of using the 'Abstract Factory Pattern'.

This is how you should do the implementation

Declare an interface ShowAnimation

public interface ShowAnimation{
  public void onState();
  public void offState();
}

Then inside each of your Pokemon , Barbie and Superman classes, you will simply implement this ShowAnimation interface and override the onState and offState methods as per your business logic. This way

public class Barbie implements ShowAnimation{
  @override
  public void onState(){
     System.out.println("Show dance");
  }

  @override
  public void offState(){
     System.out.println("Do Namaste");
  }

}

This entire pattern you term as Abstract Factory Design Pattern.

Follow this link for more reference http://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm

Edit 1- I would like to update my answer "This is the Command Pattern that you will use using an interface and implementing the interface, however, you can create a separate abstract class Toys

abstract public class Toys {
provide some basic functionalities for this Toy class
}

and make other concrete class Barbie to extend Toys and implement ShowAnimation

So, basically you can mix both 'Factory Pattern' as well as 'Command Pattern' to achieve this.