0% found this document useful (0 votes)
91 views6 pages

Cheat Sheet

The document summarizes several creational design patterns including Factory Method, Abstract Factory, Singleton, Builder, and Prototype. Factory Method allows creating objects from subclasses through a factory class. Abstract Factory forms groups of classes with shared functionalities (families) created by a factory. Singleton ensures only one instance of a class is created for the entire application. Builder creates complex objects from simpler parts. Prototype creates new objects by cloning existing object prototypes.

Uploaded by

parlamentsmehrh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views6 pages

Cheat Sheet

The document summarizes several creational design patterns including Factory Method, Abstract Factory, Singleton, Builder, and Prototype. Factory Method allows creating objects from subclasses through a factory class. Abstract Factory forms groups of classes with shared functionalities (families) created by a factory. Singleton ensures only one instance of a class is created for the entire application. Builder creates complex objects from simpler parts. Prototype creates new objects by cloning existing object prototypes.

Uploaded by

parlamentsmehrh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Creational Structural

They restrict or control the They describe how objects


manner in which objects are and classes can be
created, keeping the user combined to build larger
from using the “New” and more complex
instruction for creating new structures.
instances.

Abstract Factory Composite


Factory Method Decorator
Object Pool Flyweight
Prototype Adapter
Singleton Facade
Builder Bridge
Proxy

https://reactiveprogramming.io
They devote to collaboration, relationships and responsibility
delegation between classes in order to simplify they manner
in which objects communicate and interact with each other.

Iterator Template Method Iterator


Mediator Strategy Command
Memento Chain of Resp Observer

Behavioral
Creational Patterns reactiveprogramming.io

Factory Method
Type: Creational
1. The client requests ConcreteFactory for the
creation of ProductA.
Factory Method allows us to create 2. ConcreteFactory finds the concrete
objects from an specific subtype implementation of ProductA and creates a new
through a Factory class. This is instance.
3. ConcreteFactory returns a new
especially useful when we don't ConcreteProductA.
know which subtype we are going 4. The client requests ConcreteFactory for the
to use at design-time, or when do creation of ProductB.
we want to delegate object creation 5. ConcreteFactory finds the concrete
implementation of ProductB and creates a new
logic to a Factory class . instance.
6. ConcreteFactory returns a new
ConcreteProductB.

Abstract Factory 1. The client sends a request for a ConcreteFactory1


to AbstractFactory.
2. AbstractFactory creates an instance of
Type: Creational ConcreteFactory1 and returns it.
3. The client sends a request to ConcreteFactory1 for
the creation of ProductA.
Its goal is to form a group of classes with
4. ConcreteFactory1 creates an instance of
shared functionalities, better known as
ProductA1 which is a member of family1, and returns
families, created by a Factory. This pattern
it.
is especially useful when we need to
5. Now the client sends a request for a
implement certain class families to solve a
ConcreteFactory2 to AbstractFactory.
problem, however, we may need to add
6. AbstractFactory creates an instance of
parallel implementations of the same
ConcreteFactory2.
classes to solve the same problem with a
7. The client sends a request to ConcreteFactory2 for
different approach.
the creation of ProductA.
8. El ConcreteFactory2 creates an instance of
ProductA2 which is a member of Family2, and
returns it.

Singleton
Type: Creational
1. The client requests for an instance of the
It receives this name because it can Singleton through the getInstance
only have a single instance of a method.
specific class for the entire 2. The Singleton checks if the instance has
application; this is achieved by already been created, if not, a new
preventing the creation of multiple instance will be created.
instances of the same class, as it 3. Either the newly created instance or the
typically happens with the new already existing instance is returned.
operator, and imposing a private
constructor and a static method to
get that instance.

Builder 1. The client creates an instance of the


ObjectBuilder.
2. The client executes step 1 in the
Type: Creational ObjectBuilder creation.
3. Internally, TargetObject is created by the
ObjectBuilder.
It allows us to create a complex 4. The client executes step 2 in the ObjectBuilder
object from simpler ones. By using creation.
utility methods, it creates various 5. Internally, OtherObjectA is created by the
ObjectBuileder.
sections to build a larger object and 6. The client executes step 3 in the ObjectBuilder
keeps the complexity of the creation.
structure hidden from the user. 7. Internally, OtherObjectB is created by the
ObjectBuileder.
8. The client requests ObjectBuilder for the
creation of the TargetObject, this takes all the
previously created objects, assigns them to
TargetObject returns them.

