Design patterns are solutions to common problems encountered in software design. They provide a 'systematic' yet creative way to design and solve problems. When you're building software, you often face similar challenges, and design patterns offer a way to address these challenges in an efficient manner.
-
Reusability: Design patterns promote code reusability. Once you understand and implement a design pattern, you can apply it to different projects and scenarios. Note that it is not a piece of code for you to copy and paste, but a certain way of thinking and solving problems.
- But it is not the same as algorithm, whose input and output satisfy a certain specifications. Design Patterns are more creative and applicable in software development, in the sense that it does not require a strict specification on its input.
-
Maintainability: Using design patterns results in cleaner and more maintainable code. They encapsulate specific behaviors, making it easier to modify or extend functionalities without affecting the entire codebase.
-
Scalability: Design patterns contribute to scalable and flexible software architecture. They help in building systems that can adapt to changing requirements and scale gracefully.
Creational patterns focus on object creation mechanisms, dealing with the process of object instantiation.
Structural patterns are concerned with object composition, forming relationships between objects to create larger structures.
Behavioral patterns define ways for objects to communicate, encapsulating patterns of communication between objects.
We briefly introduce a Creational Pattern to demonstrate how powerful and flexible design patterns are.
Suppose we are developing a software where transportation tools are involved. At first, we only support cars and busses. But as our product grows, we have the need to support more types of transportation tools, like airplanes and boats, etc. We can certainly create more classes, like we did for cars and busses. But sometimes all we care about is that they are methods of transportation, i.e., they can take people from one place to another place. Let's call that behavior move()
.
Thus, we may define a shared interface between among these classes
class Vehicle:
def move(self):
...
We can then define
class Airplane(Vehicle):
def move(self):
...
and
class Car(Vehicle):
def move(self):
...
This way, we don't need to worry about the exact details of 'how a car/plane/bicycle' moves, but knowing that they support move()
is good enough.
- Coursera - "Design Patterns" by the University of Alberta:
- This online course covers design patterns using Java. It includes video lectures, quizzes, and programming assignments to reinforce the concepts.
-
Refactoring Guru - Design Patterns:
- This website provides a comprehensive guide to design patterns with examples in multiple programming languages. EXTREMELY RECOMMENDED!
-
DZone - Design Patterns Refcard:
- DZone offers a concise reference card on design patterns, providing a quick overview of essential patterns.
- Stack Overflow - Design Patterns Questions:
- Stack Overflow is an excellent place to find answers to specific design pattern-related questions and engage with the programming community.