Design Patterns study notes (c) - structural model (Structural Pattern)

Structural model (Structural Pattern)

  1. 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
  2. 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();
  3. 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
  4. Decorator Pattern (Decorator)
    • The wrapper may also be called an alias (the Wrapper), same as the alias adapter mode, but they apply to different situations
    • Decorative pattern was also known as "painters model", it is an object structural pattern
    • Decorative patterns, notably to increase runtime behavior, which is different and inheritance, succession is to increase behavior at compile time
    • Decorative pattern consists of four roles:
      • Abstract member defines the interface objects may be dynamically increased responsibilities (Method) for these objects;
      • DETAILED member defines the specific component object, implements the abstract method declarations in the member, the decoration may give it additional functions (methods);
      • Decorative abstract class is a subclass of abstract class member, particularly to a member for the increased responsibilities, but their specific functions implemented in a subclass;
      • Decorative concrete decorative abstract class is a subclass of class, responsible for adding new responsibilities to members
      //抽象构件
      public abstract class Person {
       public abstract void eat();
      }
      //具体构件
      public class NormalPerson extends Person {
       public void eat() println("吃饭");
      }
      //抽象装饰类
      public class PersonFood extends Person {
       private Person person;
       public PersonFood(Person person){
           this.person = person;
       }
       public void eat() {
           person.eat();
       }
      }
      //具体装饰类
      public class ExpensiveFood extends PersonFood {
       public ExpensiveFood(Person person) {
           super(person);
       }
       public void eat() {
           super.eat();
           eatSteak();
           drinkRedWine();
       }
       public void eatSteak() println("吃牛排");
       public void drinkRedWine() println("喝拉菲");
      }
  5. 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
  6. 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();
        }
    }
  7. 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

Guess you like

Origin www.cnblogs.com/kintanx/p/11444165.html
Recommended