Prototype
Type: Creational
1. The client requests for the cloning of prototype
Its functionality is based on object cloning. using the clone method.
New objects are created from a pool of 2. The prototype creates a new instance of itself,
previously created and stored prototypes. using the new operator.
This pattern is especially useful when we 3. The prototype copies all of its own attributes to
need to create objects from existing ones the new instance.
or for creating very large structures of 4. The prototype may clone all the internal objects, a
objects. process that is known as Deep Clone.
5. The newly cloned object is returned by the
prototype.

Object Pool 1. The client sends a request for an object


to ConcreteObjectPool.
Type: Creational 2. ConcreteObjectPool checks if the
required object is available, otherwise it
will send the request for the creation of a
This pattern is widely used for
new object to ConcreteObjectFactory.
working with large numbers of
3. ConcreteObjectFactory creates a new
computationally expensive objects.
object whose type is
This pattern is advantageous for
ConcretePoolableObject.
those scenarios where our program
4. ConcreteObjectPool returns the object
needs these kinds of objects for a
to the client.
short time to discard them
5. The client uses the object.
afterwards.
6. The client returns the object to
ConcreteObjectPool.
Behavioral Patterns reactiveprogramming.io

Iterator Type: Behavioral

This design pattern allows us to go


over a data structure without
1. The client sends a request for an iterator to
needing to know its internal ConcreteAggregate.
construction. It is especially useful 2. ConcreteAggregate creates a new Iterator.
when we are working with complex 3. The client enters in a loop in order to review all
the elements on the structure until there are no
data structures, because it lets us go
elements left for reviewing in the iterator, which is
over their elements through an going to be signaled by the hasNext method.
iterator. The iterator is an interface 4. The client sends the request for a new element to
including the necessary methods the iterator using the next method.
5. If there are elements left to be reviewed, then we
for reviewing all the elements of a
return to step 3 until all elements have been
data structure. reviewed.

Command
Type: Behavioral

The Command design pattern


1. The invoker gets a command from
allows us to execute operations
CommandManager.
without having to know how they
2. The invoker executes the command.
are implemented. These operations
3. The invoker gets another command
are known as commands, and each
from CommandManager.
operation is implemented as an
4. The invoker executes the command.
independent class performing a
very specific action.

Observer
Type: Behavioral 1. ObserverA registers into the Observable
object in order to be notified of any
This design pattern allows us to changes.
observe the changes suffered by an 2. ObserverB registers into the Observable
object, that is, if the observed object object in order to be notified of any
suffers any change, a notification changes.
will be sent to the observers; this is 3. The status of the Observable changes.
known as Publish-Subscribe. 4. All the Observers are notified that a
change has occurred.

Template Method
Type: Behavioral

The template design pattern focuses 1. The client creates and gets an instance of the
on code reutilization for implementing template implementation.
steps to solve problems. This is 2. The client executes the templateMethod.
achieved by implementing base classes 3. The default implementation of templateMehod
for defining basic behaviors. Typically, executes the step1, step2, step3, stepN methods in
methods are created for each step of order.
the algorithm; some of these will be 4. The template implementation returns a result.
implemented while others remain
abstract until they are executed by the
subclasses.

Strategy 1. The client creates a new context y and sets the


StrategyA.
2. The client executes the doSomething operation.
3. Context hands over this responsibility to
Type: Behavioral ConcreteStrategyA.
4. ConcreteStrategyA performs the operation and
returns the result.
It allows us to set the behavior of a class at
5. Context takes this result and hands it over to the
runtime. Strategy bases on polymorphism
client.
for implementing a series of behaviors that
6. The client changes the Context strategy at runtime.
can be interchanged during the execution
7. The client executes the doSomething operation
of the program, allowing for a modification
again.
of an object’s behavior by following a
8. Context hands over this responsibility to
strategy that has been set.
ConcreteStrategyB.
9. ConcreteStategyB performs the operation and
returns the result.
10. Context takes this result and hands it over to the
client.

Chain of Responsability 1. The client sends a request to the chain of


