1. facade pattern
1.1 schema definition facade
Also known as skin mode, provides a unified interface, the interface used to access a group subsystem. Its main feature is the definition of a high-level interface that allows the subsystem easier to use, are structural pattern.
Facade mode scenario 1.2
1.2.1 subsystem more complex, increasing the facade pattern provides a simple interface
1.2.2 Construction of a multi-layer system structure by the object as each entry facade, simplifying inter layer calls.
1.3 Facade pattern common wording
Facade pattern comprises two major roles: a unified interface (Facade role in the Facade class diagram), the role of the subsystem (class diagram SubSystemA, SubSystemB, SubSystemC)
1.4 facade model business scenarios examples
public class GiftInfo { private String name; public GiftInfo(String name) { this.name = name; } public String getName() { return name; } } // subsystems role public class PaymentService { public boolean pay(GiftInfo giftInfo){ System.out.println ( "Deductions" + giftInfo.getName () + "successful integration" ); return to true ; } } // subsystems role public class QualifyService { public boolean isAvailable(GiftInfo giftInfo){ System.out.println ( "check" + giftInfo.getName () + "integration through inventory through." ); Return to true ; } } // subsystems role public class the ShippingService { public String Delivery (GiftInfo giftInfo) { System.out.println (giftInfo.getName () + "into stream system" ); String shippingNo = "666"; return shippingNo; } } //外观角色 public class FacadeService { private QualifyService qualifyService = new QualifyService(); private PaymentService paymentService = new PaymentService(); private ShippingService shippingService = new ShippingService(); public void exchange(GiftInfo giftInfo){ if(qualifyService.isAvailable(giftInfo)){ if(paymentService.pay(giftInfo)){ String shippingNo = shippingService.delivery(giftInfo); System.out.println ( "the single successful system stream, single stream number is:" + shippingNo); } } } } // test class public class the Test { public static void main(String[] args) { FacadeService facadeService = new FacadeService(); GiftInfo GiftInfo = new new GiftInfo ( "" 5 core principles of the Spring "" ); facadeService.exchange(giftInfo); } }
The test results show
Check "Spring 5 core principle of" integration through inventory through.
Deductions "Spring 5 core principle of" integration success
"Spring 5 core principles" into the logistics system
Under a single successful logistics system, logistics is a single number: 666
1.5 Application Mode in the source code Facade
JdbcUtils under the category 1.5.1Spring JDBC module that encapsulates all operations and associated JDBC
Configuration class 1.5.2MyBatis. It is the beginning of which there are many new ways
1.5.3Tomcat RequestFacade the source class that encapsulates the request operation is very much, but also the integration of some of the elements other than many servlet-api, to users provides great convenience. Similarly, Tomcat Response to Session and also when the package
ResponseFacade and StandardSessionFacade class.
Facade pattern of strengths and weaknesses 1.6
1.6.1 Advantages:
1, simplifying the calling process without understanding subsystems, to prevent risks to the subsystem.
2, the system to reduce the dependency, loosely coupled
3, better division level of access, improves security
4, following the Law of Demeter, that is the least known principles.
1.6.2 Disadvantages:
1, when the increase and expansion subsystem subsystem behavior could easily bring unknown risks
2, does not comply with the principle of opening and closing
3, in some cases may be contrary to the principle of single responsibility.
2. Decorator
2.1 Definitions
Without changing the original object, with the function attached to the object, providing more flexible than alternative succession (expansion of the original object function), belonging to the structure schema . The core decorator pattern is extensions . The decorator pattern can be transparent and dynamically extend functions of the class.
FIG universal decorator UML:
As shown above, the decorator includes four roles:
1) abstract component (Component): it may be an interface or abstract class, which serves as the original decorative objects, provides the behavior of the object to be decorated;
2) specific components (ConcreteComponent): to achieve a particular subject or succession of Component, i.e. also decorative objects;
3) Abstract decorators (Decorator):.. Common decorative ConcreteComponent decorative internal configuration comprising:
a. a property points abstraction component Component
B. Such ships is an abstract class, subclass primarily to let pass a form configured according to a Component abstract component, which is common behavior mandatory.
. c Specific Decorator (ConcreteDecorator): Decorator concrete implementation class, in theory, each ConcreteDecorator have expanded functionality Component a subject;
Abstract decorator Source:
public abstract class Decorator extends Component { /** * Component Object holders */ protected Component component; /** * Constructor, passing Component Object * @param component 组件对象 */ public Decorator(Component component) { this.component = component; } public void Operation () { // forward the request to component objects, additional actions may be performed before and after the forwarding component.operation (); } }
Test category Source:
public class Client{ public static void main(String[] args){ C1 the Component = new new ConcreteComponent (); // First create the original object to be decorated (that is, to be decorative objects) Decorator decoratorA = new new ConcreteDecoratorA (c1); // to increase the functionality of transparent objects A and call decoratorA .operation ( ); DecoratorB the Decorator = new new ConcreteDecoratorB (C1); // to increase the functionality of transparent objects and calls B decoratorB .operation (); DecoratorBandA the Decorator = new new ConcreteDecoratorB (decoratorA); // decoration may also be decorative concrete decorative objects, in this case corresponds to the subject function on the basis of the increase in the added function A B decoratorBandA.operation (); } }
2.2 Application of decorative mode in the source code
2.2.1 the JDK classes embodied in the most obvious is the IO related classes, such as . BufferedReader, InputStream, OutputStream class diagram as follows:
TransactionAwareCacheDecorator class 2.2.2Spring. This class is mainly used for transaction cache.
public class TransactionAwareCacheDecorator implements Cache { private final Cache targetCache; public TransactionAwareCacheDecorator(Cache targetCache) { Assert.notNull(targetCache, "Target Cache must not be null"); this.targetCache = targetCache; }p ublic Cache getTargetCache() { return this.targetCache; } ... }
2.2.3 The MVC Decorator HttpHeadResponseDecorator class
2.3 Decorative proxy mode and a comparison mode
2.3.1 decorator pattern is actually a special agent mode
2.3.2 Decorator stressed expand their capabilities, focusing on Functional changes
2.3.3 Proxy model emphasizes control of the proxy process
2.4 decorator pattern of strengths and weaknesses
2.4.1 Advantages:
1) decorator is a strong complement to inherit, inheritance flexible than, the situation does not change the original objects dynamically next to an object extensions that plug and play.
2) By using different combinations and permutation of these decorative decoration may achieve different effects.
3) decorator fully comply with the principle of opening and closing
2.4.2 shortcomings
1) there will be more code, more classes, increasing program complexity.
2) dynamic decoration, more complex multilayer decorative.