Design Patterns

2 min. read

Design Patterns

Introduction

SOLID Design Principles
A property is a collection of methods

The Patterns

Creational Patterns

Builder
Factories
Abstract Factory
Factory Method
Prototype
Singleton

Structural Patterns

Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

Behaviour Patterns

Chain of Responsibility

Command

Interpreter

Iterator
Mediator
Memento
Null Object
Observer
State

Strategy

References:
Strategy Pattern – Design Patterns (ep 1)

Template Method

Visitor

Dependency Injection - Autofac

SOLID Design Principles
Single Responsibility Principle (SRP)
A class should have only a single reason to change, single responsibility
Separation of concerns - different classes handling different independent tasks/problems
Open-Closed Principle (OCP)
Open for extension but closed for modifications
Use Interfaces and inheritance
Enterprise pattern -> Specification Pattern
Debug.Log($” {varName}”);
Liskov Substitution Principle (LSP)
Should be able to substitute a base type for a subtype
Use virtual properties and override
Interface Segregation Principle (ISP)
Instead of a big interface, split interfaces into smaller Interfaces, single responsibility
YAGNI - You Ain’t going to need it
Dependency Inversion Principle (DIP)
High-level modules should not depend upon low-level ones, use abstraction

Creational Patterns
Builder Pattern
When piecewise object construction is complicated, provide an API for doing it succinctly.

StringBuilder is the best example
Fluent Builder
An interface which allows you to chain several calls by returning a reference to the object you’re working with.

LeanTween is an example of Fluent Interfaces.

Fluent Builder Inheritance with Recursive Generics

Faceted Builder

Factories
A component responsible solely for the wholesale (not piecewise) creation of objects
Factory Method
Public static functions
Constructor becomes private
The name of the method is not linked to the class
Proper Factory
Separate class, responsible for creating classes only
The original class’s constructor will be public
GameObject gameObject = new GameObject(“name”); is a factory
Inner Factory
Change the class to internal
Internal means it can only be instantiated from its own Assembly
Or
Create an inner class
Example: Point.Factory
Abstract Factory

References
5 Design Patterns Every Engineer Should Know

https://github.com/DovAmir/awesome-design-patterns

https://github.com/abishekaditya/DesignPatterns

https://github.com/anupavanm/csharp-design-patterns-for-humans

https://github.com/kamranahmedse/design-patterns-for-humans

Software Design Patterns and Principles (quick overview)

Strategy Pattern – Design Patterns (ep 1)