Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'composite pattern'?

Could anybody please explain and give a real-live example of Composite Design Pattern?

like image 953
Kerstin Schuster Avatar asked Jan 30 '11 13:01

Kerstin Schuster


People also ask

What is composite patterning?

The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.

What is composite pattern used for?

Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.

What is composite pattern in Java?

The composite pattern is meant to allow treating individual objects and compositions of objects, or “composites” in the same way. It can be viewed as a tree structure made up of types that inherit a base type, and it can represent a single part or a whole hierarchy of objects.

Is composite pattern good?

The Composite pattern lets you run a behavior recursively over all components of an object tree. The greatest benefit of this approach is that you don't need to care about the concrete classes of objects that compose the tree. You don't need to know whether an object is a simple product or a sophisticated box.

What is composite pattern in web design?

Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy. This type of design pattern comes under structural pattern as this pattern creates a tree structure of group of objects.

What is a composite pattern in Python?

The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.

When to use a composite design pattern UML?

Using a Composite Design Pattern is recommended when many primitive objects meet composite objects. This software design approach lets clients treat individual and compound objects consistently by hiding their differences from the client. What is a Composite Pattern? Which problems can be solved using a Composite Pattern UML?

When does the composite pattern make sense?

Using the Composite pattern makes sense only when the core model of your app can be represented as a tree. For example, imagine that you have two types of objects: Products and Boxes. A Box can contain several Products as well as a number of smaller Boxes. These little Boxes can also hold some Products or even smaller Boxes, and so on.


1 Answers

The composite pattern can be used when a collection of objects should be treated the same way as one object of the same type. This is often used with tree-structured data. Below is an example where this pattern suits well:

public abstract class Shape {
    public abstract void Draw();
}

public class Line : Shape {
    public override void Draw() {
        // Draw line
    }
}

public class Polygon : Shape {

    private IList<Line> lines;

    public override void Draw() {
        foreach (Shape line in lines) {
            line.Draw();
        }
    }
}

As you can see, the pattern makes it possible for the code dealing with drawing shapes to be unaware of how many lines are drawn.

like image 163
Andreas Vendel Avatar answered Oct 06 '22 04:10

Andreas Vendel