Internet line Java Kong face questions: Spring with what design patterns? Thinking Deeply Initiated

Internet line Java Kong face questions: Spring with what design patterns? Thinking Deeply Initiated



Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Foreword

Design Patterns as a pillow book work and study, but often in Qin said no awkward position, nor is it we often forget, but has no memory. Spring framework as a classic industry, both in terms of design architecture, or in terms of coding, it is called inline model.

Well, I did not talk much, start today's content. spring commonly used design patterns to achieve nine, at our example.

After no longer afraid of the interviewer asked me: Spring with what a design pattern.

Simple factory pattern

Also known as static factory method (StaticFactory Method) mode, but does not belong to one of 23 kinds of GOF 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 in the BeanFactory is the embodiment of simple factory pattern to obtain a bean object from the passed unique identifier, whether but after passing parameters to create or passed to create this situation should be based on specific parameters before. The following configuration is to create a itxxzBean in HelloItxxz class.


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Factory Method pattern

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 the bean. If the application is subject to the Spring own factory management, the management of Spring is not common bean, but the factory Bean.

Take the factory method Static method example to explain this:


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Config.xm build a configuration file will be incorporated into the Spring container to manage, you need to specify the name of the static method by factory-method:


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



test:


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Singleton

To ensure that a class has only one instance and provide a global access point to access it.

After a single spring embodiment mode, a half of the sentence is completed, i.e., provides global access point BeanFactory. But no single embodiment to control the level of the builder, since spring management is arbitrary java object.

Core Tip point: default next Spring bean are singleton, by singleton = "true | false" or to specify scope = "?."

适配器模式

在Spring的Aop中,使用的Advice(通知)来增强被代理类的功能。Spring实现这一AOP功能的原理就使用代理模式(1、JDK动态代理。2、CGLib字节码生成技术代理。)对类进行方法级别的切面增强,即,生成被代理类的代理类, 并在代理类的方法前,设置拦截器,通过执行拦截器重的内容增强了代理方法的功能,实现的面向切面编程。

Adapter类接口:Target


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



包装器模式

在我们的项目中遇到这样一个问题:我们的项目需要连接多个数据库,而且不同的客户在每次访问中根据需要会去访问不同的数据库。我们以往在spring和hibernate框架中总是配置一个数据源,因而sessionFactory的dataSource属性总是指向这个数据源并且恒定不变,所有DAO在使用sessionFactory的时候都是通过这个数据源访问数据库。

但是现在,由于项目的需要,我们的DAO在访问sessionFactory的时候都不得不在多个数据源中不断切换,问题就出现了:如何让sessionFactory在执行数据持久化的时候,根据客户的需求能够动态切换不同的数据源?我们能不能在spring的框架下通过少量修改得到解决?是否有什么设计模式可以利用呢?

首先想到在spring的applicationContext中配置所有的dataSource。这些dataSource可能是各种不同类型的,比如不同的数据库:Oracle、SQL Server、MySQL等,也可能是不同的数据源:比如apache 提供的org.apache.commons.dbcp.BasicDataSource、spring提供的org.springframework.jndi.JndiObjectFactoryBean等。然后sessionFactory根据客户的每次请求,将dataSource属性设置成不同的数据源,以到达切换数据源的目的。

spring中用到的包装器模式在类名上有两种表现:一种是类名中含有Wrapper,另一种是类名中含有Decorator。基本上都是动态地给一个对象添加一些额外的职责。

代理模式

为其他对象提供一种代理以控制对这个对象的访问。 从结构上来看和Decorator模式类似,但Proxy是控制,更像是一种对功能的限制,而Decorator是增加职责。

spring的Proxy模式在aop中有体现,比如JdkDynamicAopProxy和Cglib2AopProxy。

观察者模式

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。

spring中Observer模式常用的地方是listener的实现。如ApplicationListener。

策略模式

定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。

spring中在实例化对象的时候用到Strategy模式

在SimpleInstantiationStrategy中有如下代码说明了策略模式的使用情况:


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



模板方法模式

定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

Template Method模式一般是需要继承的。这里想要探讨另一种对Template Method的理解。spring中的JdbcTemplate,在用这个类时并不想去继承这个类,因为这个类的方法太多,但是我们还是想用到JdbcTemplate已有的稳定的、公用的数据库连接,那么我们怎么办呢?我们可以把变化的东西抽出来作为一个参数传入JdbcTemplate的方法中。但是变化的东西是一段代码,而且这段代码会用到JdbcTemplate中的变量。怎么办?那我们就用回调对象吧。

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 may be Template Method does not need to inherit another implementation.

The following is a specific example:

The execute method JdbcTemplate

JdbcTemplate execute method execute


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated



Spring Learning Brain Mapping


Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated


Readers Benefits:

Thank you for watching, I hope you can give a clearer picture. In addition, recent private letter I have a lot of friends told me to prepare for the spring strokes, and asked me if I had tidied interview to share documents, compiled a specially today face questions information to everyone, I hope you interview well. Need self-created data

Receive way: My concern for the kind of numbers of people to access our FREE Java Zhou

Internet line Java Kong face questions: Spring with what design patterns?  Thinking Deeply Initiated


Guess you like

Origin blog.51cto.com/14456091/2477126