
7 Design Patterns Every Developer Should Know
Table of Contents
Table of Contents
7 Design Patterns Every Developer Should Know
Design patterns are proven software design solutions that help solve recurring problems in programming. Applying the right design pattern makes your code easier to maintain, extend, and optimize for performance. This article summarizes the 7 most common design patterns every developer should know, with examples and real-world applications.
1. Singleton Pattern
Singleton ensures that only one instance of a class exists throughout the application’s lifecycle, commonly used for logging or database connections.
- Advantages: Manages global state, saves resources.
- Disadvantages: Hard to test, needs thread-safety considerations.
Example:
class Singleton:
static instance = null
static method get_instance():
if instance == null then
instance = new Singleton()
end if
return instance
end method
# Using Singleton
singleton = Singleton.get_instance()
2. Builder Pattern
Builder helps create complex objects by breaking the initialization process into multiple steps, suitable for classes with many optional parameters.
Example:
class ProductBuilder:
method __init__():
this.product = new Product()
end method
method set_name(name):
this.product.name = name
return this
end method
method set_price(price):
this.product.price = price
return this
end method
method build():
return this.product
end method
# Using Builder
builder = new ProductBuilder()
product = builder.set_name("Laptop").set_price(1500).build()
3. Factory Pattern
Factory provides an interface for creating objects without specifying the exact class, making code flexible and easy to extend.
Example:
class UserFactory:
static method create_user(user_type):
if user_type == "admin" then
return new Admin()
else if user_type == "moderator" then
return new Moderator()
else
return new RegularUser()
end if
end method
# Using Factory
user = UserFactory.create_user("admin")
4. Facade Pattern
Facade hides the complexity of a system, providing a simple interface for clients, often used in APIs or UIs.
Example:
class ComplexSystem:
method operation_a():
print("Operation A")
end method
method operation_b():
print("Operation B")
end method
class Facade:
method __init__():
this.system = new ComplexSystem()
end method
method simplified_operation():
this.system.operation_a()
this.system.operation_b()
end method
# Using Facade
facade = new Facade()
facade.simplified_operation()
5. Adapter Pattern
Adapter connects two incompatible interfaces, often used when integrating external APIs or libraries.
Example:
class OldSystem:
method old_method():
print("Old Method")
end method
class Adapter:
method __init__(old_system):
this.old_system = old_system
end method
method new_method():
this.old_system.old_method()
end method
# Using Adapter
old_system = new OldSystem()
adapter = new Adapter(old_system)
adapter.new_method()
6. Strategy Pattern
Strategy allows you to change the execution algorithm at runtime, helping code comply with the Open-Closed Principle.
Example:
class TravelStrategy:
method travel():
pass
end method
class CarStrategy(TravelStrategy):
method travel():
print("Travel by Car")
end method
class BikeStrategy(TravelStrategy):
method travel():
print("Travel by Bike")
end method
class Context:
method __init__(strategy):
this.strategy = strategy
end method
method execute_strategy():
this.strategy.travel()
end method
# Using Strategy
context = new Context(new CarStrategy())
context.execute_strategy()
7. Observer Pattern
Observer helps monitor and react to state changes, commonly used for notifications or event systems.
Example:
class Subject:
method __init__():
this.observers = []
end method
method attach(observer):
this.observers.append(observer)
end method
method notify():
for observer in this.observers do
observer.update()
end for
end method
class Observer:
method update():
pass
end method
class ConcreteObserver(Observer):
method update():
print("Observer Updated")
end method
# Using Observer
subject = new Subject()
observer = new ConcreteObserver()
subject.attach(observer)
subject.notify()
Why Learn Design Patterns?
- Improve problem-solving and code optimization skills.
- Make teamwork and communication easier with a common language.
- Enhance system design skills and prepare for technical interviews.
References:
- Refactoring Guru - Design Patterns
- Wikipedia - Software design pattern
- Book: “Head First Design Patterns”
Practice and apply these patterns in real projects to gain deeper understanding!
Related Posts
7 Design Patterns Mà Mọi Developer Nên Biết
Khám phá 7 design patterns phổ biến nhất trong phát triển phần mềm: Singleton, Builder, Factory, Facade, Adapter, Strategy, Observer. Bài viết giải thích chi tiết, ví dụ minh họa, ứng dụng thực tế giúp developer nâng cao kỹ năng thiết kế hệ thống.
Read more7 Design Patterns Every Developer Should Know
Explore 7 essential design patterns: Singleton, Builder, Factory, Facade, Adapter, Strategy, and Observer with examples and real-world applications.
Read moreKiến Trúc Backend for Frontend (BFF): Hiểu Rõ và Ứng Dụng Hiệu Quả
Khám phá kiến trúc Backend for Frontend (BFF), lợi ích, thách thức, ví dụ thực tế và thực tiễn tốt nhất khi áp dụng BFF vào dự án.
Read more