Abstract factory pattern in design pattern

Keywords: Java Redis

Abstract factory pattern

Originally, I didn't want to write this article, but I didn't want to write the factory mode, because everyone would, but today I saw an old fellow's blog. https://www.yuxuan66.com/16 As for the realization of the abstract pattern, it was well written. After seeing it several times, I had some ideas of myself and I was familiar with the factory mode again. So I got this article, mainly the abstract factory, and cited the example of the old fellow iron factory: the coffee factory is bigger and stronger, and introduced new kinds of drinks: tea and carbonated drinks. Chinese factories can only make coffee and tea, while American factories can only make coffee and carbonated drinks

Use

I'm here to create an abstract factory. The Chinese factory and the American factory inherit it. Then create a drink interface to realize various kinds of drinks. Create another provider to provide corresponding products according to the factory and type

Create an abstract factory

/**
 * @program:hope
 * @author:aodeng
 * @WeChat public: low key Panda
 * @create:2018-11-22 14:43
 * Abstract factory
 **/
public abstract class AbstractDrinksFactory {
    /***
     *
     * @param whatYouLike Your favorite drink types: coffee, tea, sodas
     * @return
     */
    public abstract Drink createDrink(String whatYouLike);
}

Chinese factory and American factory inherit it

public class ChinaAbstractDrinksFactory extends AbstractDrinksFactory {
    @Override
    public Drink createDrink(String whatYouLike) {
        Drink drink=null;
        if ("coffer".equals(whatYouLike)){
            drink=new CofferDrink();
        }else if ("tea".equals(whatYouLike)){
            drink=new TeaDrink();
        }
        return drink;
    }
}
//
public class USAAbstractDrinksFactory extends AbstractDrinksFactory {
    @Override
    public Drink createDrink(String whatYouLike) {
        Drink drink=null;
        if ("coffer".equals(whatYouLike)){
            drink=new CofferDrink();
        }else if ("sodas".equals(whatYouLike)){
            drink=new SodasDrink();
        }
        return drink;
    }
}

Create another drink interface

//Beverage interface
public interface Drink {
    String getName();
}

Achieve a variety of drinks

//Coffee beverage
public class CofferDrink implements Drink {
    @Override
    public String getName() {
        return "coffer";
    }
}
//Carbonated drinks
public class SodasDrink implements Drink {
    @Override
    public String getName() {
        return "sodas";
    }
}
 // Tea beverage
public class TeaDrink implements Drink {
    @Override
    public String getName() {
        return "tea";
    }
}

Create another provider to provide corresponding products with factories and categories

 * Factory provider
 **/
public class DrinkStore {
    AbstractDrinksFactory abstractDrinksFactory;

    /***
     * Dynamic plant selection
     * @param abstractDrinksFactory
     */
    public DrinkStore(AbstractDrinksFactory abstractDrinksFactory){
        this.abstractDrinksFactory=abstractDrinksFactory;
    }

    /***
     * Production according to product type
     * @param whatYouLike
     * @return
     */
    public Drink createDrink(String whatYouLike){
        return abstractDrinksFactory.createDrink(whatYouLike);
    }
}

Main method test

/**
 * @program:hope
 * @author:aodeng
 * @WeChat public: low key Panda
 * @create:2018-11-22 15:07
 **/
public class Main {
    public static void main(String[] args) {
        //Use factory to provide class to select factory
        DrinkStore store=new DrinkStore(new ChinaAbstractDrinksFactory());
        //Produce drinks according to products
        Drink drink=store.createDrink("tea");
        //Get specific drinks from specific factories
        System.out.println(drink.getName());//Output tea
    }
}

There are some differences between simple factory and abstract factory. In addition to the differences in structure, the main difference lies in the different use scenarios.

**A simple factory is used to create a single product. It centralizes the creation process of all subclasses in one factory. If you want to modify it, you only need to modify one factory. Simple factories are often used together with singleton mode, for example, to create cache objects (file cache) with simple factories. One day, you need to use redis cache to modify factories.
Abstract factories are often used to create an entire product family rather than a single product. The advantage of choosing different factories is that the whole product family can be replaced quickly by replacing factories. For example, the U.S. factory produces the U.S. drive and the Chinese factory produces the Chinese drive.
Advantage
The client is decoupled from the specific product to be created, with high scalability and flexibility**

* disadvantages
When adding objects to be created, more code needs to be added, which will make the system more complex**

This article is released by the low-key panda one article multiple sending operation! Welcome to public address: low key panda.

Posted by tulleh on Fri, 01 Nov 2019 11:05:49 -0700