JAVA design mode 17-Composite (Combination Mode)

Source: Internet
Author: User

Combination Mode


Composite Pattern is sometimes called partial-overall Pattern, which makes the concept of simple elements and complex elements fuzzy in the tree structure problem.
The customer program can process complex elements like simple elements to decouple the customer program from the internal structure of complex elements. Combined mode allows you to optimize processing delivery
Returns or grades the data structure. There are many examples of hierarchical data structures, making the combination mode very useful. A general example of hierarchical data structure is
The file system encountered when using the computer. The file system consists of directories and files. Content can be installed in each directory. The contents of a directory can be files or directories.
In this way, computer file systems are organized in recursive structures. If you want to describe such a data structure, you can use the Composite mode.
Definition (GoF design patterns): combines objects into a tree structure to represent a "partial whole" hierarchy. The combination mode makes the user consistent with the use of a single object.
Involved roles:
1. Component is the object declaration interface in the combination. When appropriate, it implements the default behavior of interfaces common to all classes. Declare an interface to access and manage Component
Child parts.
2. Leaf indicates the Leaf node object in the combination, and the Leaf node does not have any child nodes.
3. Composite defines the behavior of a branch node, which is used to store sub-parts. operations related to sub-parts are implemented in the Component interface, such as adding and deleting.

