Design Pattern learning-Abstarct Factory
What
Abstarct Factory: provides an interface for creating a series of related or mutually dependent interfaces without specifying their specific classes.
Why
Abstarct Factory is a type of creation design pattern, which is mainly decoupled when an object is created to avoid direct dependency on the object and facilitate replacement and customization. For example, if a function has two different styles and needs to be switched Based on the configuration, or in a program that needs to be applied to multiple database switches, both use Abstact Factory.
Applicable to Abstarct Factory scenarios:
1. When the system display or function needs to be configurable.
2. Create a system module, which must be independent of the system module.
3. When the system needs to be dynamically customized.
How
In the following scenario, when writing a database access layer, you must support switching between two types of databases. For example, you can switch between sqlserver and mysql. This simple example illustrates the implementation of Abstarct Factory.
First, we define our dao interface:
public interface UserDao { void insert(User user); void delete(String id); User find(String id);}
This interface requires three simple methods: insert, delete, and query.
Define the factory interface for creating Dao instances
public interface DaoFactory { UserDao create();}
Implementation of UserDao used to access sqlserver
public class SqlServerUserDaoImpl implements UserDao { @Override public void insert(User user) { System.out.println("sqlserver insert user"); } @Override public void delete(String id) { System.out.println("sqlserver delete user"); } @Override public User find(String id) { System.out.println("sqlserver find user"); return null; }}
Implementation of UserDao for accessing mysql
public class MysqlUserDaoImpl implements UserDao { @Override public void insert(User user) { System.out.println("mysql insert user"); } @Override public void delete(String id) { System.out.println("mysql delete user"); } @Override public User find(String id) { System.out.println("mysql find user"); return null; }}
The factory used to create sqlserver userDao
public class SqlserverDaoFactoryImpl implements DaoFactory { @Override public UserDao create() { return new SqlServerUserDaoImpl(); }}
The factory used to create mysql userDao
public class MysqlDaoFactoryImpl implements DaoFactory { @Override public UserDao create() { return new MysqlUserDaoImpl(); }}
Client call Method
public class App { public static void main( String[] args ){ DaoFactory daoFactory=new MysqlDaoFactoryImpl(); UserDao userDao=daoFactory.create(); userDao.insert(null); DaoFactory daoFactory1=new SqlserverDaoFactoryImpl(); UserDao userDao1=daoFactory1.create(); userDao1.delete(""); }}
The class diagram of the above instance is as follows:
Discuss
In the above example, you can also write the database selection in the configuration file, and load different programs according to the configuration during system startup through reflection, this was previously used when c # was used as a system. In java web development, the spring framework is generally used. It provides IOC technology to initialize data sources by configuring beans.
Abstract Factory is also used in spring source code, such as BeanFactZ? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnm + cores/cores + wyrG3os/Wsb692sq + wP2yu7m7zerJxqOsy/cores + LHwus3Bqs + cores + Csq + wP20 + sLru/ nT2tLUyc/KvsD90N64xKOsyOe5 + 8DtveLBy8q + California/California = "http://www.2cto.com/uploadfile/Collfiles/20140801/20140801085425168.png" alt = "">