# Design patterns

> The classic design patterns in plain words: creational, structural and behavioral, each with when to use it, its pros and cons, and a small Java example.

Page: https://haythamasalama.me/learn/design-patterns

## Creational

How objects get created, so the code that uses them doesn’t depend on how.

- [Singleton](https://haythamasalama.me/learn/design-patterns/singleton.md): A class that makes sure only one instance of itself exists, and hands everyone that same instance. Example: A shared database connection.
- [Factory](https://haythamasalama.me/learn/design-patterns/factory.md): One place decides which concrete class to create, so the rest of the code only asks for what it needs. Example: A pizza shop that makes margherita and pepperoni.
- [Builder](https://haythamasalama.me/learn/design-patterns/builder.md): Build a complex object step by step, setting only the parts you need, then get the finished object in one call. Example: A car with maker, model, year, colour and gearbox.
- [Prototype](https://haythamasalama.me/learn/design-patterns/prototype.md): Create a new object by copying an existing one, then change only what differs. Example: Copies of a Ford Mustang.

## Structural

How classes and objects fit together into larger structures.

- [Adapter](https://haythamasalama.me/learn/design-patterns/adapter.md): A thin wrapper that lets a class work with code that expects a different interface. Example: A USB drive used through a memory card interface.
- [Bridge](https://haythamasalama.me/learn/design-patterns/bridge.md): Split a class into two parts that can change on their own: what it does, and how it gets done. Example: An application that runs on Windows or Mac.
- [Composite](https://haythamasalama.me/learn/design-patterns/composite.md): Treat a single object and a group of objects the same way, by building them into a tree. Example: Files and directories in a file system.
- [Decorator](https://haythamasalama.me/learn/design-patterns/decorator.md): Wrap an object in another object that adds behaviour, without changing the original class. Example: Booking plans that add to the price.
- [Facade](https://haythamasalama.me/learn/design-patterns/facade.md): One simple class in front of a complex system, so callers talk to it instead of to every part. Example: Buying and selling shares through one class.
- [Flyweight](https://haythamasalama.me/learn/design-patterns/flyweight.md): Share the common part of many similar objects, so large numbers of them cost little memory. Example: Trees in a forest that share their shape and colour.
- [Proxy](https://haythamasalama.me/learn/design-patterns/proxy.md): A stand-in with the same interface as the real object, which controls when and how the real one is used. Example: A database that connects only when asked.

## Behavioral

How objects share the work and talk to each other.

- [Chain of Responsibility](https://haythamasalama.me/learn/design-patterns/chain-of-responsibility.md): Pass a request along a chain of handlers, where each one can deal with it or hand it to the next. Example: Middleware that checks a web request.
- [Command](https://haythamasalama.me/learn/design-patterns/command.md): Turn a request into an object, so it can be passed around, queued, logged or undone. Example: Commands that turn a light on and off.
- [Iterator](https://haythamasalama.me/learn/design-patterns/iterator.md): Walk through the items of a collection one by one, without knowing how the collection stores them. Example: A small collection backed by an array.
- [Observer](https://haythamasalama.me/learn/design-patterns/observer.md): One object announces a change, and every object that subscribed to it hears about it. Example: A tweet and its followers.
- [State](https://haythamasalama.me/learn/design-patterns/state.md): An object changes its behaviour when its internal state changes, as if it had changed its class. Example: A transaction that moves from pending to approved.
- [Strategy](https://haythamasalama.me/learn/design-patterns/strategy.md): Put each way of doing a task in its own class, and choose which one to use while the program runs. Example: A logger that writes to a file or the console.
- [Template Method](https://haythamasalama.me/learn/design-patterns/template-method.md): A base class fixes the steps of a task, and subclasses fill in the steps that differ. Example: Importing CSV, Excel and Word files.
- [Visitor](https://haythamasalama.me/learn/design-patterns/visitor.md): Add new operations to a set of classes without changing them, by moving each operation into its own visitor. Example: A payroll for full-time and part-time staff.
