Design Patterns study notes (c) - structural model (Structural Pattern)
Others
2019-09-02 07:31:52
views: null
Structural model (Structural Pattern)
- Adapter mode (wrapper (Wrapper)) (Adapter)
- Class Adapter (through inheritance), object adapter (empty achieved by abstract class / interface method) (by combining (member variables)), interface adapter
- A conversion interface into another interface, so that the interface class can be incompatible with the work
- An object adapter can adapt to many different person fit into the same target, but Java single inheritance, do not support
- Application mode: a JDBC driver software is interposed between the JDBC interface adapter software and database engine interface
- Bridge mode (Bridge)
- Object structural model, also known as the shank member (Handle and Body) mode or an interface (Interface) mode
- If the software system, a class there are two separate dimensions change, which can be separated by mode these two dimensions, so that the two can be scaled independently, the system more in line with "single responsibility principle."
- The most intimate dimension designed to "abstract class" hierarchy (abstract part), and the other dimension is designed to "achieve class" hierarchy (partially realized)
Abstraction pa = new RefinedAbstraction(new ConcreteImplementorA());
pa.operation();
Abstraction pb = new RefinedAbstraction(new ConcreteImplementorB());
pb.operation();
- Combined mode (Composite)
- Also known as part of the overall pattern, for a group of similar objects as a single object.
- Combination pattern based on a combination of the object tree structure, a partially used and the overall level.
- Typical applications: operating system's file / directory structure
- Decorator Pattern (Decorator)
- Appearance Mode (Mode Facade) (the Facade)
- Facade appearance model also known pattern, it is an object schema structure
- An external communication subsystem with a unified must look through an object, to provide a consistent interface subsystem is a set of interfaces,
- Facade pattern defines a high-level interface that makes the subsystem easier to use
- Flyweight (the Flyweight)
- Flyweight achieve the same or similar objects reuse by sharing techniques:
- May share the same content is referred to as internal state (IntrinsicState),
- Those who can not share the contents of the external environment need to set is called an external state (Extrinsic State)
- Usually occurs in the Flyweight factory mode, you need to create a Flyweight factory responsible for maintaining a pool Flyweight (Flyweight Pool) is used to store the same internal state of an object Flyweight
- Typical applications: String constant pool, database connection pool
public abstract class Flyweight {
public String intrinsic = "Flyweight";//内部状态
protected final String extrinsic;//外部状态
//要求享元角色必须接受外部状态
public Flyweight(String extrinsic) {
this.extrinsic = extrinsic;
}
//定义业务操作
public abstract void operate(int extrinsic);
//TODO intrinsic内部状态的get/set方法
}
public class ConcreteFlyweight extends Flyweight {
public ConcreteFlyweight(String extrinsic) {
super(extrinsic);
}
public void operate(int extrinsic) {
System.out.println("具体Flyweight:" + extrinsic);
}
}
public class FlyweightFactory {
private static HashMap<String, Flyweight> pool = new HashMap<>();
public static Flyweight getFlyweight(String extrinsic) {
Flyweight flyweight = null;
if(pool.containsKey(extrinsic)) {
flyweight = pool.get(extrinsic);
System.out.print("已有 " + extrinsic + " 直接从池中取---->");
} else {
flyweight = new ConcreteFlyweight(extrinsic);
pool.put(extrinsic, flyweight);
System.out.print("创建 " + extrinsic + " 并从池中取出---->");
}
return flyweight;
}
public static int getCount(){
return pool.size();
}
}
- Agent mode (Proxy)
- Can be divided into three categories: static agents, dynamic proxies, Cglib agent (Code Generation Library) subclass Agent)
- VS decorator pattern:
- Proxy mode, the relationship between the agent and the real object is usually at compile time has been determined,
- Recursively decorator can be configured at runtime
Origin www.cnblogs.com/kintanx/p/11444165.html