Design patterns to learn (on)

Written in 2019-11-12

Before the embankment before understanding design patterns:
goal one, the pattern is very important indicator of the serviceability of the program, like a one-time Demo program does not need to how high the maintainability;
2, recognize and acknowledge oriented interface the benefits of programming, does not endorse please look back at 1;
3, design patterns are decoupled nature of the fundamental means of decoupling is layered, the more sub-layers, the more the relationship is not intuitive;
4, do not affect the implementation of design patterns completely demand, just write code much, much reconstruction, read, read, also entered the "stereotype" design pattern.

Create a schema

Simple factory
if not a design pattern is a common encoding mode. The new process for encapsulating an object instance of a simple static factory method together, so that the client side does not depend on the specific implementation class Impl, only need to modify a static method can be implemented when changing class, to achieve the initial decoupling. I think in scenarios, simple factory suitable for use in stationary conditions, such as gender, category up is: male, female, unknown, probability basically no modifications. Further use of reflection, makes scalability more simple plant, for example: Class.forName method.

Factory method
drawback is that when the plant is simple need to add a new implementation class Impl time, simple to modify the static factory method, namely Impl class with this simple factory class couples. Imagine if your simple factory method is used in simple items no problem, if you write a framework, you let the caller how expansion? If I make a different Impl class depends different factories, and that the caller need only to extend the time to add Impl class, plus a corresponding factory class, decided by the caller different factories to produce instance, this is the factory method.

Abstract factory
An obvious disadvantage of factory methods is too much factory class, how to organize these factories merge it? Such as millet production of laptop + mobile phones, Huawei also manufactures laptop + mobile phone, by brand, then it has two factories, one is millet factory, one is Huawei factory. By product, then there are two factories, a mobile phone factory, it is a computer factory. Products are generally considered mobile phones and laptops, while Huawei millet or just an attribute of this product: the brand. So it should be abstract factory plant millet and Huawei factory, two factories there are two methods, namely, the production of notebook computers and mobile phone production.

Singleton
nothing to say, I wrote code that people understand. It is worth mentioning that the spring IOC's controller, service, dao default is singleton.

Prototype model
used is a deep copy everything. It is worth mentioning that, I personally think the object between two different types of the same name field copy can be considered a prototype model of the application, such as BeanUtils.copyProperties. Scenarios are generally formed from a copy of the target object by a prototype in another new object, and then changes in the appropriate fields.

The builder pattern
used Jooq, Mybatis generator or Activiti should be no stranger to the chain calls, in fact, the chain is called with the builder pattern, just as by the responsibilities of Director of Client-side.
For example:. New BaseResponse.Builder (1) .setData (data) .setMessage ( "moext") build ()
application scenario is common, there is a plurality of attributes, some attributes Alternatively, when the overloaded constructor a lot of time can be considered rewritten with the builder pattern, those quick to constructors four above parameters for the caller is very unfriendly.

Structural model

Adapter mode
adapter mode are relatively large with a pattern, such as the most common programming interface to convert between entities DTO Entity database tables, further encoding protocol conversion and the like.

Flyweight pattern
a pattern Flyweight pattern is an enterprise project must be used, various pools, such as thread pool, connection pool are all application metadata model to enjoy.

Proxy mode
comes with the JDK interface generation agent for the proxy class, the dynamic proxy Cglib more directly with bytecode enhancement way to achieve, compared to more consistent agent JDK own proxy mode, the agent can be used in general classes are enhanced proxy classes, external access to the proxy classes directly through a proxy class.

Decorative patterns
decorative pattern and proxy mode is similar in structure, but different purposes, Decorator by the object original packaging, mostly applied ornament chain, such as a blank room by hydropower decorative plaster model, painting mode , soft loading mode, and ultimately achieve the effect of hardcover room, in the process of rough housing objects, hard-mounted room objects, hardcover room objects are accessible, and proxy mode, you can generally only be achieved by accessing the proxy is a proxy object access the effect of the object. The decorator pattern is widely used in BIO. Application of this model in everyday coding requires a combination of modeling, the original model step by step through a decoration, to reach the target model, while at every step of the decorative objects out are accessible. Of course, you love to use process-oriented approach, step by step plus decorative effect does not mean that can not be achieved without design mode does not affect Coding requirements.

Facade pattern
that popular example is the launch vehicle, the driver, it is a method open, and inside, there are power system open, open, open power system fuel system, etc., because the drivers do not care about the interior of the car launch details, he only concerned with the entire car has a unified open method. Aspect Mode GUI programming in direct application of a lot, where appropriate model, in fact, everyday applications also can use awareness, the key lies in the use of the user's point of view or need attention to detail components.

Combination pattern
combination mode must first be abstracted into a tree structure, such as file systems, by classification: directory, subdirectory, file, first need to abstract concept nodes, this node can be a directory, subdirectory or file, and then form a tree structure. Common applications in the tree structure organizational structure, menus, file systems, etc. to represent the hierarchical relationship of parts to the whole.

Bridging mode
is the specific portion and in part by the abstract "bridge" pick up, so that the two parts can be varied independently without affecting each other. When you find the program to be considered X product Y Cartesian cases when you can consider whether to use bridge mode, such as coffee capacity of large, medium, small cup, taste are: flavor, add milk, sugar are three if you inheritance to achieve it, is 3 3 = 9 kinds implementation class. And a bridging mode, the capacity can be as abstract part, implemented as part of the taste. Tastes like coffee category by reference for all tastes reach add to the effect of coffee. A cup of coffee with milk is the big new LargeCoffee (new Milk ()) ; see, taste extra time, only need to add a taste to realize, when adding capacity, and needs to achieve a capacity class, taste and capacity can vary independently. Select the part that choose to implement an abstract part of it under what circumstances and under what circumstances? In general abstract part is relatively stable or latitude is limited objective things, such as electricity supplier companies: new Phone (new HuaWei () ), then (new Phone ()) better than new HuaWei, because this phone objective things relatively brand will be more stable.

Part II describes the behavioral patterns

Guess you like

Origin www.cnblogs.com/mzsg/p/11978028.html