当前位置:网站首页>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 .
边栏推荐
- Written before father's Day
- 慢慢学JVM之缓存行和伪共享
- 数据存储:MySQL之InnoDB与MyISAM的区别
- Redis discovery bloom filter
- [MySQL] MySQL million level data paging query method and its optimization
- Ribbon负载均衡服务调用
- Daily production training report (17)
- 国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
- Red team scoring method statistics
- 【C语言】深度剖析数据在内存中的存储
猜你喜欢
cartographer_ optimization_ problem_ 2d
【C語言】深度剖析數據在內存中的存儲
小小面试题之GET和POST的区别
冒泡排序(Bubble Sort)
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
Pytorch (network model training)
Redis discovery bloom filter
There are applications related to web network request API in MATLAB (under update)
DOM文档
Leetcode513.找出树的左下角的值
随机推荐
Henkel database custom operator '~~‘
Red team scoring method statistics
MySQL source code reading (II) login connection debugging
[arm] add desktop application for buildreoot of rk3568 development board
RIA想法
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
[C language] deep analysis of data storage in memory
Consul服务注册与发现
[arm] build boa based embedded web server on nuc977
无线网络存在的安全问题及现代化解决方案
最后一次飞翔
冒泡排序(Bubble Sort)
The difference between get and post in small interview questions
售前分析
Talk 5 wireless communication
Feelings of virtual project failure
cross entropy loss = log softmax + nll loss
家庭记账程序(第二版 加入了循环)
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
A love that never leaves