Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

0 Software design patterns

Software design patterns


4/28/2022
1 Software design patterns

1. Discuss the similarities of the patterns in each group of patterns.


 Command, Bridge and Object Adapter:
Command Design pattern:
The command pattern is a data-driven design pattern that is classified as a behavioral pattern.
A request is wrapped in an object and sent to the invoker object as a command. The invoker
object searches for an object that can handle this command and sends the command to that
object, which then executes it.
Bridge Design pattern:
When we need to separate an abstraction from its implementation so that both can change
independently, we utilize Bridge. This design pattern is classified as a structural pattern
because it provides a bridge structure that connects implementation and abstract classes.
This pattern uses an interface as a bridge, separating the functionality of concrete classes
from the functionality of interface implementer classes. Structures of both sorts of classes can
be changed without impacting the other.
Object Adapter:
An object's principal means of accessing ORB services like object reference creation is
through an object adapter. Standard interfaces are exported to the object using a portable
object adaptor. An object adapter's major tasks are to generate and understand object
references.
Similarities of the Command, Bridge and Object Adapter

The command pattern wraps a request as a command within an object. It is passed to an


invoker object, which searches for an item that can handle this command and grants authority
to the matching method/object that performs it. The object adapter pattern, on the other hand,
wraps an existing interface as an adoptee in an adapter.

 Flyweight, Prototype and Singleton:


Flyweight Design pattern:
The Flyweight pattern is used to reduce the number of objects created, reduce memory
footprint, and improve performance. This design pattern is classified as a structural pattern
since it shows how to reduce the number of objects in an application's object structure. When
no identical objects are located, the Flyweight design tries to reuse them by storing them and
then constructs a new item.
Prototype Design pattern:
The prototype pattern refers to duplicating an item while considering performance. This
design pattern is classified as a creational pattern since it gives one of the most effective ways
to construct an object.
This technique entails creating a prototype interface that instructs the current object to be
cloned. When creating an object directly is expensive, this pattern is employed.

April 28, 2022


2 Software design patterns

Singleton Design pattern:


The singleton pattern is one of the most basic design patterns. This design pattern is classified
as a creational pattern since it gives one of the most effective ways to construct an object.
This pattern uses a single class that oversees creating an object while ensuring that only one
object is created. This class provides a method to access its single object, which may be
accessed without having to instantiate the class's object.
Similarities of Flyweight, Prototype and Singleton

Both the prototype and singleton design patterns are based on creational patterns. Both stress
the creation of objects automatically within the code. By minimizing the number of objects in
the application, the Flyweight Pattern helps to improve its performance. Because common
attributes are shared between objects via Intrinsic properties, the Flyweight Pattern decreases
memory footprint and saves RAM. On the same side the prototype also improves the
performance of the application just like the flyweight design pattern but by improving the
application’s functionalities. Bridge is like Adapter, except when you define both the abstract
interface and the underlying implementation, it's called Bridge. You're not adapting to legacy
or third-party code; instead, you're the code's designer, and you need to be able to change out
multiple implementations.

 Visitor, Proxy, and Strategy:


Visitor Design pattern:
A visitor class is used in the Visitor pattern to update an element class's executing algorithm.
As a result, the element's execution algorithm can change as the visitor changes. This pattern
belongs to the category of behavioral patterns. The element object must accept the visitor
object as per the pattern so that the visitor object can perform the operation on the element
object.
Proxy Design pattern:
A class in the proxy pattern represents the functionality of another class. This design pattern
is classified as a structural pattern.
In the proxy pattern, we create an object that contains the original object and uses it to
interface its capabilities with the outside world.
Strategy Design pattern:
A class's behavior or algorithm can be altered at runtime using the Strategy pattern. This
design pattern is classified as a behavior pattern.
We generate objects that represent numerous strategies and a context object whose behavior
varies depending on its strategy object in the Strategy pattern. The context object's running
algorithm is changed by the strategy object.

April 28, 2022


3 Software design patterns

Similarities of Visitor, Proxy, and Strategy

