When should you use a composite design template?

I do not understand when I should use the composite design template . What benefits will I get from this design pattern? I visited this site , but it only tells me about the structure of the design pattern, and not about the scenarios in which it is used. I hope this will be useful for programmers like me who are starting to learn a design pattern.

+42
design-patterns composite
Mar 17 2018-11-11T00:
source share
10 answers

Quote from design patterns ,

Use the Composite Template When

  • you want to represent the hierarchies of the objects of the whole.
  • you want clients to be able to ignore the difference between composition of objects and individual objects. Clients process all objects in a composite structure evenly.

Common use is an example of motivation in a book - a display system for graphic windows, which may contain other windows and graphic elements, such as images, text. A component can be compiled at runtime, and client code can manage all the elements without worries, for what type it is designed for ordinary operations, such as drawing.

+21
Mar 17 '11 at 12:15
source share

A Composite is a template that is useful at any time when you may need to selectively process a group of objects that are part of a hierarchy as “identical” when they are actually different. Commonly used examples speak the same in terms of leaf and node processing, but the template can also be extended to heterogeneous lists.

For example, consider visiting a doctor. When you go to the doctor, different things happen, you usually see a nurse or assistant, they take your temperature, etc. Then the doctor performs an exam and makes a diagnosis. Then the doctor may do some treatment, but often the nurse comes back to finish. During the visit, various events are held. You have observations, such as weight and temperature. But the laboratory, for example, will be a different facility, because it often requires a sample, which can then be sent and requires that the results be recorded later.

So, we have software that allows us to record all this, and it usually creates some hierarchy with nodes such as:

Encounter:
Preexam
Exam
Treatment

and under each of these nodes you will have many records, such as diagnostics, observation, laboratory procedure, diagnostics, injection, etc.

This is good and good, and you get a structured, albeit very complex, hierarchical record of the meeting.

Now suppose you need to create billing. Suddenly you are faced with a completely different demand. Your medical record was necessary to create a very accurate picture of the meeting. When billing, although you don’t care who did what or in what order, you don’t really care what happens outside of the billing code. You just need one list of paid actions, i.e. codes.

This information is not only embedded in the record, but this record is very complex, because it contains a large number of different objects. It also varies in a hierarchical structure - if you have a nail in your head, they can skip any preliminary exam or exam on this subject and switch to treatment. If you go in to remove stitches, there may not be an exam or exam. The annual physical treatment does not have. etc. It is very difficult to list this type of object graph.

A composite template solves all this. You define a common interface or base class for all objects. Let me call him "CareEntry." CareEntry has the BillingCode property. Now your Encounter may seem like a simple container that contains only CareEntry objects. Now your billing service will simply list everything without worrying about whether there is something node (PreExam, Exam) compared to the sheet (weight temperature) or that the node object is located (PreExam exam, etc.) Or what is the actual type of object (laboratory, injection, etc.). Everything also applies to CareEntry and is processed evenly. You simply list all CareEntry objects in Encounter and collect each one that has a zero billing code, and you're done. It is so simple.

+54
Mar 17 2018-11-11T00:
source share

A composite template allows customers to process individual objects as well as compositions (individual objects) evenly.
For example, when you double-click a folder, it should open the folder. In a double file, it must be opened in the corresponding program.
The operation is the same, but behaves based on whether they are separate objects or compositions

Common interface for individual objects and composite objects

interface Data{ public void doubleClick(); } 

The implementation of individual objects

 class File implements Data { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void doubleClick() { System.out.println(this.getName()+" file is Opened in a Program "); } } 

Composite implementation

 class Folder implements Data { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } private List<Data> folder = new ArrayList<Data>(); @Override public void doubleClick() { System.out.println(this.getName() + " folder is Opened"); for(Data data : folder) { data.doubleClick(); } } public void add(Data data) { folder.add(data); } public void remove(Data data) { folder.remove(data); } } 

