mardi 24 novembre 2020

Creating instances of class using decorator design principle based on user input

At the moment I have executable code like this:

Sandwich sandwich_wTuna_wDoubleCheese = new Cheese(new Cheese(new Tuna(new Bread())));

For your better understanding, Sandwich is an abstract class that is extended by Bread and the SandwichDecorator. The sandwich decorator himself is again abstract and is extended by Cheese, Tuna, Tomatoes,.. you name it.

Now this works fine, however I have some bakeries that should sell these Sandwiches that were created using the decorator design principle. How am I going to do this?

I want that I can do something like this:

List<String> list=new ArrayList<String>();
list.add("Cheese");
list.add("Cheese");
list.add("Tuna");
Sandwich sandwich_wTuna_wDoubleCheese = bakery1.SellBreads(list);

Sure I could pass in the instances already and do the decorating within the bakery function, but I hope there are other ways.

Have you an idea, how to solve this? Thanks for your help and advice.

Aucun commentaire:

Enregistrer un commentaire