OO mode-Composite, oo-composite
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. *; // Add reference public abstract class AbstractFile {protected String name; // define the name field public void printName () {System. out. println (name);} // The add and remove methods are usually used to provide the function of adding or removing leaves or branches. public abstract boolean addChild (AbstractFile file ); // Add public abstract boolean removeChild (AbstractFile file); // remove // a set of objects that store the abstract file's subfiles 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;} // The file is not added. It is just a separate individual public boolean addChild (AbstractFile file) {return false;} // For the file itself, it is already a leaf node, so there is no public boolean removeChild (AbstractFile file) {return false;} // Since File is already a leaf node, so there is no set, so this method returns a null 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; // used to create a set to save the sub-file this. childList = new ArrayList <AbstractFile> () ;}// Add the subfile public boolean addChild (AbstractFile file) {return childList. add (file) ;}// Delete the subfile public boolean removeChild (AbstractFile file) {return childList. remove (file) ;}// the return type of the subclass should be consistent with the definition of the parent class. 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) {// method stub automatically generated by TODO // construct a tree-like file and directory structure AbstractFile rootFolder = new Folder ("c :\\"); abstractFile compositeFolder = new Folder ("composite"); AbstractFile windowsFolder = new Folder ("windows"); AbstractFile file File = new file ("TestComposite. java "); rootFolder. addChild (compositeFolder); rootFolder. addChild (windowsFolder); compositeFolder. addChild (file); // print the directory file tree 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); // call the printing method }}}
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!
What is composite mode?
The COMPOSITE mode is the component video mode. It is to divide the image and video signals into red, green, blue three base colors, namely RGB mode, or a Brightness Signal, two chromatic aberration signals, namely Y, Yb, Yc transmission mode.
What is the oo design model?
OO is the Object Oriented object-oriented
Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued.
The oo design pattern is the object-oriented design pattern.