There are feelings, there are dry goods, wechat search [three prince Ao bing] pay attention to the programmer who has a little bit of things.
This article has been included in GitHub github.com/JavaFamily, there are a line of large factory interview complete test sites, information and my series of articles.
preface
I do not know that with the growth of our working years, there is no sense of crisis, afraid of their technical depth began not to improve, so often go to see a bit of the framework of the source, or some online classes to improve their own.
Recently, I had a colleague from my old employer who talked with me about this problem. He said that he had just changed companies, but his code felt “low”. When I was working in CodereView, I was always given various suggestions.
And when I want to learn the depth of technology (look at the source code of a framework), I always feel that I only understand the surface, but I don’t understand the idea inside.
I think you all know what I’m talking about here, and that’s design patterns, and that’s actually what I was thinking in my first year and my second year, why do I study design patterns? What are the advantages of design patterns?
- Improve the ability to view framework source code
- Improve my code design ability and code ability of complex business logic
- Build a solid foundation for the interview and the rest of your career
For example, when I was engaged in e-commerce activities, my partner who was engaged in venue activities used the mode of responsibility chain + factory + singleton. I only needed several types of factories, and then passed the responsibility chain, and it was ok to reuse their singleton. This is the advantage of the design mode, which can be installed with X and benefit future generations.
The body of the
Today I will talk about the factory model, which is very widely used in our e-commerce field. I looked at the code for my computer project before I wrote it, and it was everywhere.
Factory models can be divided into three main categories:
- Simple Factory model
- Factory method pattern
- Abstract engineering model
Today, I will focus on these three models for example
Simple Factory model
The factory pattern is mainly used to encapsulate the implementation logic and to provide the service of the object’s actual drawing through a common interface, so I don’t have to do much when I add new classes, just modify them a little bit.
Take an example of how the e-commerce business I worked for before created goods:
In this simple factory, if we want to create active goods 1 and 2, when we want to create goods, we just call the create goods method in the simple factory, create different goods according to the type and then return the column.
Several ways to implement simple factory:
Static factory mode
Let’s go back to the example of creating goods
It seems that there is no problem with creating different items according to the type, but there is one problem that I wonder if you have noticed.
Is it that every time I add a type do I have to change the if else of the createProduct method? Isn’t that against our open and closed principle?
So that’s not a good way to do it. We have two more:
-
Use reflex mechanism
-
Directly register a commodity object, add a method of Type Type, and return its own method of the same Type based on Type Type
Again, we’ll start with creating goods: reflection implementations
When I look at the code above, I see that reflection is also easy to implement, but in some cases, it is not suitable, and in some cases it is not possible to implement, and reflection can reduce the performance of the application, should be avoided in high performance scenarios.
There is also a problem for improper reflex online machine is easy to cause a problem, because we reflection created object attribute was SoftReference soft references, so when * * – XX: SoftRefLRUPolicyMSPerMB * * is not set well will always let the machine CPU is very high.
Of course, its default value is 1000, which depends on your situation, but notice this.
The last one, which is very similar to the reflection implementation, is to avoid using reflection, and instead of storing the classes to be added in the Map object, it stores the columns of each type of object to be added. I won’t write the demo here 😂
Factory method pattern
Factory method pattern is an improvement on the traditional model of static factory, our factory class directly by abstraction, specific to a particular logic code of transferred to the realization of abstract method subclass, so we don’t go to modify the factory class (i.e., don’t have to do if the else) it is also a way which are frequently used in our current.
Let’s take creating a product as an example:
So if you look at this diagram you’re essentially creating another factory, which is going to create a factory object
Let’s look at how the code implements this:
Here we start by creating an abstract factory method
Create a commodity factory to inherit the abstract factory method.
When there are other types we just inherit the factory method that we created
This code we can certainly come out, but what we really need to learn is the idea of the factory model of the predecessors. Applying this idea to our real business scenarios can really improve us.
Let me give you another example: apply what you have learned
Suppose that now the leader asks you to make a picture of shared goods. We know that there are many types of goods, such as goods without SKU, goods with SKU, order sharing, invite sharing…… And so on. So how do we design this code to make it easier to understand, easier to read, and more scalable in the future?
Ps: SKU and SPU are terms in our e-commerce. Spu is almost the same as item, that is, commodity is one dimension. An item has many SKUs, for example, iPhone is an item and SPU.
The first step should always be to define a create share template
The second step is to create a share factory to get the bean columns we preloaded in the Spring container based on our type
Finally, we define our different types of sharing images.
In fact, it is this idea that we learn design mode. With this idea, the three advantages mentioned above can be reflected in our future growth, and our ability to design and think in the face of complex business can be improved.
So the question is, when should we use the factory method pattern rather than the simple factory pattern?
Here: a quote from the beauty of the design patterns inside when the object creation logic is complicated, it is ok to not just a simple new, but to combine other class object, doing all sorts of initialization, we recommend using a factory method pattern, will create logical split into multiple complex factory class, let each factory class is not too complicated.
With the simple factory pattern, putting all the creation logic into a single factory class leads to a complex factory class
Abstract Factory pattern
After looking at the factory method pattern, it is easier to understand the abstract factory pattern because it is really an extension of the factory method.
There is only one abstract method in a factory method class. To implement different class objects, you have to create different subclasses of concrete factory methods to implement them, whereas an abstract factory is responsible for creating multiple objects of different types
It’s a little confusing, but let’s draw a picture
It may be a little ugly, but it shows that looks can actually be divided into the following parts:
- Abstract Factory class
- Specific Factory
- An abstract class
Abstract factory class I can personally understand as a new factory mobile phone, concrete abstract factory this is that we each of the mobile phone wallpaper custom Settings, the last abstract class I understand is mobile phone wallpaper.
Each of us can customize different wallpapers such as: GIF sister, still scenery, little fool and so on.
At the end
During my tenure in several e-commerce companies, I have seen a lot of big guy’s code, how can I describe it, is a variety of design patterns, it looks really elegant, and then when we access it is very convenient, just pass parameters.
Design patterns themselves are ubiquitous in our business code. When I was writing this article, I deliberately looked through our previous projects and saw their presence in almost every project. After talking with my former colleague, I realized more deeply.
In fact, if we want to go further and higher, we can’t stop learning all the time. Later, I may write a process engine of an Internet company for design pattern, to see how we can use the framework to realize it for more complex business logic.
I’m Aobing, the more you know, the more you don’t know, thank you for your talent: likes, favorites and comments, we’ll see you next time!
This article is constantly updated. You can search “Santaizi Aobing” on wechat and read it for the first time. Reply [Information] There are the interview materials and resume templates for first-line big factories prepared by me.