I. decorator Mode
In the modifier mode, a class is redecorated without changing the original functions, so that it has more powerful functions. It is described as "icing on the cake" with an idiom ".
Class Structure:
Component: an abstract Component that defines a set of abstract interfaces and specifies the functions of the decorated Component.
ComponentImpl: Abstract component implementation class, complete basic function implementation
Decorator: Decorator role, which holds instance references of Component. It is a bit recursive.
Pseudocode:
Component c=new ComponentImpl();Decorator d1=new Decorator();d1.setComponent(c);Decorator d2=new Decorator();d2.setComponent(d1);Decorator d3=new Decorator();d3.setComponent(d2);Decorator d4=new Decorator();d4.setComponent(d3);d4.methodA();
The modifier mode is similar to the adapter mode. wrapper both has a class, but its contents are different. The adapter mode converts an interface to another interface to achieve matching. The modifier mode does not change the original interface. Instead, it changes the processing method of the original object and uses recursion to improve performance.
Ii. Proxy Mode
Proxy mode, which provides a proxy for other objects to control access to this object.
Class Structure:
Subject: interface class, which defines some interface methods that require proxy
RealSubject: Implementation class
ProxySubject: A proxy class that saves a Subject reference and can inject a specific subclass such as RealSubject.
The proxy mode is to introduce a certain degree of indirect property when operating the object. This indirect nature can add many additional operations. For example, permission control and parameter verification
Interface Class
/*** Personal information management ** @ author onlyone */public interface PersonManager {/*** query the salary ** @ param name: name of the person to be found * @ param operateName Operator name */ public double getSalary (String name, string operateName );}
Implementation class
/*** Specific implementation class ** @ author onlyone */public class RealPersonManager implements PersonManager {@ Override public double getSalary (String name, String operateName) {// query the database, returns the actual salary. return 1000000 ;}}
Proxy
/*** Proxy class ** @ author onlyone */public class ProxyPersonManager implements PersonManager {// interface reference PersonManager realPersonManager = new RealPersonManager (); @ Override public double getSalary (String name, string operateName) {// 1. add some permission judgments. For example, whether the operator has the permission to query a person's salary. // 2. Call return realPersonManager. getSalary (name, operateName) for a specific class );}}