4

Design Principles and Design Patterns

Principles tell us what direction to take. Patterns show us one established way to get there.

 

Part 1 of 3 · Topic 4 of 14 · 4 min read

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 principlesDesign patterns
General guidelinesReusable solution structures
Apply across many situationsSolve particular recurring problems
Explain what direction to followShow how objects can be arranged
More abstractMore practical and structured
Example: Favor composition over inheritanceExample: Strategy Pattern
Guide our decisionsHelp 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:

PatternProblem it helps solve
StrategySwitching between different behaviours
FactoryCreating objects without exposing complicated creation logic
ObserverNotifying several objects when something changes
AdapterMaking incompatible interfaces work together
DecoratorAdding 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: Checkout contains a discount strategy.
  • Program to interfaces: Checkout only expects an object with calculate().
  • Loose coupling: Checkout does 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:

CategoryPurposeExamples
CreationalControl how objects are createdFactory, Builder, Singleton
StructuralOrganize classes and objectsAdapter, Decorator, Facade
BehaviouralManage communication and behaviourStrategy, 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

QuestionDesign principleDesign pattern
What is it?General design adviceReusable solution structure
What does it address?Overall design qualityA recurring design problem
What does it provide?DirectionA possible class arrangement
Is it complete code?NoNo, it must still be adapted
ExampleFavor compositionStrategy 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.