OO mode-Composite

Source: Internet
Author: User

The combination mode is also called the "part-whole" mode. In this way, the definition is obvious, which corresponds to the knowledge of data structures, combine objects into a tree structure to represent the "part-whole" hierarchy.

First look at the class diagram:


First, let's analyze this class chart. Leaf and composite are of the same generation and inherit from the parent class component. Also, because the component contains the leaf subclass, there is an aggregation relationship between it and component.


A common example is that we deal with computers and various files and folders every day. Isn't that a good combination mode?

Let's look at the class diagram:


Let's take a look at the specific implementation code:

Abstractfile is the object declaration interface in the combination to realize the default behavior of interfaces common to all classes.

package composite;import java.util.*;    //添加引用public abstract class AbstractFile {protected String name; // 定义name字段public void printName() {System.out.println(name);}// 通常都用add和remove方法来提供增加或移除树叶或树枝的功能public abstract boolean addChild(AbstractFile file); // 增加public abstract boolean removeChild(AbstractFile file); // 移除// 一个集合,存放摘要文件的子文件的对象public abstract List<AbstractFile> getChildren();}
File is a subclass file that inherits the parent class and is also called a leaf node in the tree. The leaf node does not have child nodes. Therefore, the so-called methods of the parent class cannot be implemented. True and null are returned.

package composite;import java.util.*;public class File extends AbstractFile {public File(String name) {this.name = name;}//文件并没有添加的能力,它只是一个单独的个体public boolean addChild(AbstractFile file) {return false;}//对于文件本身,已经是叶子节点了,所以也无删除子文件的功能public boolean removeChild(AbstractFile file) {return false;}// 由于File已经是叶子节点了,所以就不存在集合这一说,所以这个方法返回的是空值public List<AbstractFile> getChildren() {return null;}}
Folder is a subclass folder and also inherits from the parent class. However, this class is only a common node and still contains leaf nodes.

package composite;import java.util.*;public class Folder extends AbstractFile {private List<AbstractFile> childList ;public Folder(String name) {this.name = name;//用来建立一个集合保存子文件this.childList = new ArrayList<AbstractFile>();}//添加子文件public boolean addChild(AbstractFile file) {return childList.add(file);}//删除子文件public boolean removeChild(AbstractFile file) {return childList.remove(file);}// 子类的返回类型应该和父类的定义保持一致public List<AbstractFile> getChildren() {return childList;}}
Finally, let's take a look at how the client calls and prints

package composite;import java.util.List;public class Client {public static void main(String[] args) {// TODO 自动生成的方法存根// 构造一个树形的文件、目录结构AbstractFile rootFolder = new Folder("c:\\");AbstractFile compositeFolder = new Folder("composite");AbstractFile windowsFolder = new Folder("windows");AbstractFile file = new File("TestComposite.java");rootFolder.addChild(compositeFolder);rootFolder.addChild(windowsFolder);compositeFolder.addChild(file);// 打印目录文件树printTree(rootFolder);}private static void printTree(AbstractFile ifile) {ifile.printName();List<AbstractFile> children = ifile.getChildren();if (children == null)return;for (AbstractFile file : children) {printTree(file); // 打印方法的调用}}}

Transparent and secure

In the file subclass, the so-called methods are not implemented, but they still exist. This method is called "transparent mode "; the advantage of this is that leaf nodes and branch nodes have completely consistent behavior interfaces, but the problem is also obvious, that is, the existence of the so-called methods in the file is meaningless;

If you do not want to make it useless, that is, remove all meaningless methods in the file subclass. This method is called "security mode", but because it is not transparent, therefore, they cannot have the same interface, which increases the complexity of the interface, and the client needs to add the corresponding judgment for the call.


When do I use the combination mode?

1) This mode is used when the requirement reflects the "part-whole" hierarchy;

2) This mode is used when you want to ignore the differences between a composite object and a single object and uniformly use all objects in the composite structure;


Conclusion:

Every mode actually needs to be understood slowly. If you really understand the essence of it, you will feel close when you see similar words, the combination modes are summarized as follows:

    • The combination mode provides a structure that can accommodate both individual objects and composite objects;
    • Allow customers to treat individual objects and composite objects equally;
    • Any object in the combination structure is called a component. A component can be a combination or a leaf node;
    • There are many design compromises when implementing the combination mode. At this time, we need to select the transparent or secure mode!


OO mode-Composite

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.