Design Mode Study Notes-Composite mode

Source: Internet
Author: User



Before model learning


What is the design model: when we design the program, we gradually formed some typical problems and solutions, which is the software model; each mode describes a problem that often occurs in our program design and the solution to the problem. When we encounter the problem described by the mode, the corresponding solution can be used to solve the problem. This is the design mode.

A design pattern is an abstract thing. It is not learned but used. Maybe you don't know any pattern at all, and you don't consider any pattern, but write the best code, even from the perspective of "pattern experts", they are all the best designs and have to say "best pattern practices". This is because you have accumulated a lot of practical experience, knowing "where to write code" is a design pattern.

Some people say: "If the level is not reached, you can learn it in vain. If the level is reached, you can do it yourself ". It is true that the mode is well-developed and may still not be able to write good code, let alone design a good framework. OOP understanding and practical experience reach a certain level, and it also means to summarize a lot of good design experience, however, it may not be the case that "no teacher can do anything", or you can say that, on the basis of level and experience, it is time for the system to learn the "mode, it is helpful to learn the experts' summary and verify your own shortcomings.

This series of design patterns learning notes is actually a Learning Record for the book "Java and patterns.


Composite Definition


The Composite mode is sometimes called the Part-Whole mode. The merging mode organizes objects into the tree structure and can be used to describe the relationship between the whole and the part. The merging mode allows the client to view elements as compound elements.

Structure:

Vcn5w/fX07bUz/examples + dPQubm8/examples + examples/OhozwvcD4KPHA + examples/fL + examples/aoaM8L3A + examples + PHN0cm9uZz694bm5zbw8L3N0cm9uZz48L2gyPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140403/20140403091637114.png" alt = "\">

Roles involved

(1) The role of the abstract Component (Component): This is an abstract role. It defines public interfaces and default behaviors for The objects involved in the combination and can be used to manage all sub-objects. A merging Object usually treats its sub-objects as Component objects. In a safe synthesis mode, the component role does not define a method for managing sub-objects. This definition is provided by the Branch component object.

(2) Leaf component (Leaf) Role: A Leaf object is an object without sub-child objects and defines the behavior of the original objects in the combination.

(3) Composite role: represents the object with sub-object in the combination. The branches component class provides all methods for managing sub-objects, such as add (), remove (), and components.

Code Implementation

Import java. util. *; interface Component {// return your instance Composite getComposite (); // a commercial method void sampleOp ();} class Composite implements Component {private List
 
  
ComponentList = new ArrayList
  
   
(); // Return your instance public Composite getComposite () {return this;} // a commercial method public void sampleOp () {Iterator it = componentList. iterator (); while (it. hasNext () {(Component) it. next ()). sampleOp () ;}// container management, add the sub-Component object public void add (Component c) {componentList. add (c);} // container management, delete the sub-Component object public void remove (Component c) {componentList. remove (c) ;}} class Leaf implements Component {// return your instance public Composite getComposite () {// Write you code herereturn null ;} // a commercial method public void sampleOp () {// Write your code here }}
  
 

Transparent synthesis Mode


Structure chart


Roles involved

(1) Abstract Component (Component) Role: This is an abstract role that specifies an interface for the objects that participate in the combination to standardize common interfaces and default behaviors. This interface can be used to manage all sub-objects. An interface must be provided to obtain and manage lower-level components in a standardized manner, including methods such as add (), remove (), and getChild.

(2) Leaf component (Leaf) Role: this role represents the Leaf objects that participate in the combination and defines the behavior of the original objects that participate in the combination. The leaf class provides mediocre implementation of methods such as add (), remove (), and getChild () used to manage subclass objects.

(3) Composite role: represents the object with sub-objects in the combination and defines such behavior.

Code Implementation

Import java. util. *; interface Component {// return your instance Composite getComposite (); // a commercial method void sampleOp (); // container management, add the sub-Component object void add (Component c); // container management, delete the sub-Component object void remove (Component c);} class Composite implements Component {private List
 
  
ComponentList = new ArrayList
  
   
(); // Return your instance public Composite getComposite () {return this;} // a commercial method public void sampleOp () {Iterator it = componentList. iterator (); while (it. hasNext () {(Component) it. next ()). sampleOp () ;}// container management, add the sub-Component object public void add (Component c) {componentList. add (c);} // container management, delete the sub-Component object public void remove (Component c) {componentList. remove (c) ;}} class Leaf implements Component {// return your instance public Composite getComposite () {// Write you code herereturn null ;} // a commercial method public void sampleOp () {// Write your code here} // container management, add the sub-Component object public void add (Component c) {}// container management, delete the sub-Component object public void remove (Component c ){}}
  
 

Learn more


(1) Where JDK uses the most compositing mode, it is an AWT library, java. awt. button/java. awt. checkBox is a leaf component, java. awt. container is a branch component, java. awt. component is an abstract Component.

(2) The merging mode should be considered in the following circumstances: a. The part of the object to be described and the overall hierarchical structure; B. The client must ignore the differences between the individual component and the composite component; the client must treat all components equally, including individual components and composite components.

(3) in actual production, structures with tree-level relationships, such as categories, permissions, and departments, are very suitable for the application of the synthesis mode. For example, tree classification, in the past, we may only create a model class that is exactly the same as the data table row. Now we can develop it based on the thought of the merging mode to fulfill more responsibilities and make it easier to use. This extension does not have to have leaf components or abstract components. It may only have branch components. In short, it is suitable for specific scenarios.

(4) In the course of study, I can't find the scenario of the synthesis mode, or understand that I can optimize the previous design, and the Development has broadened a lot. It seems like I have an epiphany.

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.