mirror of
https://github.com/iandinwoodie/cpp-design-patterns-for-humans.git
synced 2025-12-17 12:34:38 +03:00
Added text for structural design patterns.
This commit is contained in:
164
README.md
164
README.md
@@ -313,7 +313,35 @@ entities.
|
||||
|
||||
### 🔌 Adapter
|
||||
|
||||
#### Overview
|
||||
Real world example:
|
||||
|
||||
> Consider that you have some pictures in your memory card and you need to
|
||||
transfer them to your computer. In order to transfer them you need some kind of
|
||||
adapter that is compatible with your computer ports so that you can attach
|
||||
memory card to your computer. In this case card reader is an adapter.
|
||||
|
||||
Another real world example:
|
||||
|
||||
> Another example would be the famous power adapter; a three legged plug can't
|
||||
be connected to a two pronged outlet, it needs to use a power adapter that makes
|
||||
it compatible with the two pronged outlet.
|
||||
|
||||
And another:
|
||||
|
||||
> Yet another example would be a translator translating words spoken by one
|
||||
person to another.
|
||||
|
||||
In plain words:
|
||||
|
||||
> Adapter pattern lets you wrap an otherwise incompatible object in an adapter
|
||||
to make it compatible with another class.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In software engineering, the adapter pattern is a software design pattern that
|
||||
allows the interface of an existing class to be used as another interface. It is
|
||||
often used to make existing classes work with others without modifying their
|
||||
source code.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
@@ -321,66 +349,200 @@ TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### 🚡 Bridge
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> Consider you have a website with different pages and you are supposed to allow
|
||||
the user to change the theme. What would you do? Create multiple copies of each
|
||||
of the pages for each of the themes or would you just create separate theme and
|
||||
load them based on the user's preferences? Bridge pattern allows you to do the
|
||||
second i.e.
|
||||
|
||||

|
||||
|
||||
In plain words:
|
||||
|
||||
> Bridge pattern is about preferring composition over inheritance.
|
||||
Implementation details are pushed from a hierarchy to another object with a
|
||||
separate hierarchy.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> The bridge pattern is a design pattern used in software engineering that is
|
||||
meant to "decouple an abstraction from its implementation so that the two can
|
||||
vary independently."
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### 🌿 Composite
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> Every organization is composed of employees. Each of the employees has the
|
||||
same features i.e. has a salary, has some responsibilities, may or may not
|
||||
report to someone, may or may not have some subordinates etc.
|
||||
|
||||
In plain words:
|
||||
|
||||
> Composite pattern lets clients treat the individual objects in a uniform
|
||||
manner.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In software engineering, the composite pattern is a partitioning design
|
||||
pattern. The composite pattern describes that a group of objects is to be
|
||||
treated in the same way as a single instance of an object. The intent of a
|
||||
composite is to "compose" objects into tree structures to represent part-whole
|
||||
hierarchies. Implementing the composite pattern lets clients treat individual
|
||||
objects and compositions uniformly.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### ☕ Decorator
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> Imagine you run a car service shop offering multiple services. Now how do you
|
||||
calculate the bill to be charged? You pick one service and dynamically keep
|
||||
adding to it the prices for the provided services till you get the final cost.
|
||||
Here each type of service is a decorator.
|
||||
|
||||
In plain words:
|
||||
|
||||
> Decorator pattern lets you dynamically change the behavior of an object at
|
||||
run time by wrapping them in an object of a decorator class.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In object-oriented programming, the decorator pattern is a design pattern that
|
||||
allows behavior to be added to an individual object, either statically or
|
||||
dynamically, without affecting the behavior of other objects from the same
|
||||
class. The decorator pattern is often useful for adhering to the Single
|
||||
Responsibility Principle, as it allows functionality to be divided between
|
||||
classes with unique areas of concern.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### 📦 Facade
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> How do you turn on the computer? "Hit the power button" you say! That is what
|
||||
you believe because you are using a simple interface that computer provides on
|
||||
the outside, internally it has to do a lot of stuff to make it happen. This
|
||||
simple interface to the complex subsystem is a facade.
|
||||
|
||||
In plain words:
|
||||
|
||||
> Facade pattern provides a simplified interface to a complex subsystem.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> A facade is an object that provides a simplified interface to a larger body of
|
||||
code, such as a class library.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### 🍃 Flyweight
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> Did you ever have fresh tea from some stall? They often make more than one
|
||||
cup that you demanded and save the rest for any other customer so to save the
|
||||
resources e.g. gas etc. Flyweight pattern is all about that i.e. sharing.
|
||||
|
||||
In plain words:
|
||||
|
||||
> It is used to minimize memory usage or computational expenses by sharing as
|
||||
much as possible with similar objects.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In computer programming, flyweight is a software design pattern. A flyweight
|
||||
is an object that minimizes memory use by sharing as much data as possible with
|
||||
other similar objects; it is a way to use objects in large numbers when a simple
|
||||
repeated representation would use an unacceptable amount of memory.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
### 🎱 Proxy
|
||||
|
||||
#### Overview
|
||||
|
||||
Real world example:
|
||||
|
||||
> Have you ever used an access card to go through a door? There are multiple
|
||||
options to open that door i.e. it can be opened either using access card or by
|
||||
pressing a button that bypasses the security. The door's main functionality is
|
||||
to open but there is a proxy added on top of it to add some functionality. Let
|
||||
me better explain it using the code example below.
|
||||
|
||||
In plain words:
|
||||
|
||||
> Using the proxy pattern, a class represents the functionality of another
|
||||
class.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> A proxy, in its most general form, is a class functioning as an interface to
|
||||
something else. A proxy is a wrapper or agent object that is being called by the
|
||||
client to access the real serving object behind the scenes. Use of the proxy can
|
||||
simply be forwarding to the real object, or can provide additional logic. In the
|
||||
proxy extra functionality can be provided, for example caching when operations
|
||||
on the real object are resource intensive, or checking preconditions before
|
||||
operations on the real object are invoked.
|
||||
|
||||
#### Programmatic Example
|
||||
|
||||
TODO
|
||||
|
||||
#### When To Use
|
||||
|
||||
TODO
|
||||
|
||||
## Behavioral Design Patterns
|
||||
|
||||
In plain words:
|
||||
|
||||
Reference in New Issue
Block a user