What are design patterns
Design patterns are typical solutions to common problems in software design. They are like prefabricated blueprints that can be adapted to meet requirements and can be used to solve recurring design problems in your code.
Design patterns are not used in the same way as methods or libraries, and it is difficult to apply a design pattern directly to your own programs. A pattern is not a specific piece of code, but a general concept that solves a specific problem. You can follow the pattern to implement a solution that fits the actual needs of your application.
Why and how to learn design patterns?
Perhaps you have been developing programs for years and have no idea what a singleton pattern is. A lot of people do. Even so, you may have used some design patterns without even knowing it. So why not take some time to learn more about them?
- Design patterns are toolboxes for common problems in software design, and the tools are proven solutions. Even if you have never encountered these problems, knowing patterns is still very useful because it can guide you in how to use object-oriented design principles to solve various problems.
- Design patterns define a common language that allows you to communicate more effectively with your team members. Just say, “Oh, let’s use singletons here,” and everyone will understand the idea behind the suggestion. As long as you know the schema and its name, you don’t need to explain what a singleton is.
Classification of design patterns
Different design patterns vary in complexity, detail level and application scope in the whole system. I like to compare it to road construction: if you want to make intersections safer, you can install traffic lights, or you can build multi-storey intersections that include pedestrian underpasses.
All patterns can be classified according to their intent or purpose, and there are three main categories of patterns:
- Creative pattern (provides a mechanism for creating objects, increasing the flexibility and reusability of existing code)
- Structural patterns (how to assemble objects and classes into a larger structure while keeping the structure flexible and efficient)
- Behavior patterns (responsible for effective communication and delegation of responsibilities between clients)
Creation pattern
1.1 Factory method pattern
The factory method pattern is a creative design pattern that provides a method to create an object in a parent class, allowing subclasses to determine the type of object to instantiate.
1.2 Generator Mode
1.3 Abstract factory pattern
1.4 Prototype Mode
1.5 Singleton Mode
Copy the code
Copy the code
Copy the code
Copy the code
Copy the code
Copy the code