MANUAL

Programming paradigms and design patterns

Programming styles and design patterns

Your AI assistant uses terms such as ‘functional’, ‘dependency injection’ or ‘observer pattern’. You don’t need to be able to programme them yourself, but if you know what they mean, you’ll be better placed to judge whether its suggestion is appropriate.

01

Programming styles (paradigms)

Style Essence Where you see it
Imperative / procedural Explaining how to do something step by step Scripts, older PHP
Declarative Say what you want, not how SQL, HTML, CSS, Terraform, React components
Object-oriented (OOP) Data and behaviour combined in objects (classes) Laravel models and controllers, Java
Functional (FP) Functions without side effects; do not modify data, but create new data map/filter/reduce, Laravel Collections, React
Event-driven Code responds to events Laravel Events and Listeners, JavaScript clicks, webhooks
Reactive Data streams you ‘subscribe’ to RxJS, live updates
Concurrent / parallel Multiple things at once Queues, async/await, threads
Logical / declarative with rules Facts and rules; the system finds the solution Prolog, rule engines
Aspect-oriented programming (AOP) Weaving general concerns (logging, permissions) ‘around’ the code Middleware is similar to this

In practice, modern code combines several styles. A Laravel app is object-oriented, uses functional Collections, declarative migrations and event-driven queues.

02

Key concepts

03

Design patterns you often come across

Pattern At a glance Example in Laravel/JS
Factory A place that creates objects for you Model factories for test data
Builder Building a query step by step The query builder: ->where()->orderBy()->get()
Singleton Exactly one instance app()->singleton(...) in the service container
Adapter Adapting items with a slightly different shape Email and storage drivers (S3, local)
Facade A simple front-end for something complex Cache::, Log::, Storage::
Decorator Adding behaviour without modifying the original Middleware, caching around a service
Proxy Intermediary that manages access Lazy loading of relationships
Observer Letting others know that something has happened Model, observers, events, addEventListener
Strategy Interchangeable approaches behind a single interface Various payment or delivery methods
Command A command as an object Queued jobs, Artisan commands
Chain of responsibility A request passes through a series of processors The middleware pipeline
Iterator Step by step through a collection Collections, foreach, generators
Template method Fixed step-by-step plan, with details to be filled in Base classes with overridable methods
Composite Tree structure of identical components Menus, React components
Abstract factory Creating families of related objects Themes or drivers per platform
Prototype Creating a new object by copying an existing one $model->replicate(), JS prototypes

Dependency injection: a class receives what it needs from an external source, rather than creating it itself. Laravel’s service container does this automatically when you request a class in your constructor. This makes testing much easier.

04

When a pattern is not required

Patterns are tools, not an end in themselves. Is your assistant proposing three layers of interfaces for a simple function? Ask: “Can this be done more simply, and what do we gain from this pattern?” Simple code that the team understands is usually better.

05

Need help?

It’s best to make major design decisions before you start building. At OpenSpec**

06

Skills

07

Related services