Essential Design Patterns

Harry lives a miserable life.

Harry receives an invitation to another world.

Family prevents Harry from accepting the invitation.

Harry meets a mentor.

Harry travels to a new world.

Harry makes friends and faces rivals.

Harry ventures into a dangerous and forbidden place.

Harry is confronted with a life-or-death challenge.

Harry receives a reward.

Harry returns to a place of safety.

Harry is transformed by his experiences.

Harry returns to his normal life,
strengthened by new confidence.

Luke lives a miserable life.

Luke receives an invitation to another world.

Family prevents Luke from accepting the invitation.

Luke meets a mentor.

Luke travels to a new world.

Luke makes friends and faces rivals.

Luke ventures into a dangerous and forbidden place.

Luke is confronted with a life-or-death challenge.

Luke receives a reward.

Luke returns to a place of safety.

Luke is transformed by his experiences.

Luke returns to his normal life,
strengthened by new confidence.

also works for

Lord of the Rings

The Matrix

Men in Black

Batman Begins

The Lion King

Aladdin

The Wizard of Oz

"Monomyth"

-- James Joyce, 1939

"Hero's Journey" / "Quest"

-- Joseph Campbell: "The Hero with a Thousand Faces", 1949

"Here, fully explored,
was the pattern I had been sensing.
Campbell had broken the secret code of story."

-- Christopher Vogler: "The Writer's Journey: Mythic Structure for Writers", 1992

At the core […] is the idea people should design their homes, streets, and communities.

This idea […] comes from the observation most of the wonderful places of the world were
not made by architects, but by the people.

-- Christopher Alexander et al., "A Pattern Language"

https://claytondorge.com/patterns-list: 253 patterns

Life Cycle

Market of Many Shops

Street Cafe

Master and Apprentice

Creational Patterns

  • Singleton
  • Factory
  • Builder

"Singletons are the path to the dark side."

-- Stefan Priebsch

Singleton


public static function getInstance(): self
{
    if (!isset(self::$instance)) {
        self::$instance = new self();
    }

    return self::$instance;
}
  

Singleton


...
$singleton = Singleton::getInstance();
...
  

"Singletons are the path to the dark side."

-- Stefan Priebsch

Structural Patterns

  • Adapter
  • Proxy
  • Facade

Protecting Proxy


interface Something
{
    public function doWork(): void;
    ...
}
  

Protecting Proxy


final readonly class SomethingImplementation
                                        implements Something
{
    public function doWork(): void
    {
        // ...
    }
}
  

Protecting Proxy


final readonly class ProtectingProxy implements Something
{
    public function __construct(
        private readonly Something $something
    ) {}

    public function doWork(): void
    {
        if (!access_is_allowed) {
             throw new Exception('No nice things for you.');
        }

        $this->something->doWork();
    }
}
  

Lazy Loading Proxy


final readonly class LazyLoadingProxy implements Something
{
    private Something $something;

    public function __construct(
        private SomethingId $id,
        private SomethingRepository $repository
    ) {}

    ...
  

Lazy Loading Proxy



    ...

    public function doWork(): void
    {
        if (!isset($this->something)) {
            $this->something =
                $this->repository->findById($this->id);
        }

        $this->something->doWork();
    }
}
  

Behavioral Patterns

  • Template Method
  • Mediator
  • Chain of Responsibility
  • Strategy

Strategy


final readonly class RevenueReport
{
    public function __construct(
        private RevenueRecognitionStrategy $strategy
    ) {}

    ...
}
  

Strategy


final readonly class RevenueReport
{
    ...

    private function calculateRevenue(
        Employee $employee,
        Period $period
    ): void
    {
        return $this->strategy->calculateRevenue(
            $employee,
            $period
        );
    }
}
  

Template Method


abstract readonly class Application
{
    final public function run(): void
    {
        $request = HttpRequest::fromSuperGlobals();
        $response = $this->handleRequest($request);
        $response->send();
    }

    abstract protected function
        handleRequest(HttpRequest $request): HttpResponse;
}
  

Transaction Script

Domain Model

Service Layer

Active Record

Data Mapper

Repository

Data Transfer Object

Server Session State

Template View

Front Controller

Design Patterns Are

proven Solutions

Customizable

Concept, not Code

Communication

I can help you to ...

introduce Design Patterns into your application

introduce Design Patterns into your team

learn more about Design Patterns

https://thephpcc.academy

Thank you!

Talk to me about Design Patterns, Software Architecture, and PHP in general:

stefan@thephp.cc

https://zeeg.me/spriebsch/30mins

https://thephp.cc

About Stefan

Sources