1. Simple factory model

Brother: Is the simple factory model really simple?

A: It’s really simple. Let’s start with an example

XXX kang is a factory, the factory has a partition about mobile phones, respectively are all kinds of mobile phones, as shown in the picture below, the factory and customers to negotiate to produce mobile phones

The emperor came and said: “I want a” brick “mobile phone.

So-and-so kang: identify identity information >>> “brick”, and then the factory internal production of a kind of mobile phone to the emperor.

The minister came, said: “this official wants a” watermelon “mobile phone.

So-and-so kang: identify to identity information >>> “watermelon”, then factory interior production of a kind of mobile phone to the minister.

Brother: Simple factory mode doesn’t seem to work!

A: No and no! Instead of making their own phones, the emperor and his ministers would get their own phones from factories. That is to say, users do not have to manufacture mobile phones, but issue orders to factories to obtain mobile phones, and the factories produce mobile phones internally, separating producers and users and achieving decoupling. Chairman MAO said: “there is no high or low level of work, only different division of labor.” We do it through division of labor. Here we go, here’s a picture kangkang, no picture no truth, no picture noBB

Brother: What if I want to add a new type of mobile phone?

A: This model is used to produce the same type of product. If a new type of product is to be added, not only the new production line of mobile phone should be added, but also the logo of the product should be added in the factory, so that customers can know about the new mobile phone. Brother, isn’t it easy?

Brother:…

Second, factory method mode

Brother: Do you know OOP?

Fortunately, I just read it yesterday. I was pleasantly surprised and answered: OOP is the seven principles.

  1. Open and close principle: external extension, closed to modify
  2. Richter’s substitution principle: Inheritance must ensure that all attribute subclasses used by the superclass are still true
  3. The dependency inversion principle: Program to the interface, not to the implementation
  4. Single responsibility principle: Control the granularity of classes, decouple objects, and improve their cohesion
  5. Interface isolation principle: Create special interfaces for each class that they need
  6. Demeter’s Rule: Talk to your direct friends, but don’t talk to your friends’ friends
  7. The principle of composite reuse: use association relations such as composition or aggregation first, and inheritance relations second.

Brother: Ha ha, I see your seven principles, in the simple factory model used Demeter’s law and dependence inversion principle.

A: You are really a little clever, if you want to go further can also achieve the open and close principle

Brother: Tell me about it?

A: Upgrade the simple factory, first set up a factory representative, and then set up the actual factory around the product, so that the emperor wants a brick phone, then only need to go to the brick phone factory to get the phone.

Similarly, if a minister wants a watermelon phone, all he has to do is go to the corresponding factory and get it.

If, say, a farmer comes in and wants a stone phone, just add a model stone phone, and the manufacturing plant, and you get the phone from the new factory. Here we go. Here’s another picture kangkang, no picture no truth, no picture noBB.

Brother: In this way, the principle of open and close is met, and the new product can be added without modifying the existing code. I’m going to go straight to this.

A: No and no! As a result, the complexity of code, programming, structure and management is obviously improved. Therefore, although the factory method mode is more in line with the design principle, the simple factory mode is still used in most cases in actual production.

Brother: That’s interesting…

To be continued >>> Updated version of abstract Factory Pattern

github