当前位置:网站首页>Combined mode, transparent mode and secure mode
Combined mode, transparent mode and secure mode
2022-06-26 05:45:00 【Green water monster 12138】
What is a combination pattern
Portfolio model (composite): Combine objects into a tree structure to represent “ part - whole ” The hierarchy of . Make the use of single object and composite object consistent .
UML chart

Use scenarios for composite patterns
Source blog
1、 If you need to implement a tree object structure , Consider using a combination pattern .
The composite pattern provides you with two basic element types that share a common interface : Simple leaf nodes and complex containers . Containers can contain leaf nodes and other containers . This allows you to build a tree nested recursive object structure .
2、 If you want client code to handle simple and complex elements in the same way , You can use this mode .
All elements defined in the composite schema share the same interface . With the help of this interface , The client doesn't have to care about the specific class of the object it uses .
Small exercise
Implement a commodity tree structure

package composite;
public class test {
public static void main(String[] args) {
incGoods incGoods1=new incGoods(" clothing ");
incGoods incGoods2=new incGoods(" men's wear ");
incGoods2.add(new speGoods(" shirt "));
incGoods2.add(new speGoods(" The jacket "));
incGoods1.add(incGoods2);
incGoods2=new incGoods(" Women's wear ");
incGoods2.add(new speGoods(" skirt "));
incGoods2.add(new speGoods(" suit "));
incGoods1.add(incGoods2);
incGoods1.display(1);
}
}
package composite;
public abstract class Goods {
protected String name;
public Goods(String name){
this.name=name;
}
public abstract void add(Goods goods);
public abstract void remove(Goods goods);
public abstract void display(int depth);
}
package composite;
public class speGoods extends Goods{
public speGoods(String name){
super(name);
}
@Override
public void add(Goods goods) {
// TODO Auto-generated method stub
System.out.println(" Cannot add item ");
}
@Override
public void remove(Goods goods) {
// TODO Auto-generated method stub
System.out.println(" Cannot delete item ");
}
@Override
public void display(int depth) {
// TODO Auto-generated method stub
System.out.println(name+"-"+depth);
}
}
package composite;
import java.util.ArrayList;
import java.util.List;
public class incGoods extends Goods{
List<Goods> goods;
public incGoods(String name){
super(name);
goods=new ArrayList<Goods>();
}
@Override
public void add(Goods goods) {
// TODO Auto-generated method stub
this.goods.add(goods);
}
@Override
public void remove(Goods goods) {
// TODO Auto-generated method stub
goods.remove(goods);
}
@Override
public void display(int depth) {
// TODO Auto-generated method stub
System.out.println(name+"-"+depth);
for(Goods goodss:goods){
goodss.display(depth+1);
}
}
}
Transparent way 、 safe mode
In combination mode UML In the picture we can see ,Ledf Class inherited Component Class add and remove Method , But these two methods are useful for leaf Class does not have to be implemented ( But it must be realized ), This method reduces the judgment of client object processing . It's called Transparent way
safe mode , take add and remove Method in Component Remove from , stay compisite Direct implementation in . In this way, specific judgment must be made during client object processing , for example :
In transparent mode ,
Component a=new Leaf();
Component b=new Composite();
b.add(a);
In a safe way ,
Leaf a=new Leaf();
Composite b=new Composite();
b.add(a);
How to choose , It needs to be based on the specific situation .
The relationship with other design patterns
Bridging mode 、 Status mode and The strategy pattern ( To some extent, it includes the adapter pattern ) The interface of is very similar . actually , They are all based on composite patterns —— Delegate work to other objects , But they also solved different problems . Patterns are not just recipes for organizing code in a particular way , You can also use them to discuss problems solved by patterns with other developers .
You can use... When creating complex composite trees Builder pattern , Because this allows its construction steps to run recursively .
The responsibility chain model is often used in conjunction with the composite model . under these circumstances , After the leaf component receives the request , The request can be passed along the chain containing all the parent components to the bottom of the object tree .
You can use the iterator pattern to traverse the composite tree .
You can use visitor mode to perform operations on the entire composite tree .
You can use the shared element mode to realize the shared leaf nodes of the composite tree to save memory .
Combination and Decoration mode The structure diagram of is very similar , Because both rely on recursive composition to organize an unlimited number of objects .
Decoration is similar to combination , But it has only one sub component . There is another obvious difference : Decoration adds additional responsibilities to the encapsulated object , Only the results of its child nodes are combined “ Sum up ”.
however , Patterns can also cooperate with each other : You can use decoration to extend the behavior of specific objects in the composition tree .
Designs that make extensive use of composition and decoration often benefit from the use of prototype patterns . You can use this pattern to copy complex structures , Instead of rebuilding from scratch .
边栏推荐
- 写在父亲节前
- CMakeLists. txt Template
- Describe an experiment of Kali ARP in LAN
- Red team scoring method statistics
- 423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
- What management systems (Updates) for things like this
- Thinking about bad money expelling good money
- [arm] build boa based embedded web server on nuc977
- Pytorch (network model)
- Some doubts about ARP deception experiment
猜你喜欢
随机推荐
How Navicat reuses the current connection information to another computer
cartographer_ optimization_ problem_ 2d
redis探索之布隆过滤器
9 common classes
一段不离不弃的爱情
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
[arm] build boa based embedded web server on nuc977
Daily production training report (16)
【PHP】PHP二维数组按照多个字段进行排序
【ARM】讯为rk3568开发板buildroot添加桌面应用
[upsampling method opencv interpolation]
旧情书
A new explanation of tcp/ip five layer protocol model
bingc(继承)
虚拟项目失败感想
力扣 875. 爱吃香蕉的珂珂
Use jedis to monitor redis stream to realize message queue function
uniCloud云开发获取小程序用户openid
Leetcode513.找出树的左下角的值
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree

![[C language] deep analysis of data storage in memory](/img/2e/ff0b5326d796b9436f4a10c10cfe22.png)






