Analysis of Android source code design patterns and practices (19th)

Source: Internet
Author: User

Analysis of Android source code design patterns and practices (19th)
Chapter 2 combination mode

The combination mode is also called the partial-overall mode, one of the structural design modes.

1. Definition

Combine objects into a tree structure to represent the "part-whole" hierarchy, so that users can use a single object and a combination object in a consistent manner.

2. Use Cases

(1) indicates the part of the object-the overall hierarchy.

(2) scenarios where some modules or functions can be isolated from a whole.

3. UML class diagram

(1)Component: Abstract The root node, which is the object declaration interface in the combination. When appropriate, implement the default behavior of all class interfaces. Declare an interface to access and manage child nodes of Component. You can define an interface in a recursive structure to access a parent node and implement it as appropriate.

(2)Composite: Defines the actions of sub-nodes, stores sub-nodes, and implements operations related to sub-nodes in the Component interface.

(3)Leaf: Indicates the leaf node object in the combination. The leaf node does not have any subnodes and defines the behavior of the Node object in the combination.

(4)Client: The Component interface is used to manipulate the objects of a combined node.

This method defines all the methods used by the combination in the abstract class.Transparent Combination ModeIf you Remove Add, Remove, and GetChild from Component, Add them only in Composite. This method is calledSafe Combination Mode. However, the latter violates the Dependency inversion principle.

4. Simple implementation

Take a file system like files and folders as an example.

Abstract class of files and folders: (Component)

Public abstract class Dir {/*** declares a List member variable to store all elements in the folder */protected List
Dirs = new ArrayList (); Private String name; // name of the current file or folder public Dir (String name) {this. name = name;}/*** Add a file or folder ** @ param dir file or folder */public abstract void addDir (Dir dir ); /*** remove a file or folder ** @ param dir file or folder */public abstract void rmDir (Dir dir ); /*** clear all elements in the folder */public abstract void clear ();/*** Output Folder directory structure */public abstract void print (); /*** get all files or folders in the folder ** @ return all files or folders in the folder */public abstract List GetFiles ();/*** get the file or folder name ** @ return file or folder name */public String getName () {return name ;}}

Indicates the folder class: (Composite)

public class Folder extends Dir{    public Folder(String name) {        super(name);    }    @Override    public void addDir(Dir dir) {        dirs.add(dir);    }    @Override    public void rmDir(Dir dir) {        dirs.remove(dir);    }    @Override    public void clear() {        dirs.clear();    }    @Override    public void print() {        System.out.print(getName() + "(");        Iterator
Iter = dirs. iterator (); while (iter. hasNext () {Dir dir = iter. next (); dir. print (); if (iter. hasNext () {System. out. print (",") ;}} System. out. print (")") ;}@ Override public List GetFiles () {return dirs ;}}

Folder class: (Leaf)

Public class File extends Dir {public File (String name) {super (name) ;}@ Override public void addDir (Dir dir) {throw new UnsupportedOperationException ("the file object does not support this operation! ") ;}@ Override public void rmDir (Dir dir) {throw new UnsupportedOperationException (" the file object does not support this operation! ") ;}@ Override public void clear () {throw new UnsupportedOperationException (" the file object does not support this operation! ") ;}@ Override public void print () {System. out. print (getName () ;}@ Override public List
GetFiles () {throw new UnsupportedOperationException ("the object does not support this operation! ");}}

Customer type:

Public class Client {public static void main (String [] args) {// construct a directory object to indicate the C root directory Dir diskC = new Folder ("C "); // There Is A log.txt diskC file under the C-root directory. addDir (new File ("Log.txt"); // The C root directory contains three directories: Windows, PerfLogs, and Program File Dir dirWin = new Folder ("Windows "); // The Windows directory contains the file assumer.exe dirWin. addDir (new File ("assumer.exe"); diskC. addDir (dirWin); // PerfLogs directory Dir dirPer = new Folder ("PerfLogs"); // The perflogsdirectory contains the file null.txt dirPer. addDir (new File ("null.txt"); diskC. addDir (dirPer); // Program File directory Dir dirPro = new Folder ("Program File"); // The Program filedirectory contains the ftp.txt dirPro File. addDir (new File ("ftp.txt"); diskC. addDir (dirPro); // print the file structure diskC. print ();}}

Result:

C(Log.txt, Windows(explorer.exe), PerfLogs(null.txt), Program File(ftp.txt))
5. Implementation of the pattern in the Android Source Code 1. nested combination of View and ViewGroup

The structure of View and ViewGroup is similar to the above UML class diagram. However, the View level of View uses a safe combination mode. ViewGroup has methods such as addView, removeView, and getChildAt for the View, so you may be familiar with them.

6. Summary 1. Advantages

(1) The composite mode clearly defines hierarchical complex objects, indicating all or part of the objects. It allows the high-level module to ignore the differences in layers and control the entire hierarchy.

(2) simplified the code of the high-level module.

(3) adding new branches and leaf components in the combination mode is convenient. You do not need to modify the existing class libraries and comply with the "Open and Close principle ".

(4) tree structure control becomes simple.

2. Disadvantages

The combination mode is not easy to restrict components in the combination. In most cases, they all come from the same abstraction layer. In this case, you must perform a type check to implement this implementation process.

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.