The visitor and the strategy design patterns are both similar in this way that they both belongs
to the behavior patterns. Both the patterns describe the behavior of the application. A visitor
is a strategy that has numerous procedures and can be dispatched twice. The Visitor also
enables secure runtime binding between two physical objects also the strategy is similar with
the visitor that it has the one procedure or method that works multiple times.

 Builder, Abstract factory, and Factory:


Builder Design pattern:
The Builder pattern creates a complex object from simple objects in a step-by-step manner.
This design pattern is classified as a creational pattern since it gives one of the most effective
ways to construct an object.
Abstract Factory Design pattern:
Abstract Factory Patterns are based on a super-factory that generates more factories. This
plant is also known as the factory of factories. This design pattern is classified as a creational
pattern since it gives one of the most effective ways to construct an object. An interface is
responsible for establishing a factory of related objects in the Abstract Factory design without
explicitly declaring their classes.
Factory Design pattern:
The factory pattern is one of Java's most popular design patterns. This design pattern is
classified as a creational pattern since it gives one of the most effective ways to construct an
object. We generate objects without disclosing the creation mechanism to the client in the
Factory design, and we refer to freshly formed objects using a common interface.
Similarities of Builder, Abstract factory, and Factory

One of the Creational patterns is the Abstract Factory design pattern. Abstract Factory Pattern
is a layer of abstraction over Factory Pattern that is almost identical to Factory Pattern.
Abstract Factory Patterns are based on a super-factory that generates more factories. Builder
pattern is like the Factory Pattern in that the client requests a specific type of object from a
class hierarchy, but the factory class's Create method delegated the creation of the specific
object to the derived classes and returned the object of the class of the type requested by the
client. In essence, you have a single point of contact for the construction of a class hierarchy's
multiple objects.

April 28, 2022


4 Software design patterns

2. Discuss the differences of the patterns in each group of patterns.


 Command, Bridge and Object Adapter:
Differentiate between Command, Bridge and Object Adapter

Command Design pattern Bridge Design pattern Object Adapter design


pattern

The command design The behavior design patterns The structural design
pattern belongs to the are affected by the bridge patterns are affected by the
category of behavior design design pattern object adapter pattern.
patterns.

We modify an object's Only before the application We utilize the Adapter


interface to a common is designed may a Bridge pattern to transform an
Command interface and pattern be used. Allows for object's interface to another
provide an execute method the separation of abstraction so that we can use it from
that invokes the real and implementation. another interface.
methods that accomplish the
work in the Command
pattern.

Requests can be The emphasis is on bridging The goal is to eliminate


encapsulated as objects the gap between an incompatibilities between
using the Command pattern, abstraction and its many two existing interfaces.
allowing clients to be possible implementations.
parametrized with distinct
requests.

A command's creator is not Provides clients with a Doesn't pay attention to the
the same as the one who stable interface. implementation of certain
executes it. interfaces.

As the system matures the As the system matures, it Doesn't consider how such
command pattern can accommodate additional interfaces might grow on
accommodate the additional implementations. their own.
implementations.

 Flyweight, prototype, and singleton:


Differentiate between Flyweight, prototype, and singleton
Flyweight Design pattern Prototype Design pattern Singleton Design pattern

The composition of classes The prototype pattern The singleton pattern


and objects is the emphasis highlights the creation of highlights the creation of
of flyweight. objects automatically within objects automatically within
the code. the code.

The flyweight design pattern The prototype design pattern The singleton design pattern

April 28, 2022


5 Software design patterns

belongs to the structural belongs to the creational belongs to the creational


design patterns category. design patterns category. design patterns category.

When we have a lot of By prototyping or cloning a The singleton design pattern


similar items, we can utilize prototypical instance of an limits the total number of
the flyweight design pattern item, the prototype design instances of a class to only
to speed up our code. By pattern creates new objects. one at any given time.
sharing data among
comparable objects, this
helps to save memory and
resources.

When you have a lot of When you have the existing When you're a singleton,
different varieties of the object then the prototype is you only have one thing.
same product, it's called used to reuse the existing
flyweight. object.

 Visitor, proxy, and strategy:


