spring source of design patterns

Turn: https://blog.csdn.net/huyang0304/article/details/82928900

We'll just introduced in the Spring of common design patterns.

1.1, a simple factory pattern (Factory)

Scenario: also known as static factory method (StaticFactoryMethod) mode, but does not belong to one of 23 design patterns. The essence of simple factory pattern is a class factory according to the parameters passed, the dynamic should decide which class to create a product.

Spring BeanFactory is reflected in the simple factory pattern, according to Bean object passed to obtain a unique identifier, whether but after passing parameters to create or passed to create this situation should be based on specific parameters before.

Classify

Feature

Exhaustive

Create a schema

It is a complex mental model of the factory model

Mass production, standardization

1.2, factory method model (FactoryMethod)

Scenario: typically used by new applications to directly create new objects, in order to create and use objects of phase separation, the use of the factory model, that application will create and initialize the object's responsibilities to the factory object.

Under normal circumstances, the application has its own factory object to create Bean. If the application is subject to the Spring own factory management, then Spring

Management is not an ordinary Bean, but the factory Bean.

Classify

Feature

Exhaustive

Create a schema

For the caller, hiding the complexity of the logic process, the caller only care about the results.

For the plant is responsible for the results, ensure the production of compliant products.

Assembly line production

1.3, singleton pattern (the Singleton)

Scenario: ensure a class has only one instance and provide a global access point to access it.

Spring singleton pattern after completion of the half of the sentence, i.e., it provides global access point BeanFactory. But not to control the level of a single case from the builder, because management is Spring is arbitrary Java objects. Default next Spring Bean are single cases.

Classify

Feature

Exhaustive

Create a schema

Guarantee from the system to boot the system terminates the entire process will only produce one instance.

When we encounter conflicts in functional applications, it requires the use of the Singleton pattern.

Profile, calendar, OC container

 

Common singleton pattern written: starving type, lazy type, register type, serialization.

1.4, the prototype model (Prototype)

Scenario: create another prototype model is a customizable object from an object, and does not need to know details of any created.

The so-called prototype model, is cloning technology in Java to an object as the prototype. Copy the new object. Apparently the new object have the characteristics of the prototype object, high efficiency (to avoid the re-construction process execution step).

Classify

Feature

Exhaustive

Create a schema

First, there is a prototype.

The same data content, but a different object instances (two complete individuals).

Monkey blowing vellus

1.5, proxy mode (Proxy)

Scenario: providing a proxy to control access to the object to other objects. Structurally Decorator pattern and the like, but is controlled Proxy, more like a restriction on the function, and increase duty Decorator.

Spring modes are embodied in the Proxy AOP, such JdkDynamicAopProxy and Cglib2AopProxy.

Classify

Feature

Exhaustive

Structural model

Performer, the principal

For the principal, this thing must be done, but I do not want to do or do not have time to do.

For agents, the need to get people to be agents of personal data, only one or a few aspects involved in the whole process.

Rental agency, ticket scalper, matchmaking, brokers, courier, Acting, non-invasive monitor log

1.6 Strategy Mode (Strategy)

Scenario: define a set of algorithms, encapsulate them one by one, and makes them interchangeable. This mode can be alone makes the algorithm

Stand varies clients that use it.

Spring used in Strategy mode when the object is instantiated, it is used in the SimpleInstantiationStrategy.

Classify

Feature

Exhaustive

Behavioral patterns

The final results of the implementation is fixed.

Execution and implementation of logic is not the same.

Tourist travel mode

1.7, the template method pattern (TemplateMethod)

The algorithm defines a skeleton of the operation, some steps to subclasses delay. TemplateMethod such subclasses may not change the structure of certain steps of an algorithm of the algorithm to redefine.

TemplateMethod mode of inheritance is generally required. Here we want to explore another understanding of the TemplateMethod. Spring in JdbcTemplate, does not want to extend this class when using this class, because too many methods of this class, but we still want to use JdbcTemplate

