Design Patterns learning Summary-composite method)

Source: Internet
Author: User

Problem:
In some cases,
An object with the "container feature" acts as an object and is a container of another object, such as an object in a tree structure, if the customer relies too much on the complex internal implementation structure of the object container, changes to the internal implementation structure of the object container will cause the customerCode. How can we decouple "Customer Code from complex object container structures? Let the object container implement its own complex structure, so that the customer code can process the complex object container like a simple object?
Definition:
Composite is one of the constructor design patterns. composite is a hierarchy that combines objects into a tree structure to represent "part-whole, the combination mode ensures consistency between the use of a single object and a combination object.

Intent:
A component role is provided, which defines the universal interfaces for accessing and managing components (Part-whole). They generally include add (), remove (), and other methods. Defines the composite role of a user to store sub-parts. It implements all the methods that the component role provides to access and manage parts. A reference list of the component role is maintained to manage and maintain all child parts. In this way, the basic object can be combined into a more complex composite object, and the composite object can be combined. In this way, a complex object with a tree structure is formed after recursion. The combination mode uses a tree structure to implement ubiquitous object containers, which converts one-to-many relationships into one-to-one relationships so that the customer code can process objects and object containers in a consistent manner, you do not need to worry about processing a single object or a combined object container. Change customerProgramIt is decoupled from the internal object, so that the object container can implement its own complex structure. After decoupling, the client program can only have a relationship with the interface at the upper layer of the object.

Participants:
• Abstract component role (Component ):
Is the object declaration interface in the combination. When appropriate, the default behavior of all class common interfaces is implemented. Declare an interface to access and manage component sub-parts.
• Leaf ):
In the combination, it indicates that the leaf node object does not have a subnode and implements the interface for declaring the role of the abstract component.
• Sub-component role (composite ):
Indicates a branch Node object in a combination. It is used to store sub-parts and has sub-nodes. interfaces that implement abstract component role declarations, such as add and remove.
UML:

Code Description: 

///   <Summary>
/// Abstract component role)
///   </Summary>
Public Abstract Class Component
{
Public String Name { Get ; Set ;}
Public Abstract Void Add (Component component );
Public Abstract Void Remove (Component component );
Public Abstract Void Show ();
}
///   <Summary>
/// Leaf component role (leaf)
///   </Summary>
Public Class Leaf: Component
{
Public Override Void Add (Component component)
{
System. Console. writeline ( " I am a leaf node and cannot add sub-objects " );
}
Public Override Void Remove (Component component)
{
System. Console. writeline ( " I am a leaf node with no sub-objects " );
}
Public Override Void Show ()
{
System. Console. writeline ( " I am a leaf node. " );
}
}
///   <Summary>
/// Composite)
///   </Summary>
Public Class Compositeclass: Component
{
List <component> List = New List <component> ();
Public Override Void Add (Component component)
{
List. Add (component );
}
Public Override Void Remove (Component component)
{
List. Remove (component );
}
Public Override Void Show ()
{
Foreach ( VaR S In List)
{
System. Console. writeline (string. Format ( " I am a {0} node. " , S. Name ));
}
}
}
///   <Summary>
/// Client Test
///   </Summary>
Public Void Compositetest ()
{
Component OBJ = New Compositeclass ();
OBJ. Name = " I am a node " ;
Component obj1 = New Compositeclass () {name = " I am still a node " };
Component obj2 = New Compositeclass () {name = " I am still a node " };
Obj2.add ( New Leaf () {name = " I am a leaf node " });
OBJ. Add (obj1 );
OBJ. Add (obj2 );
}

Advantages:
• The customer code is decoupled from the complex object container structure, allowing the object container to implement its own complex structure. You do not have to modify the client code to add or remove objects.
• A single object and a composite object have consistent interfaces, so you do not have to process a single object or the entire composite structure on your own, which simplifies the client code.

Applicable scenarios:
• Indicates the part of the object-the overall hierarchy (tree structure ).
• If you want to ignore the differences between a composite object and a single object, you will use all objects in the composite structure in a unified manner.

Transparent and secure:
Transparent mode (clients are irrelevant and do not know whether to use composite or leaf ):
Declare all methods used to manage sub-objects in component, including add (), remove (), etc. In this way, all sub-objects of the component interface have add (), remove (), etc, in this way, gof can say that "the use of a single object and a composite object is consistent", but it does not have add (), remove () or other operations for the leaf node. This implementation is meaningless.
Security Mode:
Declare all methods used to manage sub-objects in composite, and do not define or implement unnecessary operations in leaf. This will make composite and leaf have different interfaces, client calls must be judged accordingly.
Gof is more inclined: In exchange for transparency with respect to non-compliance with the single responsibility principle, so that the client can combine and leaf nodes in a consistent manner.
There are many design trade-offs when implementing the combination mode. Balance transparency and security as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.