responsibility for its processing.
2. The first Handler tries to process the request
Type: Behavioral but it’s unable to do it, therefore, it relays the
request to the next Handler.
It allows us to solve problems where 3. The second Handler tries to process the
request but it’s unable to do it, therefore, it relays
we’re not sure which object should the request to the next Handler.
process a specific request; this 4. The third Handler tries to process the request
design pattern can easily solve but it’s unable to do it, therefore, it relays the
problems inheritance can’t because request to the next Handler.
5. The HandlerN (Any handler in the chain) is the
of its chain structure, where a series one finally succeeding in processing the request,
of objects try to process a request in after which it sends a response (optional) so it
sequence. can be relayed by all the previous Handlers until
it reaches to the Client.
Behavioral Patterns (Continuation) reactiveprogramming.io

Interpreter
1. The client creates the context
Type: Behavioral for the execution of the interpreter.
2. The client creates or gets the expression to be
The Interpreter design pattern is evaluated.
3. The client requests for the evaluation of the
used for evaluating a determined
expression to the interpreter. To that end, it sends
language and return an Expression. it the context.
This pattern can interpret 4. The Expression calls the Non-terminal
languages such as Java, C#, SQL, or Expressions.
5. The Non-terminal Expressions call all the
even a new one invented by us, and
Terminal Expressions.
provide a response based on its 6. The Root Expression requests for the
evaluation. interpretation of a Terminal Expression.
7. The expression is completely evaluated and
provides the result of the interpretation of all the
Terminal and Non-Terminal Expressions.

Mediator Type: Behavioral

It handles the manner in which a group 1. ComponentA wants to communicate with


of classes interact with each other. This ComponentB and sends a message through the
pattern is especially useful when we mediator.
2. The mediator may analyze the message for
have a large number of classes
debugging or tracking purposes, or for directing it
communicating directly with one to the receiver.
another. By implementing this pattern, 3. The message is delivered to the receiver, which in
we can create a bidirectional turn sends a response to the mediator.
communication layer, which can be 4. The mediator receives the response and directs it
used by a class in order to interact with to ComponentA.
others through a common object 5. Likewise, the process can go in reverse, from
acting as a mediator. ComponentB to ComponentA by repeating the
previous steps in order to achieve a bidirectional
communication.

Memento
1. The Client makes a change to the
Originator.
Type: Behavioral
2. The Originator creates a new Memento
which represents its current state.
It allows us to record the state of an
3. The Client stores the Memento into the
object on a specific moment in
Caretaker in order to change between
order to return it to that state at any
different Originator states.
given time. Memento is useful when
4. After some time, the Client sends a
we have many objects changing
request to the Caretaker for a previous
over time and, for some reason, we
state of the Originator.
need to restore them to a previous
5. The Client uses the Memento sent by
state.
the Caretaker to restore the state of the
Originator.

Null Object
Type: Behavioral 1. The client looks for a specific object.
2. ObjectLookup checks if the requested object
It helps us avoid the appearance of exists.
a. If the requested object doesn't exist, an instance
null values at runtime. What this of NullObject will be returned.
pattern basically proposes is using 3. On the other hand, if the requested object is
instances for implementing the located, an instance of ConcreteObject will be
required interface but with a void returned.
4. The client receives any of the two mentioned
body, instead of returning a null instances, however, it will never get a null reference
value. if the object were not to be found.

State
Type: Behavioral 1. A state by default is set in the Context, this is
StateA.
2. A request operation is executed in the Context,
It changes its behavior depending
delegating the execution to the current state
on the state of the application. To (StateA).
achieve this, a series of classes are 3. The Context changes from StateA to StateB.
created for representing the 4. Again, a request operation is executed in the
Context delegating the execution to the current
different states an application can
state (StateB).
go through, that is, one class for 5. The execution of StateB results in a change to
each state of the application. StateC.
6. A new request operation is executed in the
Context delegating the execution to the current
state (StateC).