(Remove.
  
Applicability
The Composite mode is applicable in the following situations:
1. You want to represent the part of the object-the overall hierarchy
2. 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.
Summary
The combination mode decouples the internal structure of the customer program and complex elements, so that the customer program can process complex elements like simple elements.
If you want to create a hierarchy and treat all elements in the same way, the combination mode is the best choice. This chapter uses a file
The system example illustrates the purpose of the combined mode. In this example, all files and directories run the same interface, which is the key to the combination mode. By executing phase
With the same interface, you can treat files and directories in the same way, so as to store files or directories as sub-level elements of directories.

:
 

Example:
[Java]
1. package design. composite;
2 ./**
3. * file name: design. composite. Company. java
4. * created by: Fei Wong
5. * Creation Time:
6. * Email: www.2cto.com
7 .*
8 .*/
9. public abstract class Company {
10. private String name;
11.
12. public Company (String name ){
13. this. name = name;
14 .}
15.
16. public Company (){
17 .}
18.
19. public String getName (){
20. return name;
21 .}
22.
23. public void setName (String name ){
24. this. name = name;
25 .}
26.
27. protected abstract void add (Company company Company );
28.
29. protected abstract void romove (Company company Company );
30.
31. protected abstract void display (int depth );
32 .}
33.
34.
35.
36.
37. package design. composite;
38.
39. import java. util. ArrayList;
40. import java. util. List;
41 ./**
42. * file name: design. singleton. CommandInvoker. java
43. * created by: Fei Wong
44. * Creation Time:
45. * Email: www.2cto.com
46 .*
47 .*/
48. public class ConcreteCompany extends Company {
49. private List <Company> cList;
50.
51. public ConcreteCompany (){
52. cList = new ArrayList <Company> ();
53 .}
54.
55. public ConcreteCompany (String name ){
56. super (name );
57. cList = new ArrayList <Company> ();
58 .}
59.
60. @ Override
61. protected void add (Company company ){
62. cList. add (company );
63 .}
64.
65. @ Override
66. protected void display (int depth ){
67. // TODO Auto-generated method stub
68. StringBuilder sb = new StringBuilder ("");
69. for (int I = 0; I <depth; I ++ ){
70. sb. append ("-");
71 .}
72. System. out. println (new String (sb) + this. getName ());
73. for (Company c: cList ){
74. c. display (depth + 2 );
75 .}
76 .}
77.
78. @ Override
79. protected void romove (Company company Company ){
80. cList. remove (company );
81 .}
82 .}
83.
84.
85.
86. package design. composite;
87 ./**
88. * file name: design. composite. FinanceDepartment. java
89. * created by: Fei Wong
90. * Creation Time:
91. * Email: www.2cto.com
92 .*/
93. public class FinanceDepartment extends Company {
94.
95.
96. public FinanceDepartment (){
97.
98 .}
99.
100. public FinanceDepartment (String name ){
101. super (name );
102 .}
103.
104. @ Override
105. protected void add (Company company ){
106.
107 .}
108.
109. @ Override
110. protected void display (int depth ){
111. StringBuilder sb = new StringBuilder ("");
112. for (int I = 0; I <depth; I ++ ){
113. sb. append ("-");
114 .}
115. System. out. println (new String (sb) + this. getName ());
116 .}
117.
118. @ Override
119. protected void romove (Company company Company ){
120.
121 .}
122.
123 .}
124.
125.
126.
127.
128. package design. composite;
129 ./**
130. * file name: design. composite. HRDepartment. java
131. * created by: Fei Wong
132. * Creation Time:
133. * Email: www.2cto.com
134 .*/
135. public class HRDepartment extends Company {
136.
137.
138. public HRDepartment (){
139.
140 .}
141.
142. public HRDepartment (String name ){
143. super (name );
144 .}
145.
146. @ Override
147. protected void add (Company company ){
148.
149 .}
150.
151. @ Override
152. protected void display (int depth ){
153. StringBuilder sb = new StringBuilder ("");
154. for (int I = 0; I <depth; I ++ ){
155. sb. append ("-");
156 .}
157. System. out. println (new String (sb) + this. getName ());
158 .}
159.
160. @ Override
161. protected void romove (Company company Company ){
162.
163 .}
164.
165 .}
166.
167.
168.
169.
170. package design. composite;
171.
172. public class Client {
173.
174 ./**
175. * @ param args
176 .*/
177. public static void main (String [] args ){
178. // TODO Auto-generated method stub
179. Company root = new ConcreteCompany ();
180. root. setName ("Beijing Head Office ");
181. root. add (new HRDepartment ("Head Office Human Resources Department "));
182. root. add (new FinanceDepartment ("Head Office Finance Department "));
183. Company shandongCom = new ConcreteCompany ("Shandong Branch ");
184. shandongCom. add (new HRDepartment ("Shandong Branch Human Resources Department "));
185. shandongCom. add (new FinanceDepartment ("Accounting Department of Shandong Branch "));
186. Company zaozhuangCom = new ConcreteCompany ("Zaozhuang Office ");
187. zaozhuangCom. add (new FinanceDepartment ("Zaozhuang Office Finance Department "));
188. zaozhuangCom. add (new HRDepartment ("Zaozhuang Office Human Resources Department "));
189. Company jinanCom = new ConcreteCompany ("Jinan Office ");
190. jinanCom. add (new FinanceDepartment ("Finance Department of Jinan Office "));
191. jinanCom. add (new HRDepartment ("Jinan Office Human Resources Department "));
192. shandongCom. add (jinanCom );
193. shandongCom. add (zaozhuangCom );
194. Company huadongCom = new ConcreteCompany ("Shanghai East China Branch ");
195. huadongCom. add (new HRDepartment ("Shanghai East China Branch Human Resources Department "));
196. huadongCom. add (new FinanceDepartment ("Shanghai East China Branch Account Department "));
197. Company hangzhouCom = new ConcreteCompany ("Hangzhou Office ");
198. hangzhouCom. add (new FinanceDepartment ("Hangzhou Office Finance Department "));
199. hangzhouCom. add (new HRDepartment ("Hangzhou Office Human Resources Department "));
200. Company nanjingCom = new ConcreteCompany ("Nanjing Office ");
201. nanjingCom. add (new FinanceDepartment ("Finance Department of Nanjing Office "));
202. nanjingCom. add (new HRDepartment ("Nanjing Office Human Resources Department "));
203. huadongCom. add (hangzhouCom );
204. huadongCom. add (nanjingCom );
205. root. add (shandongCom );
206. root. add (huadongCom );
207. root. display (0 );
208 .}
209.
210 .}

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.