Client program

 public class CompositePattern { public static void main(String[] args) { Folder f1 = new Folder();f1.setName("Folder 1"); Folder f2 = new Folder();f2.setName("Folder 2"); Folder f3 = new Folder();f3.setName("Folder 3"); File file1 = new File();file1.setName("File 1"); File file2 = new File();file2.setName("File 2"); File file3 = new File();file3.setName("File 3"); File file4 = new File();file4.setName("File 4"); f1.add(file1); f2.add(file2); f3.add(f2); f3.add(file3); f3.add(file4); f1.doubleClick();f2.doubleClick();f3.doubleClick(); } } 
+13
Jul 16 '15 at 9:03
source share

I often use the Composite pattern to hide collections. In many cases, we work with collections in the same way when there are many elements and when they have only one element.

This is a problem because the class containing the collection is then overflowed with foreach loops, which basically do the same thing - go through all the elements and apply some aggregate function.

To solve this problem, I present an interface that is implemented by a single element, as well as a class that hides a collection of these elements. The purpose of a composite class is to then contain all the aggregated functions that were previously in the client class.

You will find some useful examples in this article: Working with collections

It is generally accepted that Composite does not necessarily represent a partial whole relationship. You can only enter a compound element to move contours from the client.

And here is one example of a book using the Composite template to hide details behind a composite element: Composite design template

+6
Jun 17 '15 at 12:57
source share

You may find it necessary when you work with binary trees or other complex data structures like list of lists of lists - etc ... then when each element (class) implements 1 interface, you can use the same methods for 1 sheet or a whole group of them - bypass, add, delete, move ... no matter what you did correctly. It is very useful and easy.

+2
Mar 17 '11 at 8:00
source share

The answer should be -

Compose objects in tree structures to represent a hierarchy of whole parts. Composite allows customers to process individual objects and composition of objects evenly.

  • Recursive composition
  • "Directories contain entries, each of which may be a directory.
  • 1-to-many has up, it's a hierarchy

copied from the forum.

+2
May 14 '13 at 10:00 a.m.
source share
+1
Mar 17 2018-11-11T00:
source share

A composite sample of a real-world pattern, When we have the likelihood of having an instance of the same type of the parent type inside or the type of the component.

Example: In currency trading systems Ex1

You can have a cross-currency pair (AUD / EUR) = (AUD / USD and 1 / (EUR / USD)) point here - your Instrument (Cross) can have two Instruments (straight) inside.

In another example

One tool (cross) and tool (straight) and tool (cross) which can be further divided into a tugboat Instrument (Direct). SGD / CZK = USD / SGD (direct) and USD / CZK (cross) = USD / SGD (direct) and (1 / EUR / USD) (direct) and EUR / CZK (direct)

Here you continue to share until you find all the direct currency pairs.

The above can be easily implemented using the Composite Design template.

+1
Jan 20 '14 at 6:03
source share

if you want to create nested similar objects, you can go to the composite design template for example: in real time, if you want to show a tree structure for an office employee based on a hierarchy

+1
May 04 '17 at 2:24 pm
source share

Having recently studied and tried my hand, I learned one powerful concept to keep in mind about composites.

Composites hide the complexity associated with collections, that is, looping them, sorting them, filtering some, etc. and allowing it to be treated as if it were the only organism.

Tell me, do you have a dog in one kennel and many dogs in another. You want to feed and vaccinate them, but you cannot feed them if they ate for an hour or fed them if they were vaccinated in the last five hours, or vaccinated if they vomited, etc.

More importantly, there are packing order rules where dog breed A eats before breed B if the dog of breed C was not around and barked to the top of its lungs.

This will quickly reach the point where you just don't want to take care of anything, but just call the assistant and tell him to feed all the dogs. Or better, three helpers to keep track of feeding, vaccination and vomiting, barking and packing and all the other amazing things.

By calling assistants, you relied on the composition template. You just go and feed each kennel, be it one dog or ten. You just want a kennel full of dogs to figure it out and figure out how they eat, because you have too much in the hands of the cashier.

So, for you, the dog is an IDog with Feed (), Bark (), Vomit (), and GetVaccine (). The kennel is also the Dog that you call the kennel .Feed (). You made. The nursery must find out what to do now internally. It may have a time-saving mechanism to monitor each dog’s feeding and other times the body is functioning. All of this is encapsulated neatly.

+1
Nov 10 '17 at 21:03
source share



All Articles