Professional Documents
Culture Documents
Pattern Assignment
Pattern Assignment
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.
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.
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.
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.
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.
The flyweight design pattern The prototype design pattern The singleton design pattern
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.
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).
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?
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?
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:
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.
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.
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.