C # learning notes for object-oriented design patterns (8)-composite combination patterns (Structural Patterns)

Source: Internet
Author: User

In some casesCodeToo much depends on the complex internal implementation structure of the object container. Changes in the internal implementation structure (rather than abstract interfaces) of the object container will cause frequent changes in the customer's code, it brings about code maintainability, scalability, and other drawbacks.

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?

Intent:

Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the use of a single object and a composite object consistent.

 

  Public     Interface  Ibox
{
Void Process ();
Void Add (ibox box );
Void Remove (ibox box );
}

Public Class Singlebox: ibox
{
Public Void Process ()
{
// 1. Do process for myself
}

Public Void Add (ibox box)
{
Throw Unsuporttedexception ();
}
Public Void Remove (ibox box)
{
Throw Unsuporttedexception ();
}
}

Public Class Containerbox: ibox
{
Arraylist list = Null ;

Public Void Add (ibox box)
{
If (List = Null )
{
List = New Arraylist ();
}
List. Add (box );
}

Public Void Remove (ibox box)
{
If (List = Null )
{
Throw New Exception ();
}
List. Remove (box );
}

Public Void Process ()
{
If (List ! = Null )
{
// 1. Do process for myself

// 2. Do process for the box in the list
Foreach (Ibox box In List)
{
Box. Process ();
}
}
}
}

 

 
  //Customer Code
ClassApp
{
Ibox box=Factory. getbox ();
Box. Process ();
}

 

Key points:

1. The composite mode uses a tree structure to implement a common object container, which converts the "one-to-many" relationship into a "one-to-one" relationship, this allows the customer code to process objects and object containers in a consistent manner without worrying about processing a single object or a combined object container;

2. Decoupling customer code from complex object container structures is the core idea of the composite mode, the Customer Code will be dependent on the pure abstract interface, rather than the complex internal implementation structure of the object container, so as to better "respond to changes ".

3. In composite mode, "add, remove, and other methods related to object containers" are defined in "component class indicating abstract objects, defining it in the "composite class that represents the object container" is a dilemma related to "Transparency" and "security" and requires careful consideration. This may violate the "single Responsibility Principle" of object-oriented systems, but this is a price that must be paid for this special structure. The implementation of ASP. NET controls provides a good demonstration in this respect.

4. In the composite mode, the sub-objects in the parent object can be traced back. If the parent object requires frequent traversal, you can use the cache technique to improve efficiency.

Related Article

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.