Simplified explanation
Design principles and design patterns have a similar goal:
- Make software flexible
- Make it easier to change
- Reduce unexpected problems
- Improve reuse and maintenance
However, they are not the same.
- A design principle is general advice that guides design decisions.
- A design pattern is a reusable structure for solving a particular type of problem.
The Main Difference
| Design principles | Design patterns |
|---|---|
| General guidelines | Reusable solution structures |
| Apply across many situations | Solve particular recurring problems |
| Explain what direction to follow | Show how objects can be arranged |
| More abstract | More practical and structured |
| Example: Favor composition over inheritance | Example: Strategy Pattern |
| Guide our decisions | Help us implement those decisions |
A useful memory line is:
Principles tell us what direction to take. Patterns show us one established way to get there.
Design Principles
Design principles guide how we structure classes and relationships between objects.
Examples include:
- Encapsulate what varies
- Favor composition over inheritance
- Program to interfaces
- Keep objects loosely coupled
- Follow the SOLID principles
They help us avoid designs that are:
- Rigid: One change requires many other changes.
- Fragile: A change unexpectedly breaks unrelated code.
- Immobile: Code is difficult to reuse elsewhere.
A principle does not provide a complete class structure. It provides advice that helps us make better decisions.
For example:
Encapsulate what varies.
This tells us to separate frequently changing behaviour from stable behaviour. However, it does not specify exactly which classes we need to create.
Design Patterns
Design patterns provide reusable structures for solving commonly occurring design problems.
A pattern normally explains:
- The problem it solves
- When it may be useful
- The classes or objects involved
- How those objects communicate
- The advantages and disadvantages
A pattern is not finished copy-and-paste code. It is a general design that must be adapted to the application.
Examples include:
| Pattern | Problem it helps solve |
|---|---|
| Strategy | Switching between different behaviours |
| Factory | Creating objects without exposing complicated creation logic |
| Observer | Notifying several objects when something changes |
| Adapter | Making incompatible interfaces work together |
| Decorator | Adding behaviour without changing the original class |
How Principles Lead to Patterns
Consider this principle:
Encapsulate what varies.
Suppose discount calculations change regularly.
A simple design might place every discount rule directly inside Checkout:
class Checkout:
def calculate_total(self, price, customer_type):
if customer_type == "student":
return price * 0.9
if customer_type == "premium":
return price * 0.8
return price
Every time a new discount is introduced, the Checkout class must be modified.
The principle tells us to separate the changing discount behaviour. The Strategy Pattern gives us a practical structure for doing that:
class RegularDiscount:
def calculate(self, price):
return price
class StudentDiscount:
def calculate(self, price):
return price * 0.9
class PremiumDiscount:
def calculate(self, price):
return price * 0.8
class Checkout:
def __init__(self, discount_strategy):
self.discount_strategy = discount_strategy
def calculate_total(self, price):
return self.discount_strategy.calculate(price)
We can select the required strategy when creating Checkout:
student_checkout = Checkout(
StudentDiscount()
)
print(student_checkout.calculate_total(100)) # 90
A new discount can now be added without changing the Checkout class:
class SeasonalDiscount:
def calculate(self, price):
return price * 0.7
seasonal_checkout = Checkout(
SeasonalDiscount()
)
print(seasonal_checkout.calculate_total(100)) # 70
In this example:
- Design principle: Encapsulate what varies.
- Design pattern: Strategy Pattern.
- Final implementation: Separate discount classes used by
Checkout.
The principle guided the decision. The pattern provided the class structure.
One Pattern Can Follow Several Principles
The Strategy Pattern above supports several principles:
- Encapsulate what varies: Each discount rule is separated.
- Favor composition:
Checkoutcontains a discount strategy. - Program to interfaces:
Checkoutonly expects an object withcalculate(). - Loose coupling:
Checkoutdoes not know the details of each discount. - Open/Closed Principle: New discounts can be added without changing
Checkout.
This shows why principles are considered the foundation beneath design patterns.
This is not a strict development process. It represents how the ideas relate:
- OOP provides the basic tools.
- Principles guide how we use those tools.
- Patterns organize those tools into reusable solutions.
- Developers adapt those solutions to their applications.
The Gang of Four Patterns
The Gang of Four refers to the four authors who documented 23 well-known design patterns.
Those patterns are divided into three groups:
| Category | Purpose | Examples |
|---|---|---|
| Creational | Control how objects are created | Factory, Builder, Singleton |
| Structural | Organize classes and objects | Adapter, Decorator, Facade |
| Behavioural | Manage communication and behaviour | Strategy, Observer, Command |
These patterns are common vocabulary among developers. Saying “we could use the Strategy Pattern” quickly communicates a general design idea without explaining the entire structure from the beginning.
Final Comparison
| Question | Design principle | Design pattern |
|---|---|---|
| What is it? | General design advice | Reusable solution structure |
| What does it address? | Overall design quality | A recurring design problem |
| What does it provide? | Direction | A possible class arrangement |
| Is it complete code? | No | No, it must still be adapted |
| Example | Favor composition | Strategy Pattern |
Main Lesson
Design principles help us recognize what makes a design good. Design patterns show established ways to apply those principles to recurring problems.
Principles are broader than patterns. Patterns are more specific, but they are built upon the ideas expressed by design principles.