Combination of design patterns and Applications in Android

Source: Internet
Author: User
Definition

The combination mode allows you to combine objects into a tree structure to show the "whole/part" hierarchy. The combination can process individual objects and object combinations in a consistent manner.



Structure chart




You can use the compoment interface to interact with objects in the compoment structure. If the receiver is a leaf node, the request is processed directly. If the receiver is a composite, it usually sends the request to its child parts, and may perform some auxiliary operations before or after forwarding the request.


Sample Code

Component:

public abstract class Component {    public int getChildNum(){    throw new UnsupportedOperationException();    };    public Component getChild(int i){    throw new UnsupportedOperationException();    }    public String getType(){    throw new UnsupportedOperationException();    };    public String getName(){    throw new UnsupportedOperationException();    };    public void add(Component c){    throw new UnsupportedOperationException();    };    public void remove(Component c){    throw new UnsupportedOperationException();        };  //        public abstract void print();}

Leaf:

public class Leaf extends Component {private String strType = "leat";private String strName = "Leat";public Leaf(String strName){this.strName = strName;}    public String getType(){    return strType;    };    public String getName(){    return strName;    };@Overridepublic void print() {// TODO Auto-generated method stubSystem.out.println("i am "+getName());}}

Composite:

public class Branches extends Component {private ArrayList list;private String strType = "composite";private String strName = "Branches";public Branches(String strName){list = new ArrayList();this.strName = strName;}public int getSize(){return list.size();};    public int getChildNum(){    return list.size();    };    public Component getChild(int i){    if(i<=list.size()){    return (Component) list.get(i);    } else {    return null;    }        }    public String getType(){    return strType;    };    public String getName(){    return strName;    };    public void add(Component c){    list.add(c);    };    public void remove(Component c){    list.remove(c);    };@Overridepublic void print() {// TODO Auto-generated method stubIterator iterator = list.iterator();while(iterator.hasNext()){((Component)iterator.next()).print();System.out.println("belong to "+getName());}System.out.println("i am "+getName());}}

public class Tree extends Component {private ArrayList list;private String strType = "composite";private String strName = "Tree";public Tree(String strName){list = new ArrayList();this.strName = strName;}public int getSize(){return list.size();};    public int getChildNum(){    return list.size();    };    public Component getChild(int i){    if(i<=list.size()){    return (Component) list.get(i);    } else {    return null;    }        }    public String getType(){    return strType;    };    public String getName(){    return strName;    };    public void add(Component c){    list.add(c);    };    public void remove(Component c){    list.remove(c);    };@Overridepublic void print() {// TODO Auto-generated method stubIterator iterator = list.iterator();while(iterator.hasNext()){((Component)iterator.next()).print();}System.out.println("i am "+getName());}}

Client:

Public static void main (string [] ARGs) {// todo auto-generated method stubcomponent tree = new tree ("poplar"); component branches1 = new branches ("branches1 "); component branches2 = new branches ("branches2"); component branches3 = new branches ("branches3"); component branches4 = new branches ("branches4 "); component leaf1 = new leaf ("leaf1"); component leaf2 = new leaf ("leaf2"); component leaf3 = new leaf ("leaf3 "); component leaf4 = new leaf ("leaf4"); component leaf5 = new leaf ("leaf5"); component leaf6 = new leaf ("leaf6 "); component leaf7 = new leaf ("leaf7"); component leaf8 = new leaf ("leaf8"); tree. add (branches1); tree. add (branches2); tree. add (branches3); tree. add (branches4); then (leaf1); branches1.add (leaf2); branches2.add (leaf3); then (leaf4); then (leaf5); branches3.add (leaf6); then (leaf7 ); branches4.add (leaf8); tree. print ();}

Test results:

I am leaf1belong to publish am leaf2belong to publish am leaf3belong to publish am leaf4belong to publish am role am leaf5belong to publish am leaf6belong to publish am leaf7belong to publish AM to publish am branches4i am Poplar

This Code uses a tree (a tree in a non-data structure) to demonstrate the combination mode. The code is simple, but it can clearly reflect the advantages of the combination mode.

In-depth analysis

Decoupling customer code from complex object container structures is the core idea of the combination mode. After decoupling, the Customer Code will be dependent on the pure abstract interface, rather than the complex internal implementation structure of the object container, to better cope with changes.

The management method of child objects must be provided in the combination mode. Otherwise, the addition and deletion of child objects cannot be completed, and flexibility and scalability are lost. But is the management method declared in component or in composite? One way is to declare all the methods used to manage subclass objects in component to maximize the component interface. The goal is to make the customer look that there is no difference between leaf and branch at the interface level-transparency. However, leaves do not have child classes. Therefore, some declared methods are not applicable to leaves. This also brings about some security issues. Another method is to declare all methods used to manage subclass objects in composite. In this way, the security of the previous method is avoided, but because the leaves and branches have different interfaces, the transparency is lost. Design Patterns: In this model, we emphasize transparency over security. Empty processing or exception reporting is not required in the first method.

In a specific implementation, the combination mode allows reverse tracing of sub-objects in the parent object. If the parent object requires frequent traversal, you can use the cache technique to improve efficiency.

Key Points
  • 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 the same way, without the need to process a single object or a combined object container.

  • 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 object interface, rather than the complex internal implementation structure of the object container, so as to better "respond to changes ".

  • In composite mode, "add and remove methods related to object containers" are defined in "component class indicating abstract objects, defining it in the "composite class indicating the object container" is a dilemma related to "Transparency" and "security". We need to carefully weigh the structure, this is the price that must be paid.

  • In the specific implementation of the composite mode, the word object in the parent object can be traced back: if the parent object requires frequent traversal, the cache technique can be used to improve efficiency.

Applicability:

The composite mode is used in the following cases:

  1. The part of the object-the overall hierarchy.
  2. If you want to ignore the differences between a composite object and a single object, you will use all objects in the composite structure in a unified manner.

 

Advantages:
  1. The combination mode can easily add new components.
  2. The combination mode can make the client easy to design, because the client can treat the combination and leaf nodes equally.
Disadvantages:
  1. After the combination mode is used, it is not easy to control the type of the branches.
  2. It is very difficult to add new behaviors by using an inherited method.

Applications in Android

In Android, The View class is a typical combination mode.

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.