Design Mode-Combination Mode

Source: Internet
Author: User

Design Mode-Combination Mode

 

I have seen the shadows of design patterns in many occasions. I have been investing in algorithms and data structures for a long time. Later I found that design patterns are really important. Sometimes code maintenance, reuse, and scalability are better than pure algorithms. So I picked up Daniel's book "big talk Design Patterns" and referred to the blogs of various big cows on the Internet to start my design patterns journey. Since C/C ++ is used for programming at ordinary times, we need to learn Java again due to future work needs, and also practice Java syntax. The importance of the design pattern is particularly profound in preparing for the graduation course recently: in many cases, it is not necessary to write relevant algorithm modules, instead, I always feel that my code is less readable, reusable, and extensible, which is not conducive to transplantation.
Today, we will introduce the combination mode.
1. concept:
Composite: Composite combines objects into a tree structure to represent the 'partial-uniyun' hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.
2. Implementation:

Common tree structures include leaf nodes and branch nodes. Leaf nodes do not contain child node sets, but branch nodes do.

1) Abstract role Component: the object declaration interface in the combination can also implement default behavior for the common interface.
2) Leaf role Leaf: indicates the Leaf node object in the combination. It has no subnodes and implements the interface for declaring the role of the abstract component.
3) Branch role Composite: indicates the branch Node object in the combination and has a sub-structure to implement the interface for role declaration of abstract component.

3. Advantages:

Defines a class hierarchy that contains basic objects and composite objects. Basic objects can be combined into more complex composite objects, and these composite objects can be combined.

4. Disadvantages:

The combination mode does not support dynamic insertion or deletion of nodes, that is, it cannot determine whether the final node is a branch node or a leaf node. Therefore, the applicability is greatly limited and cannot be expanded to a tree structure with an uncertain structure.

5. Sample Code:

 

import java.util.ArrayList;/** * Created by xujin on 2014/12/7. */abstract class Component{     String m_strNodeName;    public  Component(String tName)    {        m_strNodeName=tName;    }    public abstract void  add(Component tCom);    public abstract void  delete(Component tCom);    public abstract void  show(int tDepth);}class Leaf extends Component{    public Leaf(String tName)    {        super(tName);    }    public void  add(Component tCom)    {    }    public void delete(Component tCom)    {    }    public void  show(int tDepth)    {        for(int i=0;i
 
   m_ArrList=new ArrayList
  
   ();    public Composite(String tName)    {        super(tName);        m_ArrList.clear();    }    public void  add(Component tCom)    {        m_ArrList.add(tCom);    }    public void delete(Component tCom)    {        int id=m_ArrList.size()-1;        if(id>=0)           m_ArrList.remove(id);    }    public void  show(int tDepth)    {        for(int i=0;i
   
    

 

 

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.