It has been stable, the public database connection, so how do we do? We can change things out of ways to JdbcTemplate passed as a parameter. But things are changing a code, and the code will be used in JdbcTemplate variables. How to do? Then we use the callback object bar. Define a method JdbcTemplate manipulated variable in this callback object, we have to implement this method, put the change of focus to something here. Then we pass this callback object to JdbcTemplate, thus completing the call. This is TemplateMethod do not need to inherit another implementation.

Classify

Feature

Exhaustive

Behavioral patterns

Fixed execution flow, but some intermediate steps nuances (when running OK).

Can achieve mass production.

SpringORM data model

1.8, delegate mode (Delegate)

Scenario: not belong to one of 23 design patterns are commonly used in object-oriented design patterns of a pattern. The principle of this mode of class A and class B are two classes without any relationship to each other, A and B having exactly the same methods and properties; and calls Method B, it is to call attributes, and attribute A method with the same name. B A Power of Attorney if that is a subject to intermediaries. Third-party code does not need to know the existence of A, and A does not require direct contact occurred, B can be used directly by the function A, then it will be able to use the various functions of A, and A can be a very good protection up, do both.

Classify

Feature

Exhaustive

Behavioral patterns

To distinguish and proxy mode.

Hold the referenced clients.

We do not care about the process, only care about the results.

Manager to distribute tasks, Dispatcher

1.9 Adapter mode (Adapter)

SpringAOP module BeforeAdvice, AfterAdvice, ThrowsAdvice three kinds of support notification type is actually implemented by means of an adapter mode, this advantage is such that the frame allows the user to add any kind of advice they want to support the framework, the three the notification type is SpringAOP module definition, which is a subtype Advice AOP Alliance defined.

Classify

Feature

Exhaustive

Structural model

Focus on compatibility, conversion.

Fitter and adapted no hierarchical relationship between these, there is no necessary connection.

Meet the has-a relationship.

Codec, a tow three charging head, HDMI turn VGA, Type-C to USB

1.10, Decorator (the Decorator)

Scenario: encountered such a problem in our project: Our project need to connect multiple databases, and different customers in each

Visits as needed will go to access different databases. In our previous Spring and Hibernate always configure a data source, thus SessionFactory DataSource property always point to the data source and constant, all DAO when using SessionFactory is through this data source to access the database. But now, due to the need of the project, we have to constantly switch DAO when accessing a SessionFactory in multiple data sources, the question arises: how to make SessionFactory in the implementation of data persistence, according to customer demand can dynamically switch different data sources? We can not be resolved within the framework of Spring by minor modifications? Are there any design patterns can make use of it?

First, think of all the DataSource configuration in a Spring ApplicationContext. DataSource These may be various types, such as different databases: Oracle, SQLServer, MySQL, etc., may be different data sources: for example org.apache.commons.dbcp.BasicDataSource Apache provided, org.springframework Spring provided .jndi.JndiObjectFactoryBean and so on. The SessionFactory then each request by a client, the DataSource property to various data sources, in order to achieve the purpose of switching the data source.

Spring used in the packaging mode, there are two performance on the class name: one is the class name contains Wrapper, the other is the class name contains Decorator. Basically to an object dynamically add some additional responsibilities.

Classify

Feature

Exhaustive

Structural model

1, focusing on coverage extension.

2, decorators and decorators are both implement the same interface, the main purpose is to expand still retained after OOP relationship (same root).

3, satisfy the relationship of the is-a.

IO streams packaging, packaging data source, packaging CV

1.11, observer mode (Observer)

Scenario: the definition of the dependencies between the subject an-many, when a state of the object changes, all objects that depend on it

We are notified and updated automatically.

Spring in Observer mode to achieve common place is the Listener. As ApplicationListener.
----------------
Disclaimer: This article is the original article CSDN bloggers "yyyyyhu", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/huyang0304/article/details/82928900

Guess you like

Origin www.cnblogs.com/fengli9998/p/11463088.html