Design Pattern C # language Description--synthesis (composite) mode

Source: Internet
Author: User
Tags abstract definition class definition requires safe mode
Design design Pattern C # language Description--synthesis (composite) mode



* This article refers to "Java and the pattern" part of the content, suitable for the design pattern of beginners.



The composite model pattern belongs to the structure pattern of objects, sometimes called partial-whole mode. The compositing pattern organizes objects into a tree structure that can be used to describe the relationship between the whole and the part. A compositing pattern enables a client to treat a simple element to the same as a composite element. such as folders and files are typical applications of synthetic patterns. According to the difference of the interface of the pattern, the synthetic mode can be divided into two kinds: safe type and transparent type.



The safe synthesis mode requires that the method of managing aggregation only appears in the branch component class, but not in the leaf component class. The class diagram looks like this:


involves three roles:



Abstract widget (Component): This is an abstract role that defines a common interface and its default behavior for the participating objects, and can be used to manage all of the child objects. A composite object usually treats a child object that it contains as a component object of type. In the safe synthesis mode, the component role does not define a method for managing the child objects, which is given by the tree Component object.

Leaf component (leaf): A leaf object is an object that has no subordinate child objects, defining the behavior of the original object participating in the composition.

Branch Widget (Composite): An object that represents a subordinate child object that participates in a combination. The branch widget class gives all the methods for managing child objects, such as Add (), Remove (), and so on.



Component:

public interface Component

{

void Sampleoperation ();



}//End INTERFACE DEFINITION Component



Leaf:

public class Leaf:component

{

public void Sampleoperation ()

{

}

}//End CLASS DEFINITION Leaf



Composite:

public class Composite:component

{

Private ArrayList componentlist=new ArrayList ();



public void Sampleoperation ()

{

System.Collections.IEnumerator myenumerator = Componentlist.getenumerator ();

while (Myenumerator.movenext ())

{

((Component) myenumerator.current). Sampleoperation ();

}

}



public void Add (Component Component)

{

Componentlist.add (component);

}



public void Remove (Component Component)

{

Componentlist.remove (component);

}



}//End CLASS DEFINITION Composite



In contrast to the safe mode of synthesis, the transparent synthesis pattern requires that all concrete component classes, whether tree or leaf members, conform to a fixed interface. The class diagram looks like this:




Abstract widget (Component): This is an abstract role that defines a common interface and its default behavior for the participating objects, and can be used to manage all of the child objects. To provide an interface to standardize access to and manage interfaces for the underlying components, including add (), Remove ().

Leaf component (leaf): A leaf object is an object that has no subordinate child objects, defining the behavior of the original object participating in the composition. The leaf object gives a mediocre implementation of the methods such as Add (), Remove ().

Branch Widget (Composite): An object that represents a subordinate child object that participates in a combination. Defines the behavior of such an object.



Component:

public interface Component

{

void Sampleoperation ();



void Add (Component Component);



void Remove (Component Component);



}//End INTERFACE DEFINITION Component



Leaf:

public class Leaf:component

{

Private ArrayList Componentlist=null;



public void Sampleoperation ()

{



}

public void Add (Component Component)

{



}

public void Remove (Component Component)

{



}

}//End CLASS DEFINITION Leaf



Composite:

public class Composite:component

{



Private ArrayList componentlist=new ArrayList ();



public void Sampleoperation ()

{

System.Collections.IEnumerator myenumerator = Componentlist.getenumerator ();

while (Myenumerator.movenext ())

{

((Component) myenumerator.current). Sampleoperation ();

}

}



public void Add (Component Component)

{

Componentlist.add (component);

}



public void Remove (Component Component)

{

Componentlist.remove (component);

}



}//End CLASS DEFINITION Composite





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.