When it comes to design patterns , A lot of people think it's a cliche , Some people think design patterns are necessary , Some people think design patterns are not that important , So should we pay attention to design patterns in our work ? Should we apply design patterns to our production process ?
If you've never heard of design patterns , I bet you're not a qualified programmer . It's like a person saying he's a mathematician , But he hasn't even heard of the Pythagorean theorem , How could it be ?
Factory mode
This is a basic design pattern , It's also the most commonly used design pattern , This is one that we use all the time , But I don't know the design pattern I'm using .
The factory model is called "Si Yi" is a processing factory , What's different from reality is , It's not clothes that are produced here , leather shoes , Pants, etc , It's the most important object in our object-oriented programming .
In reality, we all know the benefits of factories , Not only can we mass produce , You can also customize it in bulk , Because there are different molds , It can produce all kinds of products that people need .
Software development , We're more concerned with the way objects are used , As for how objects are created , We don't care much about , therefore , The abstract factory solved our problem . We just need to customize the product function interface we need , Then let the factory follow our interface , That is to realize the interface production object .
Through this call , If there is a problem with our product , Can be recalled at any time , It can be patched by the factory at any time , The flexible expansion of the product is realized .
Adapter pattern
In reality , We often charge mobile phones , And we use a power adapter , Why is it called an adapter ? Because it can not only charge our own mobile phones , You can also charge someone else's cell phone , It can even charge other electrical devices , Because it can adapt to all kinds of charging devices , So it's called an adapter .
Software development , It's almost or impossible for us to develop an application without a database , So what kind of database to choose is what we need to care about most , Because if you choose wrong , There may be performance bottlenecks in the later stage , So is there a way that we can not change the code , Or to switch the database seamlessly with little code modification ? The answer is adapter mode .
Let's first define the adapter interface , Then let various databases implement the interface we have defined , We use methods defined in the interface in our code , So when we want to switch databases , As long as the database to implement the corresponding interface method , We can complete the database adaptation , You can connect seamlessly .
Observer mode
Observer mode , It's also called notification mode , It's a one to many model . In reality , When the headmaster or teacher gives an order on the stage , The loudspeaker will send the order to every student , When the students hear the order , All have their own reactions , The principal or teacher does not have to convey to the students one by one , Because the loudspeakers give orders to students , Here the loudspeaker is an observer , And teachers and principals are the observed , The students are the people to be informed , So the observer model is also called publish subscribe mode .
In software development , For example, our product has such a function , After the user places an order and pays successfully , A text message will be sent to inform the user , If you want to send more than text messages later , Also need to send email , Also need voice notification , In this case , We can use the observer model , We put the payment success information into the message queue , As for texting or email , Each business module subscribes to the message queue and processes it by itself . This is in the order module , You don't need one SMS module after another , Mail module .
Decorator mode
in real life , Generally, after a person changes a dress , We can still recognize him , Clothes are his decorations , It shows that decoration does not affect a person's appearance , That is, it doesn't affect the person's function , But by wearing a fancy coat , But it can improve one's temperament , This is the function of decoration , The decorator pattern is to make an object more powerful by decorating it without changing it .
Take a case study of software development , For example, we have designed a SMS sending function , And after testing and online testing, there is no problem with this function , But now there is a new need , When you ask to send a text message , And voice reminders , How can we be able to , To realize this function , At this point, we can use decorators , That is to decorate a voice function for SMS sending class , Let it not only send text messages , In addition, it can also realize the function of voice transmission .
The strategy pattern
Strategy is a collection of solutions to achieve goals , In short, it's a collection of methods , They are all used to achieve one thing .
Software development , An object uses different strategies for different scenarios to achieve the same function , It's a strategic approach , If you've heard of polymorphism , You'll find that it's very similar to the definition of polymorphism , The difference is that polymorphism is different processing methods of the same operation by different objects , The policy pattern is that the same object uses different policies to process the same operation .
such as , We have a national day 7 Tianyou's project , The starting point and destination are from Beijing to Moscow , But different strategies can be used , Yes, the plane , You can train , You can drive yourself , The same dream , Different ways of implementation .
Taobao home page of thousands of people, thousands of faces is also a strategic model , It's all display products , Face different people , It shows different products , This is determined by strategy .
summary
If you are careful enough , Design patterns in software development are not created out of thin air , It must have come from life , Actually , Any knowledge comes from life , It's just that after learning and refining , Put it above life .
Design patterns are not omnipotent , It doesn't help you solve all the problems , But it can help you solve most problems , And use design patterns in coding , You'll find that , Your code will look clearer , More organized .
Learning design mode , Using design patterns is not the ultimate goal , Our ultimate goal is to win without a move , When you forget all the design patterns , But when they can be used in coding , You're not learning design patterns in vain .