Introduction

An brief introduction to this guide

Hi there! My name is John McCourt. I've been a software engineer in the logistics, energy and insurance industries for over 16 years.Over time I have learned the importance of designing code well. Thankfully there are structual concepts which have already been established by experts and are in use today by many developers. These concepts are called design patterns and they help developers design the structure of their code in a logical fashion.

This is a guide that I'm writing as I study different design patterns. I intend discussing how each design pattern works, why it is used and showing some code implementations of each in PHP, C# and Java. I will explain how I created each implementation. The purpose is to help anyone who reads it learn about different ways to implement the patterns and to help me understand the concepts in such a way that I can use or explain them better. I'm writing this as I go along with my learning so will be checking updates in periodically. For this reason there may be times when it may not appear complete. Please feel free to add comments, suggestions or corrections as issues here on github and I will do my best to address them in a timely manner. Click to see my Github project for this guide

Patterns being covered

The following definitions are heavily summarised. Please use them only to help remember what separates the different patterns. More detailed definitions, explanations and implementations will be discussed later.

Simple Factory Pattern - Utilises a class that has a single method for deciding which type of class to instantiate

Factory Method Pattern - Provides an interface/abstract class for creating objects. A factory implements/entends that. Allows subclasses (factories) to decide which concrete class to instantiate

Abstract Factory Method - Factory of factories. Contains abstact & concrete factories as well as abstract & concrete products

Object Pool - Avoid expensive acquisition and release of resources by recycling objects that are no longer in use

Prototype - A fully initialized instance to be copied or cloned

Singleton - A class of which only a single instance can exist

Structural design patterns

These design patterns are all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object-patterns define ways to compose objects to obtain new functionality.

Adapter - Match interfaces of different classes

Bridge - Separates an object’s interface from its implementation

Composite - A tree structure of simple and composite objects

Decorator - Add responsibilities to objects dynamically

Facade - A single class that represents an entire subsystem

Flyweight - A fine-grained instance used for efficient sharing

Private Class - Data Restricts accessor/mutator access

Proxy - An object representing another object

Behavioral design patterns

These design patterns are all about Class's objects communication. Behavioral patterns are those patterns that are most specifically concerned with communication between objects.

Chain of responsibility - A way of passing a request between a chain of objects

Command - Encapsulate a command request as an object

Interpreter - A way to include language elements in a program

Iterator - Sequentially access the elements of a collection

Mediator - Defines simplified communication between classes

Memento - Capture and restore an object's internal state

Null Object - Designed to act as a default value of an object

Observer - A way of notifying change to a number of classes

State - Alter an object's behavior when its state changes

Strategy - Encapsulates an algorithm inside a class

Template method - Defer the exact steps of an algorithm to a subclass

Visitor - Defines a new operation to a class without change