Design Mode C # Language Description - Composite Mode

zhaozj2021-02-16  65

Design Mode C # Language Description - Composite Mode

* This article refers to some of the "Java and Mode", suitable for beginners for design patterns.

The synthetic model mode belongs to the structural mode of the object, sometimes called some - overall mode. Synthesis Mode The object is organized into the tree structure and can be used to describe the overall relationship between the overall and part. The synthesis pattern allows the client to treat simple elements with the composite element. If folders and files are typical applications for synthesis patterns. The synthesis pattern can be divided into security and transparent form according to the difference in the interface to be implemented.

Safety synthetic mode requires management aggregation only in the branches component, and does not appear in the leaves component. The class diagram is as follows:

Three roles involved:

Abstract Components: This is an abstract role that defines a common interface and its default behavior to manage all child objects. Synthetic objects typically use the child object it contains as a type of Component. In a security format, the component role does not define the method of managing the child object, which is given by the branches component object.

Leaf member (Leaf): The leaves object are objects without the lower level object, define the behavior of the original object to participate in the combination.

Tree branches: represents the object of the combination of a combination of grade objects. The branches component gives all the methods of managed objects, such as add (), remove (), etc.

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

Different from the safety format mode, transparent synthetic mode requires all specific components, regardless of the branches and leaves, all in line with a fixed interface. The class diagram is as follows:

Abstract Components: This is an abstract role that defines a common interface and its default behavior to manage all child objects. To provide an interface to specify and manage interfaces of the underlying component, including add (), remove ().

Leaf member (Leaf): The leaves object are objects without the lower level object, define the behavior of the original object to participate in the combination. The leaves of the leaves give the mediocre implementation of add (), remove (). Tree branches: represents the object of the combination of a combination of grade objects. The behavior of such an object is defined.

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

转载请注明原文地址:https://www.9cbs.com/read-25071.html

New Post(0)