Method groupSelected(components: array of Graphic) is Combine selected components into one complex composite components as well as complex composites. This way the client code can support simple leaf The client code works with all the components via their base Draw a dashed rectangle using the bounding children and so forth, the whole object tree is traversed composite's children pass these calls to their own collecting and summing up their results. It traverses recursively through all its children, A composite executes its primary logic in a particular Remove a child from the array of children. (both simple or complex) to or from its child list. A composite object can add or remove other components work to their children and then "sum up" the result.Ĭlass CompoundGraphic implements Graphic is Composite objects usually delegate the actual The composite class represents complex components that may Draw a circle at X and Y with radius R. All component classes can extend other components. objects that do the actual work, while composite objects only leaf object can't have any sub-objects. The leaf class represents end objects of a composition. simple and complex objects of a composition. The component interface declares common operations for both The client can work with very complex object structures without being coupled to concrete classes that form that structure. Thus, the client doesn’t know whether it’s working with a simple shape or a compound one. The client code works with all shapes through the single interface common to all shape classes. However, instead of doing something on its own, a compound shape passes the request recursively to all its children and “sums up” the result. A compound shape has the same methods as a simple shape. The CompoundGraphic class is a container that can comprise any number of sub-shapes, including other compound shapes. In this example, the Composite pattern lets you implement stacking of geometric shapes in a graphical editor. When you call a method, the objects themselves pass the request down the tree. You can treat them all the same via the common interface. You don’t need to know whether an object is a simple product or a sophisticated box. The greatest benefit of this approach is that you don’t need to care about the concrete classes of objects that compose the tree. The Composite pattern lets you run a behavior recursively over all components of an object tree. A box could even add some extra cost to the final price, such as packaging cost. If one of these items were a smaller box, that box would also start going over its contents and so on, until the prices of all inner components were calculated. For a box, it’d go over each item the box contains, ask its price and then return a total for this box. How would this method work? For a product, it’d simply return the product’s price. The Composite pattern suggests that you work with Products and Boxes through a common interface which declares a method for calculating the total price. All of this makes the direct approach either too awkward or even impossible. You have to know the classes of Products and Boxes you’re going through, the nesting level of the boxes and other nasty details beforehand. That would be doable in the real world but in a program, it’s not as simple as running a loop. You could try the direct approach: unwrap all the boxes, go over all the products and then calculate the total. The whole structure looks like an upside down tree. How would you determine the total price of such an order?Īn order might comprise various products, packaged in boxes, which are packaged in bigger boxes and so on. Orders could contain simple products without any wrapping, as well as boxes stuffed with products.and other boxes. Say you decide to create an ordering system that uses these classes. These little Boxes can also hold some Products or even smaller Boxes, and so on. A Box can contain several Products as well as a number of smaller Boxes. Using the Composite pattern makes sense only when the core model of your app can be represented as a tree.įor example, imagine that you have two types of objects: Products and Boxes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |