Creating DAOs using the Factory design pattern in Spring

As we know, there are a lot of design patterns that play a role in the Spring Framework. As discussed in Chapter 2, Overview of GOF Design Patterns--Core Design patterns, the Factory pattern is a creational design pattern, and it is used to create an object without exposing the underlying logic to the client, and to assign a new object to the caller using a common interface or abstract class. You can make the DAO pattern highly flexible by using the Factory method and Abstract Factory design patterns.

Let's see in our example where do we are implementing this strategy in which a factory produces the DAOs for a single database implementation. Please refer to the following diagram:

You can see in the preceding diagram that the AccountDao object is produced by AccountDaoFactory, and AccountDaoFactory is a factory for AccountDao. We can change the underlying database at any time such that we do not need to change the business code--the factory takes care of these things, Spring provides support to maintain all the DAOs in the bean factory and in the factory for DAOs as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset