Antecedents feed

As mentioned in the last video, Xiaoguang (using the prototype model) copied the model of the Optical Valley store and successfully opened the Chuangye Street branch.

Now the two branches are doing well, light leisure, and fell into the thinking (thinking is a good habit). Thinking, this time to open branches, I am completely clone the optical Valley store that set, and then modify some attributes (such as branch name and so on). However, after the development of small light hot dry noodles, will open a lot of branches, and no branch may not only the name is not the same, there are some such as the address on the drink cup will not be the same, clone up is very convenient, but in case which forgot to change the attribute, it is in trouble.

Thought of, small light thought, it is time to develop a set of standard procedures/ways to open branches.

All sample source code has been uploaded to Github, poke here

What do the branch stores have

According to the existing experience, Xiao Guang quickly summed up the current way to open a hot and dry noodle shop including a series of things:

  1. The store
  2. checkstand
  3. Glass tableware
  4. Hot and dry noodle process
  5. Beverage machine routine
  6. Active policy
  7. .

For the sake of distinction, let’s take the first three examples.

Establish a standard way to open a store

Set standards for everything

In the spirit of good interface-oriented programming thinking, Light has routinely made some standards for these things (interfaces).

Appearance:

public interface Store {

    / / address
    String getAddress(a);

    / / store name
    String getName(a);
}Copy the code

The register:

public interface Checkstand {

    // Bank account
    String getAccount(a);
}Copy the code
public interface Tableware {

    / / label
    String getLabel(a);
}Copy the code

Unified configuration production

Next, the light to do is to think about unified standards for each branch store, not only to avoid opening branches don’t miss what, but also according to the configuration of different branches out of different branches.

The criteria for opening branches are as follows:

public interface CompanyFactory {

    Store createStore(a);

    Checkstand createCheckstand(a);

    Tableware createTableware(a);
}Copy the code

Open a store this way

All right, let’s try to open the SBI Venture Street branch using this standard formula.

Store, Checkstand and Tableware:

public class SbiStore implements Store {

    @Override
    public String getAddress(a) {
        return Guanshan Chuangye Street;
    }

    @Override
    public String getName(a) {
        return "The SBI shop"; }}public class SbiCheckStand implements Checkstand {

    @Override
    public String getAccount(a) {
        return China Merchants Bank :620123131231233; }}public class SbiTableware implements Tableware {
    @Override
    public String getLabel(a) {
        return "SBI"; }}Copy the code

Then, implement an SBI proprietary factory to generate these things:

public class SbiCompanyFactory implements CompanyFactory {
    @Override
    public Store createStore(a) {
        return new SbiStore();
    }

    @Override
    public Checkstand createCheckstand(a) {
        return new SbiCheckStand();
    }

    @Override
    public Tableware createTableware(a) {
        return newSbiTableware(); }}Copy the code

Let’s build a branch using what SBI factory generates:

public class XiaoGuang {

    public static void main(String[] args) {

        CompanyFactory factory = new SbiCompanyFactory();

        // Build a branch based on what factory produces:
        Company sbiCompany = newCompany(factory.createStore(), factory.createCheckstand(), factory.createTableware()); System.out.println(sbiCompany); }}Copy the code

Results:

Branch {address: Guanshan Chuangye Street, name :SBI store, cashier Account: China Merchants Bank:620123131231233SBI}Copy the code

It’s living up to everyone’s expectations. It’s SBI.

In the future, we can open branches in this way

For example, if you want to open a Huashan Software New City store, follow the above way to open chuangye Street store:

  1. Realize HuashanStore of HuashanStore.
  2. Realize the HuashanCheckStand at the cash register of huashan store.
  3. Realize huashan store tableware HuashanTableware.
  4. Realize the HuashanCompanyFactory that produces these things in huashan store.

The code is not a paste, the complete code stamp here.

Here’s how to open huashan branch:

// Create Huashan Factory
factory = new HuashanCompanyFactory();

// Build huashan shop based on what factory produces:
Company huashanCompany = new Company(factory.createStore(), factory.createCheckstand(), factory.createTableware());

System.out.println(huashanCompany);Copy the code

Results:

Branch {Address: Huashan Software New City, Name: Huashan Store, cashier Account: China Construction Bank:62610000000000HS}Copy the code

Ok, the huashan store is finished.

After the story

In the establishment of the standard to create branches, light has always wanted to do is: to determine the branch of the opening standards, to avoid modifying the original branch products, convenient expansion opened a new branch.

This is actually the idea of the open-closed principle that we’ve talked about many times, that is, closed for modification (not changing the existing ones) and open for extension (easily extending new products/instances).

Convention, let’s sort out the relationship between classes:

That’s right, that’s the abstract factory pattern.

Abstract factories provide an interface to create a series of related or interdependent objects without specifying their concrete implementations.

The CompanyFactory interface, in this case, creates a series of related objects (Store, Checkstand, Tableware) that are not implementable (also interfaces). Specific object instances are created by HuashanCompanyFactory, SbiCompanyFactory that implements the CompanyFactory interface.

Extended Reading 1

So far, with this in mind, we’ve mentioned three kinds of factories:

  1. Small light hot dry noodles provide drinks – simple factory
  2. Kwong’s drinks have been upgraded – factory method
  3. It’s time to come up with a set of standards for opening branches – abstract factories

So what’s the difference between the three?

Let’s first compare the class diagram of the three:

A simple factory is actually a programming habit that puts initialization of similar objects in one place for easy management. It provides a factory (cousin) to produce different beverage products (orange juice, Cola, plum soup) according to different commands (drinkType). Relatively simple, suitable for situations where you want to create similar products (implementing the same interface) with a small number of products and little possibility of extension. When adding a drink, we need to modify the factory (cousin) implementation to add the corresponding drinkType implementation.

Factory method As the name implies, there is a factory, and in the factory there is a method (makeDrink, which defines an interface to create objects) that can produce a product (Drink). The class that implements the factory method decides what product to produce (cola, orange juice, milk tea, etc.).

Compared with the simple factory, the factory method has good scalability. When we need to add a beverage, there is no need to modify the factory. We just need to expand a new factory, implement its factory method, and provide a new beverage.

This is, in effect, typically closed to modification and open to extension through inheritance/implementation.

In addition, from simple factory to factory method, we can also understand as a refactoring of Switch Statements.

For refactoring of Switch Statements, see section 3.10 of Refactoring – Improving the Design of existing Code. It is a good book, recommended to me by a technical manager of Huawei in 2010, thanks to him.

In addition, it is not that we should think about transformation when we encounter Switch in the future, or use factory method when we encounter simple factories… Also need to use the appropriate according to the actual situation.

Abstract Factory Also, from the name, we know roughly that abstract factory describes an abstract factory that produces a series of related or interdependent products.

Abstract factories and factory methods have a lot in common in creating products that are closed to modification and open to extension through inheritance/implementation.

The point of abstract factories, however, as opposed to the factory approach, is that they solve the problem of creating a family of products (related or interdependent products) rather than just a class of products.

In the case of this story, the factory method is used to create a class of products through which beverages are created. The abstract factory is used to create a series of products, including shops, cash registers, tableware and so on, which are related and all needed for a branch.

For example, if I have a tire factory, and EVERYTHING I produce is tires, just in different sizes, I can use the factory method; If I’m a car factory and I produce cars, it needs tires, frames, engines… So I’m going to use an abstract factory.

Extended Reading 2

The prototype model we talked about earlier, the prototype factory, is often used in conjunction with the factory. So what kind of factory does it work with? Yes, what we call abstract factories today.

Why is that? Let’s go back and see if we created a lot of objects when we created the abstract factory. Yes, an abstract factory that produces a large family of products will find that we will create many, many objects (products), and these objects may sometimes be the same, may be very similar.

For example, our branch factory has created Checkstand, which may be the same in different branches. Slight differences may only be caused by different accounts, so we do not need to create a new one.

Recall the use of prototype mode – using clone mode to quickly create a new object instance (consistent with the prototype object instance). This approach saves us from having to repeatedly create complex things.

While our current Checkstand creation may be relatively simple, clone could be useful if Checkstand is complex enough.

Specific implementation, we can privately to transform the example code, fusion under the prototype mode, experience.


OK, create a standard to open branches, small light thinking, after opening branches are not my own… Ha ha.