In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about how e-commerce companies use Factory mode. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
I don't know if there is a sense of crisis with the increase of people's working years. I am afraid that my technical depth has not improved at the beginning, so I often look at the source code of the framework, or sign up for some online courses to improve myself.
Recently, a colleague of my old employer talked to me about this problem, saying that he had just changed the company recently, but his code felt very "low". He could always put forward all kinds of suggestions in codereview, and after years of experience, he was still making that kind of novice mistake, and writing code had no pattern and design beauty to speak of.
And when you want to learn the depth of the technology (take a look at the source code of the framework), you always feel that you only understand the surface, but not the ideas inside.
Everyone here should know what I want to say, yes, it is the design pattern. In fact, this is my first year and the second year of my job. Why should I learn design patterns? What are the advantages of design patterns?
Improve the ability to view the source code of the framework
Improve your code design ability and code ability for complex business logic
Lay a solid foundation for the interview and the future career path.
For example, when I used to do e-commerce activities, my partner who did the venue activities used the responsibility chain + factory + singleton mode. I only need multiple factory types, and then the responsibility chain is passed on, and his singleton is ok. This is the advantage of the design pattern. X can also benefit future generations.
Text
Today I will talk about the factory model, which is widely used in our e-commerce field. I looked at the code of my computer project before I wrote it, and it can be seen everywhere.
(Factory mode) Factory models can be divided into three main categories:
Simple factory model
Factory method model
Abstract engineering model
Today, I will mainly focus on these three models to give an example.
Simple factory model
The factory pattern is mainly used to encapsulate the implementation logic and provide the service of illustrating objects through common interfaces. There is no need to make a big deal when I add new classes, just a little bit of modification.
For example, my previous e-commerce business how to create 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 items according to the type, and then return by instantiation.
There are several ways to implement a simple factory:
Static factory mode
We still take the creation of goods as our list.
In fact, there seems to be no problem with creating this way, creating different products according to the type, but there is a problem that you don't know if you have found it?
Do I have to modify the if else of the createProduct method every time I add a type? Isn't this against our principle of opening and closing?
So this is not a good way, we still have two kinds of pick-up:
Use the reflection mechanism
Register the commodity object directly, add a Type type method, and return a method of the same type according to the type type
Again, let's focus on creating goods: reflection implementations.
Looking at the code above, I find that reflection is easy to implement, but it is not applicable in some specific cases, and it cannot be implemented in some specific cases, and the reflection mechanism will also reduce the running effect of the program. This implementation should be avoided in scenarios that require high performance.
There is also a problem with improper reflection, which can easily lead to problems with the online machine, because the object properties created by our reflection are soft referenced by SoftReference, so if the * *-XX:SoftRefLRUPolicyMSPerMB** is not set, the machine CPU will always be very high.
Of course, his default value is 1000, which depends on everyone's situation. Anyway, just pay attention to this.
The remaining one is very similar to the reflective implementation, which is to avoid using reflection. Instead of storing the classes to be added in the Map object, it is the actual column of each kind of object that will be added. I'm not going to write demo about this?
Factory method model
The factory method pattern is an improvement on the static factory pattern, our factory class is directly abstracted, and the specific logic code needs to be transferred to the subclass of the abstract method, so that we don't have to modify the factory class (that is, we don't have to do any if else changes), which is also a common way for us to do so.
Or let's take the creation of goods as an example:
To look at this picture is to create another factory, which is used to create factory class objects.
Let's take a look at how the code does this:
Here we first create an abstract factory method
Create another commodity factory to inherit the abstract factory method.
When there are other types, we just have to inherit the factory method we created.
I'm sure everyone can implement this code, but what we really need to learn is the idea of this factory model of our predecessors. Applying this idea to our real business scenarios and putting what we have learned into practice can really improve us.
Let me give you another example: put what you have learned into practice.
Suppose leader asks you to do a picture of shared goods now. We know that there are many types of goods, such as non-SKU goods, SKU products, order sharing, invitation sharing. Wait for a series of scenes. So how do we design this code to make it easier to understand, easy to read, and good expansibility in the future?
Ps:sku and spu are the terms in our e-commerce. Spu is almost the same as item, that is, commodity is a dimension. A commodity item has a lot of sku, for example: iphone is a commodity is item and spu, and the white iphone 1264G is a specific sku.
The first step should be to define a shared template.
In the second step, we create a sharing factory to get the bean real columns that we preloaded in the Spring container according to our type.
Finally, we define our different types to share pictures.
In fact, we learn design patterns is this kind of thinking, with this idea mentioned above three advantages can be reflected in our future growth, in the face of complex business design and thinking ability can be improved.
So the question is, when should we use the factory method model instead of the simple factory model?
Here to quote the beauty of the design pattern inside a sentence: when the object creation logic is more complex, not just a simple new can, but to combine other class objects, to do a variety of initialization operations, we recommend using the factory method pattern, the complex creation logic will be split into multiple factory classes, so that each factory class is not too complex.
Using the simple factory pattern, putting all the creation logic into a factory class will make the factory class very complex.
Abstract factory pattern
After looking at the factory method pattern, it is easier to understand the abstract factory pattern, because it is actually an extension of the factory method.
There is only one abstract method in the factory method class. if you want to implement many different class objects, you can only create subclasses of different concrete factory methods to instantiate, while the abstract factory is responsible for creating multiple different types of objects.
It feels a little roundabout to understand, so let's draw a picture.
It may be a little ugly, but it tells the story, but it actually looks like it can be divided into the following parts:
Abstract factory class
Specific factory class
Abstract class
Abstract factory class I can personally understand as a newly manufactured mobile phone, specific abstract factory this is that each of us customized settings for this mobile phone wallpaper, and finally I understand that the abstract class is mobile phone wallpaper.
Each of us can customize different wallpapers, such as: moving picture sister, still picture scenery, little fool and so on.
If we want to go higher and further, then we can't stop learning. Later, I may write an Internet company process engine for design patterns. Let's take a look at how we use the framework to implement it for more complex business logic.
The above is how e-commerce companies use Factory mode, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.