Visitor
1. The client creates the structure (Element).
2. The client creates the instance of the Visitor to be
used in the structure.
3. The client executes the accept method of the
Type: Behavioral structure.
4. The Element tells the Visitor which method must be
It separates the logic and operations used for processing each object in the structure.
5. The Visitor analyzes the Element using its
performed over a complex visitElement method and repeats the accept method
structure. On occasions, we can find execution process over the children of the Element.
data structures requiring various 6. ConcreteElementA tells the Visitor which method
operations to be performed and must be used for processing it.
7. The visitor proceeds with the rest of the children of
needing new ones to be created as the Element and executes the accept method on
the application grows. ConcreteElementB.
8. ConcreteElementB tells the Visitor which method
must be used for processing it, that is visitElementB.
9. Lastly, the results are obtained by executing the
getResults method.
Structural Patterns reactiveprogramming.io

Adapter
Type: Structural

It's used when we encounter 1. The Client invokes the Adapter with generic
incompatible software interfaces, parameters.
which, even with this inherent 2. The Adapter converts the generic parameters
incompatibility, have similar into specific parameters for the Adaptee.
3. The Adapter invokes the Adaptee.
features. This pattern is used when 4. The Adaptee answers the call.
we want to work with different 5. The Adapter converts the response of the
interfaces in a homogeneous Adaptee into a generic response for the Client.
manner, to that end, we create an 6. The Adapter attends the Client with a generic
response.
intermediate class which functions
as an adapter.

Bridge
Type: Structural
1. The client executes an AbstraccionImpl operation.
2. AbstraccionImpl relays this petition to
It decouples an abstraction from its ConcreteImplementor, at this point
implementation. The purpose is AbstraccionImpl may convert the parameters
that each one can be modified needed for executing the ConcreteImplementor.
3. ConcreteImplementor returns the results to
separately without affecting the
AbstraccionImpl.
other; in other words, an abstraction 4. The AbstraccionImpl converts the results coming
gets decoupled from its from ConcreteImplementor to give them to the
implementation so they can vary client.
independently.

Composite
Type: Structural 1. The client executes an action over
CompositeA.
It helps us constructing complex 2. Then, CompositeA executes an action
structures from simpler ones; in over CompositeB.
other words, we can use small 3. CompositeB executes an action over
structures to assemble a bigger and LeafA and LeafB and the result is returned
more complex structure. to CompositeA.
4. CompositeA spreads this action over
LeafC, which returns a result.
5. CompositeA gets an end result after an
evaluation of the entire structure, which is
then returned to the client.

Decorator
Type: Structural 1. The client executes an operation over
DecoratorA.
It allows the user to add new 2. DecoratorA executes the same operation over
DecoratorB.
features to an existing object 3. DecoratorB executes an action over
without altering its structure ConcreteComponent.
through the addition of new classes 4. DecoratorB executes a decoration operation.
wrapped around the original one, 5. DecoratorA executes a decoration operation.
6. The Client receives a decorated object by all the
giving it extra capabilities. This Decorators, which have encapsulated the
pattern has been designed to solve Component below many layers.
scenarios where a subclassification
hierarchy cannot be applied.

Facade
Type: Structural
1. The client invokes a facade operation.
It hides the complexity of interacting with a 2. The facade communicates with SubsystemA to
group of subsystems by providing a high perform an operation.
level interface, which is in charge of 3. The facade communicates with SubsystemB to
establishing communication with all the perform an operation.
subsystems, keeping the user from having 4. The facade communicates with SubsystemC to
to directly interact with each system. perform an operation.
5. The facade provides the client with the result of
the operation.

Flyweight
1. The client requests the Factory for the
Type: Structural
creation of a Flyweight object.
2. Before creating the object, the Factory
It's dedicated to building light
checks if there's an identical object to the
objects by abstracting parts that
one being requested, if so, it returns the
can be reused and shared in order
existing object; if not, it will create the new
to create new objects when
object and store it in a cache for further
required, and reusing objects
use.
created by other instances.
3. The Flyweight object is created or
borrowed from the cache and returned to
the client.
Structural Patterns (Continuation) reactiveprogramming.io

Proxy
Type: Structural 1. The client sends a request for an
object to the Factory.
2. The Factory creates a Proxy for encapsulating
It's devoted to the mediation the Object.
between two objects. What I refer 3. The client executes the Proxy, which was created
as mediation is the series of actions by the Factory.
4. The Proxy performs one or more actions before
executed before and after carrying the execution of the Object.
out the task requested by the user. 5. The Proxy hands over the execution to the
Object.
6. The Proxy performs one or more actions after the
execution of the Object.
7. The Proxy returns the result.

You might also like