This is my third article about getting started

concept

Abstract factories allow customers to use abstract interfaces to create a set of related products, regardless of what is actually produced. In this way, customers can be decoupled from specific products.

Both the Abstract factory pattern and the factory method pattern do a good job of decoupling object creation from code, but the two patterns are very different in nature. Abstract factories are better suited to situations where there are multiple product families.

For example,

The book continues.

Before, our newly opened cake shop received a good response in the opening days, attracting a large number of customers; Therefore, in order to enhance user stickiness and customer loyalty to the brand, we have done a lot of market research. In the end, we decided to launch other varieties with high competitiveness in the local market while ensuring the output quality of original pure cakes.

After a period of operation, your cake shop has become quite famous in the local area. You know, it’s time to take the next step.

As a programmer, you know the art of inwrapping. In the local market, cake is already saturated, so how to grab market share from competitors?

First, you realize that money has to be sufficient. You got investment from a former co-worker — financial freedom, a midlife crisis — and it turned out to be quite a success.

Externally, you plan to open a chain store in various areas, such as the East side and the West side. You know, location is very important, to cover the city while suppressing the living space of competitors. Then, a lot of advertising, quickly improve the visibility in the local. Then there is the price war, give customers a lot of concessions, burn money, in order to monopolize the local market, become the leader of the local or even larger area, capital is not a problem.

Internally, you raise your employees’ salaries and benefits, volunteer to work, and you can get much higher wages than your peers. As a conscientious enterprise, you certainly do not encourage overtime. The enterprise management can not be left behind, corporate culture and KPI, coupled with the elimination optimization mechanism at the end of the internal competition, well maintained the fighting strength of the whole team. Of course, you also agree to join other local shops. After a period of time, some cake shops could not withstand the pressure of burning money to choose to join.

.

After a period of hard work, you successfully open stores in different cities. But after a while, you find that the taste of cakes made by franchise stores is very different from that of direct stores, and many customers respond that cakes are very unpalatable. Originally, there are some franchisees, the use of low-cost raw materials to increase profits. You must do something to avoid ruining the sign. How to ensure that each franchise uses high quality ingredients? You are going to build a factory that will produce the ingredients and deliver them to the franchise stores. Ok, let’s do it:

The raw material factory

Now define an interface for the factory that is responsible for all raw material selection.

public interface CakeMaterialFactory {
    public Flour createFlour(a);
    public Egg selectEgg(a);
    public Milk createMilk(a);
    public Sugar createSugar(a);
}

Copy the code

Raw Material Plant (Area A1)

This is a raw material factory in A1 area. When choosing materials, we should not only consider the cost, but also the taste preference of local people

public class A1CakeMaterialFactory implements CakeMaterialFactory {

    public Flour createFlour(a) {
        return new LowGlutenFlour();
    }
    
    public Egg selectEgg(a) {
        return new NativeEgg();
    }
    
    public Milk createMilk(a) {
        return new SweetMilk();
    }
    
    public Sugar createSugar(a) {
        return newCasterSugar(); }}Copy the code

Factories in other regions are similar

Cake class

Next, we use high-quality ingredients from the factory

public abstract class Cake {
    String name;
    // The following are some of the ingredients for cake making
    Flour flour;
    Egg egg;
    Milk milk;
    Sugar sugar;
    
    // To declare the method of material selection abstract, the cake is made of materials from our raw material factory
    abstract void materialSelection(a);
}
Copy the code

Pure cake

public class PlainCake extends Cake {

    CakeMaterialFactory factory;
    
    public PlainCake(CakeMaterialFactory factory) {
        this.factory = factory;
    }
    
    void materialSelection(a) {
        System.out.println("Pure cake material acquisition"); flour = factory.createFlour(); egg = factory.selectEgg(); milk = factory.createMilk(); sugar = factory.createSugar(); }}Copy the code

There are other types of cakes, but they are similar in principle.

The raw material

/ / flour
public interface Flour {}/ / egg
public interface Egg {}/ / milk
public interface Milk {}/ / the sugar
public interface Sugar {}// Low gluten flour
public class LowGlutenFlour implements Flour {}// Local eggs
public class NativeEgg implements Egg {}/ / sweet milk
public class SweetMilk implements Milk {}/ / sugar
public class CasterSugar implements Sugar {}Copy the code

Let’s take a look at the class diagram to get an idea of the structure of this pattern:

classDiagram Cake <|-- PlainCake Cake <|-- SpongeCake CakeMaterialFactory <|.. A1CakeMaterialFactory CakeMaterialFactory <|.. B6CakeMaterialFactory Cake : + String name Cake : + Flour flour Cake : + Egg egg Cake : + Milk milk Cake : + Sugar sugar CakeMaterialFactory : + createFlour() CakeMaterialFactory : + selectEgg() CakeMaterialFactory : + createMilk() CakeMaterialFactory : + createSugar() interface CakeMaterialFactory class A1CakeMaterialFactory { + createFlour() + selectEgg() + createMilk()  + createSugar() } class B6CakeMaterialFactory { + createFlour() + selectEgg() + createMilk() + createSugar() } class PlainCake { +String name +materialSelection() } class SpongeCake { +String name +materialSelection() }
classDiagram
Flour <|.. LowGlutenFlour
Egg <|.. NativeEgg
Milk <|.. SweetMilk
Sugar <|.. CasterSugar
interface CakeMaterialFactory
interface Flour
interface Egg
interface Milk
interface Sugar

As you can see, each method of the abstract factory actually looks like a factory method, and each method within the interface is responsible for creating a concrete product, which we implement by subclassing the abstract factory.

The Abstract factory pattern provides an interface for creating families of related or dependent objects without explicitly specifying concrete classes

OK, here we have solved the problem of raw materials.

Is our bakery ready to go public next? Maybe…

That’s the end of the cake shop story.