Design Pattern learning-Abstarct Factory

Source: Internet
Author: User
Tags mysql delete mysql insert
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. For example, beanfactory is an example. Of course, its design is much more complicated than the example in this article.

Changelog

The examples in this section are not perfect, so they are improved. In changelog, the difference and connection between the two are added to the discuss chapter of the factory method in design mode learning.
The sample code is modified based on the above example. If you understand the code in the sample, the Code modified to the following structure is relatively simple and will not be posted. You can also download it from my GitHub.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.