Differentiate between Visitor, Proxy, and Strategy
Visitor Design pattern Proxy Design pattern Strategy Design pattern

The Visitor pattern is a Proxy is a structural design The behavior design patterns
behavioral pattern that pattern that creates an object are affected by the strategy
allows us to add new that serves as a stand-in for a adapter pattern.
functions to an object and genuine service object that a
separate the data structure client needs.
from it at run time.

The main purpose is the The main purpose of proxy The main purpose of the
ability to add new is to manages access to the strategy is to provide a
operations to existing actual object, allowing you mechanism for selecting
object structures without to do something before or different algorithms.
altering the structures is a after the request reaches the
practical effect of this real object.
separation.

Visitor pattern is used When we need to create a When you wish to employ
when similar procedures wrapper to hide the different variants of an
must be carried out on complexity of the core algorithm within an object
objects of various object from the client, we and be able to switch
categories that are placed utilize the proxy approach. between them during
together in a structure (a runtime, use the Strategy
collection or a more pattern.
complex structure).

 Builder, Abstract factory, and Factory:

April 28, 2022


6 Software design patterns

Differentiate between Builder, Abstract factory, and Factory


Builder Design pattern Abstract factory Design Factory Design pattern
pattern
This pattern is one of the most Abstract factory creates the The factory pattern creates
effective ways to make an objects using the an object by inheriting it.
object. composition.

It is used to create the group of It not only creates one It only creates the one
predefined types of the objects. product but the families of product.
the products.
A Builder class constructs Concrete factories Implements code in the
the final product one step at implement factory method abstract creator that uses
a time. This function is to produce or create the the concrete type produced
unaffected by other objects. product. by the sub class.

There will be one director class It returns the instance The factory pattern does
in the builder pattern, which directly being the abstract not return the instance
will instruct the builder class to factory design pattern. directly.
construct the various
parts/properties of our objects
and then obtain the object.

It will have a reference to the It does not maintain track It will have a reference to
object that was generated. of the object it creates. the object that was
generated.

3. For each pattern in each group, describe one design problem such that
only the pattern but not the other patterns should be applied (to solve
the design problem).
 Command, bridge, and object adapter:
Command Problem:
Create a puzzle game in which the user can only place puzzle pieces of five distinct shapes. A
piece can be rotated, zoomed in/out, and positioned anywhere it is required. Without taking a
lot of memory space, the user can save a completed game. Furthermore, the user can
repeatedly right-click the game to cancel the previous action. Which design pattern(s) could
be beneficial?
Bridge Problem:
We intend to create an app that will allow us to use many devices such as the television,
laptop, and music radio. The abstraction is the app's GUI layer, while the implementation is
the operating system's APIs. The abstraction layer oversees the app's presentation while the
implementation object oversees the actual work. We can alter GUI classes without affecting
API classes by using various layers. If we don't create new classes, we'll have to learn the
entire code and classes, which will be difficult. We can work on one class at a time, and it
will be easier to make modifications to the classes if we break the code down into various
classes like GUI and API. Which design pattern is the good choice?

April 28, 2022


7 Software design patterns

Object Adapter:
We already have a software system in place. We now must integrate a new vendor library
into our current software system. The new vendor's interfaces were created differently than
the previous vendors. To make our system function, we'll need to create a class that converts
the new vendor interface to the one we're looking for. Which pattern is the most effective?

 Flyweight, prototype, and singleton:


Flyweight Problem:
A digital piano can play a lot of music, but it only has 20 songs in its memory. It does,
however, have a memory stick extension slot. You'll need to develop and use the memory
stick feature so that you can play a selection of music. The songs on the memory stick should
consume as little memory as feasible. Which design pattern are you going to use?
Prototype Problem:
The processing of enormous data by a simulation takes several days, and the computed
results are saved in an object. The results of this object will be used in some following
studies. Which design pattern is a suitable choice to help execute the experiments
appropriately and independently, knowing that each experiment is likely to affect the contents
of the object?
Singleton Problem:
Create a printer manager that spools all of the submitted printing jobs so that they can be
printed in order. Without this printer manager, the printer may produce interleaved results
from the jobs that have been submitted. For this printer manager, which design pattern is a
viable option?

 Visitor, proxy, and strategy:


Visitor Problem:
When someone goes grocery shopping, they place everything they want in their shopping
cart, which can be thought of as a collection of object structure pieces. The cashier then
behaves as if he or she is a customer, scanning prices and/or weighing each individual
shopping item (i.e., element) to determine the total cost. Which pattern is the best?
Proxy Problem:
There are more than 20 subsystems in a math computation system. Professionals will be
interested in five of them. Because average consumers rarely utilize them, you've been
instructed to rebuild the system to reduce the amount of time it takes to start up. Which
pattern is the best fit for you?
Strategy Problem:
We intend to develop a user-friendly navigation app. While looking for a route to their
destination, users can view their fastest path. People will be able to walk or drive to their
destination. Adding various routing algorithms to previously written code had an impact on
the entire class, increasing the likelihood of an error. Which pattern is the best?

April 28, 2022


8 Software design patterns

 Builder, abstract factory, and factory method:


Builder Problem:
In addition to plain text, today's documents are likely to include a variety of web information.
The many types of web contents can be deduced from the header tag, such as html...>,
xml...>, asp...>, and so on. Create a system that can provide the user with a suitable document
object that can display plain text and any embedded web content based on the tag(s) in the
document file. Which design pattern is the best?
Abstract Factory Problem:
There are three levels of difficulty in a video game: beginner, intermediate, and advanced.
The game displays a corresponding menu, character selection window, and weapon selection
window based on the complexity of the selected mode. New modes may be added in the
future, which will necessitate the creation of new types of display elements that can be simply
integrated to match those modes. Which design pattern is the best?
Factory Problem:
Currently, a retail system sells three different sorts of remote controls: TV remote, VCR
remote, and DVD remote. All remote controls include a brand name attribute and a control
function that takes a char as an argument to reason about the pushed button. Soon, new types
of remotes are expected to be introduced, and the store system must be able to manage them
without requiring any code changes. Which design pattern is the best?

4. Describe how each pattern solves the design problem identified in item
3.
 Command, bridge, and object adapter:
Command Problem’s Solution:
Command design pattern will be the good choice for this puzzle game as this design pattern
encapsulates the command in the objects so that you can control the selection i.e., you can
rotate the piece, zoomed in/ out, or placed at the desired place,
Bridge Problem’s Solution:
According to the case study if we don't create new classes, we'll have to learn the entire code
and classes, which will be difficult. We can work on one class at a time, and it will be easier
to make modifications to the classes if we break the code down into various classes like GUI
and API. As a result, we must closely follow the bridge pattern.
Object Adapter Problem’s Solution:
According to the situation object adapter creates the interfaces that our classes anticipate and
communicates with the vendor interface to fulfil our demands. We develop new code for the
adapter so that our existing system and vendor class don't have to be changed.
 Flyweight, prototype, and singleton:
Flyweight Problem’s Solution:

April 28, 2022


9 Software design patterns

This design pattern enables a programmer to load and use any classes that implement a
recognized interface in response to a request, such as the piano's expansion slot, which allows
the memory stick to work properly and hold more than 20 songs.
Prototype Problem’s Solution:
Prototype design pattern is the good choice for this situation as according to the given
situation the system run the experiments correctly and independently and the prototype
design patterns is used when the system is independent and store the one object result and
compute the result for the next object in this situation, we must clone the object’s information
and use for the next object.
Singleton Problem’s Solution:
Singleton design pattern is the good for the printer manager that print the submitted jobs in
the sequence and without this printer manager the printer prints the interleaved results. For
the singleton design pattern, we know that there is only one instance of class that is
responsible to make sure that there is not any other instance and also provide the global point
of access.

 Visitor, proxy, and strategy:


Visitor Problem’s Solution:
Popular example of how visitor pattern’s function is going grocery shopping. When someone
goes grocery shopping, they place everything they want in their shopping cart, which can be
thought of as a collection of object structure pieces. The cashier then behaves as if he or she
is a customer, scanning prices and/or weighing each individual shopping item (i.e., element)
to determine the total cost.
Proxy Problem’s Solution:

April 28, 2022


10 Software design patterns

According to the scenario, there is a math calculation system with over 20 sub-systems, five
of which are for professionals because regular users rarely utilize them. Because instantiating
all the system's programs takes extra time to load the system at startup, the virtual proxy
design pattern is a good solution in this circumstance. This design pattern allows the system
to load the most demanding programs first, while deferring the loading of programs that
aren't needed right now.
Strategy Problem’s Solution:
Adding various routing algorithms to previously written code had an impact on the entire
class, increasing the likelihood of an error. This difficulty can be overcome by employing a
strategy pattern, which involves separating all algorithms into distinct classes known as
strategies. That’s why strategy pattern is the best suit to this situation.

 Builder, abstract factory, and factory method:


Builder Problem’s Solution:
We all know that the builder pattern is a versatile solution to a variety of object creation
issues. The Builder design pattern separates the manufacturing and representation of a
complex item. Because of the variety of web material that will be incorporated in the text
page, the builder pattern is the ideal answer for it.
Abstract Factory Problem’s Solution:
According to the case study, the video game has three modes: beginner, intermediate, and
advanced, and the game displays the menu based on the complexity of the selected mode.
Since it allows you to design an object that follows a general pattern, new modes may be
established, allowing display elements to be generated and easily blended with those modes.
You can create families of related or dependent things using the Abstract factory design
without having to declare their unique classes.
Factory Problem’s Solution:
The Factory pattern is an excellent choice in this case since there is a class remote and the
object of the remote type requires other classes to specify it, such as TV remote, VCR
remote, and DVD remote. The purpose of Factory Patterns is to define an interface for
creating objects while allowing subclasses to choose the class they want to instantiate. A
class can utilize the Factory Method to postpone subclass instantiation.

April 28, 2022


11 Software design patterns

5. Explain why the other patterns must not be applied.

 Command, bridge, and object adapter:


1. Other patterns are not applicable on the puzzle game because only the command
design pattern encapsulates the object’s command so that we can control the puzzle
game’s selection.
2. Only the bridge design pattern is applicable as the abstraction layer oversees the app's
presentation while the implementation object oversees the actual work. It will be
easier to make changes to the classes if we work on one at a time.
3. We can only utilize the object adapter pattern because we write new code for the
adapter and don't have to update our old system or vendor class.
 Flyweight, prototype, and singleton:
1. Flyweight pattern only applicable over the other design patterns when we have to
exceed the RAM like in the situation of the expansion slot of the piano to store more
songs.
2. No other pattern except the prototype pattern is applied because only the prototype
pattern can run the experiments independently.
3. Singleton pattern is applied when one instance of the class is responsible for the
whole situation not other design patterns are applicable.
 Visitor, proxy, and strategy:
1. Visitor pattern is applied for the shopping situation in this situation a behavior makes
sense in some classes of a class hierarchy but not in others, use the pattern.
2. Only the proxy pattern is applied for the situation because this design pattern enables
the system to load the most resource-intensive programs first, while postponing the
loading of programs that aren't currently required.
3. This challenge can be solved by employing a strategy pattern, which involves
breaking out all the algorithms into distinct groups called strategies. This problem
cannot be solved with a bridge pattern since the bridge pattern does not allow for the
selection of an algorithm's behaviour at runtime.
 Builder, abstract factory, and factory method:

April 28, 2022


12 Software design patterns

1. We all know that the builder pattern may be used to solve a wide range of object
construction problems rather than the abstract factory and factory pattern.
2. Using the Abstract factory design, you can establish families of linked or dependent
entities without having to declare their individual classes that’s why it’s only the
applicable design pattern on the video game situation.
3. The Factory pattern defines an object creation interface that allows subclasses to
select the class they want to instantiate other pattern does not provide this ability.

April 28, 2022

You might also like