Design Patterns
Design Patterns
Design patterns are common solutions to frequently occurring problems in software design.
They are like pre-made blueprints that can be customized to solve a recurring design
problem in your code.
It is not possible to select a pattern and copy it to the program as if it were already prepared
functions or libraries. The pattern is not a specific piece of code, but a general concept to
solve a particular problem. You can follow the details of the pattern and implement a
solution that fits the realities of your own program.
Patterns are often confused with algorithms because both concepts describe typical
solutions to known problems. While an algorithm always defines a clear set of actions to
achieve a goal, a pattern is a higher level description of a solution. The code of the same
pattern applied to two different programs can be different.
An analogy of an algorithm would be a cooking recipe: both have clear steps to reach a
goal. On the other hand, a pattern is more similar to a blueprint, since you can see what its
result and its functions look like, but the exact order of implementation is up to you.
Creational patterns
Creational patterns provide various object creation mechanisms that increase the flexibility
and reuse of existing code.
Factory Method
Factory Method is a creational design pattern that provides an interface for creating objects
in a superclass, while allowing subclasses to alter the type of objects that will be created.
Example here:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/creational/factorymethod
Abstract Factory
is a creational design pattern that allows us to produce families of related objects without
specifying their concrete classes.
Example here:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/creational/abstractFactory
Builder
is a creational design pattern that allows us to build complex objects step by step. The
pattern allows us to produce different types and representations of an object
using the same construction code.
Examples here
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/creational/builder
Prototype
is a creational design pattern that allows us to copy existing objects without our code
depending on their classes.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/creational/prototype
Singleton
is a creational design pattern that allows us to ensure that a class has a single instance,
while providing a global access point to that instance.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/creational/singleton
Chain of Responsibility
is a behavioral design pattern that allows you to pass requests along a chain of handlers.
Upon receiving a request, each handler decides whether to process it or pass it on to the
next handler in the chain.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral
Command
is a behavioral design pattern that turns a request into a separate object that contains all the
information about the request. This transformation allows you to parameterize methods
with different requests, delay or queue the execution of a request, and support operations
that cannot be performed.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral/command
Iterator
is a behavioral design pattern that allows you to iterate through elements of a collection
without exposing its underlying representation (list, stack, tree, etc.).
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/behavioral/iterator
Mediator
is a behavioral design pattern that allows you to reduce chaotic dependencies between
objects. The pattern restricts direct communications between objects, forcing them to
collaborate only through a mediator object.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral/mediator
Memento
is a behavioral design pattern that allows you to save and restore the previous state of an
object without revealing the details of its implementation.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/behavioral/memento
Observer
is a behavioral design pattern that allows you to define a subscription mechanism to notify
multiple objects of any event that happens to the object they are observing.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral/observer
State
is a behavioral design pattern that allows an object to alter its behavior when its internal
state changes. It appears as if the object changed its class.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/behavioral/state
Strategy
is a behavioral design pattern that allows you to define a family of algorithms, put each of
them in a separate class, and make their objects interchangeable.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral/strategy
Template Method
is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but
allows subclasses to override steps of the algorithm without changing its structure.
Example: https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/
src/main/java/com/example/desing_patterns/behavioral/templatemethod
Visitor
is a behavioral design pattern that allows you to separate algorithms from the objects they
operate on.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/behavioral/visitor
Adapter
is a structural design pattern that enables collaboration between objects with incompatible
interfaces.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/commit/
25249f7fc641052bac0d76d273a3dee26e334ba5
Bridge
is a structural design pattern that allows you to split a large class, or a group of closely
related classes, into two separate hierarchies (abstraction and implementation) that can be
developed independently of each other.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/structurals/bridge
Composite
is a structural design pattern that allows you to compose objects into tree structures and
work with those structures as if they were individual objects.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/structurals/composite
Decorator
is a structural design pattern that allows you to add functionality to objects by placing these
objects inside special encapsulating objects that contain these capabilities.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/structurals/decorator
Facade
is a structural design pattern that provides a simplified interface to a library, framework, or
any other complex group of classes.
Example:
https://git.cglcloud.com/MobileTraining/design-patterns-kotlin/tree/main/app/src/main/
java/com/example/desing_patterns/structurals/facade