lundi 13 mai 2019

Can the Composite Design pattern avoid a Collection (List, HashTable) data structure?

I want a full comprehension of all possible types of the Composite Design Pattern when looking at code. In this code I have a composite object PeanutButterAndJellySandwich which contains Bread, PeanutButter and Jelly.

The reason PeanutButterAndJellySandwhich's field variables are all of type food is to account calories

interface Food{
    int calories = 9000;
}


class Bread implements Food{
    private String flour;
    private String salt;

}

class Jelly implements Food{
    private String fruit;
    private String sugar;
}

class PeanutButter implements Food{
    private boolean crunchy;
}

class PeanutButterAndJellySandwich implements Food{
    private Food bread;
    private Food jelly;
    private Food peanutButter;

    public void setBread(Bread bread){this.bread=bread;}
    public void setJelly(Jelly jelly){this.jelly=jelly;}
    public void setPeanutButter(PeanutButter butter){this.peanutButter=butter;}
}

Is this follow the Composite Design Pattern or does PeanutButterAndJellySandwhich need something like foodItems:ArrayList<Food>. The UML Diagram would look very similar.

UML Diagram

I ask this because I have not found this anywhere explicitly stated after about 5 hours of searching and in the application of something like an embedded system we may not want a collection data structure since memory/storage is limited. However, all examples online are utilizing many forms of lists.

Aucun commentaire:

Enregistrer un commentaire