DPAT Assignment Part 1
DPAT Assignment Part 1
SECTION 1
TECHNOLOGY PARK MALAYSIA
CT070-3-3-DPAT
DESIGN PATTERNS
APU3F2302SE
WEIGHTAGE: 20%
2.0 Introduction..............................................................................................................................2
4.1 Scenario..................................................................................................................................8
6.0 References...............................................................................................................................19
Table of Figures
1|Page
2.0 Introduction
The Design stage of the Software Development Life Cycle is responsible for producing a
solution to an issue specified in the SRS (Software Requirement Specification) file. The design
phase produces a Software Design Document (SDD).
Coupling and cohesion are two fundamental concepts in software engineering that are used to
assess the design quality of a software system.
The level of interconnection among the software components is referred to as coupling. Modules
with high coupling have strong connections, and modifications in a single module might impact
other modules. Modules with low coupling are autonomous, and modifications in a single
module have no effect on the other modules (Josikakar, 2021).
Coupling and cohesion are crucial variables that influence a software system's reliability,
flexibility, and dependability. A system with high coupling and low cohesion can be hard to
modify as well as examination, whereas a system with low coupling and high cohesion is simpler
to maintain and increase (Josikakar, 2021).
As it shows in the figure below, design is essentially a two-part continuous procedure. The first
component is Conceptual Design, which informs the client about what the whole thing will do.
The second step is technical design, enable system builders to comprehend the real hardware and
software required to fix the clients issue (Josikakar, 2021).
2|Page
Figure 1: Design Parts (Josikakar, 2021)
High coupling raises interconnection among modules, causing the whole thing harder and
more complicated to comprehend.
Reduced flexibility: Because of the high coupling, it is hard to modify or substitute
particular parts with no impacting the system as a whole.
Reduced modularity: High coupling causes developing and testing components
independently harder, thereby decreasing code modularity and reusability.
3|Page
3.0 Literature review
Object-oriented (OO) design patterns are groups of interrelated classes that are used for a
specific purpose. A design pattern is an element of structure in a pattern-based system, similar to
how an operation is a fundamental unit in a function-based system (Bieman).
We used the following process to search the available literature explaining particular pattern
applications for instances of pattern coupling (Bieman):
1. Search for instances of interrelated patterns in the most recent design pattern applications
literature.
2. Create a list of all examples of Gamma et al design patterns that you can find in the literature.
3. Make a sub-list of all pattern categories utilised as pairs or various sets that interact Gamma et
al patterns.
4. Sort the list by coupling type (tightly compared to loosely coupled) and communication type
(intersection, structure, or embedding), providing an explanation for each grouping.
5. Assess the discovered situations of coupled patterns on the basis of appealing software
qualities (maintenance, factorability, reusability, as well as simple implementation), providing
justification for each grouping.
6. Analyse the data to gain a better understanding of the pattern coupling methods that, in
overall, contribute to design "goodness" while others ought to be utilised only to meet particular
application requires.
We restricted ourselves to coupled pattern groups that are entirely made up of patterns identified
by Gamma et al.
4|Page
The 23 Gamma et al OO patterns comprise the study pattern set. The patterns are divided into
three major groups and are listed below with descriptions taken from Gamma et al (Bieman):
Creational Pattern:
Factory Method: Define an interface for creating an object, but let subclasses decide
which class to instantiate.
Abstract factory: Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
Builder: Separate the construction of a complex object from its representation so that the
same construction process can produce multiple representations."
Prototype: Specify the types of objects to create using a prototypical instance, and copy
this prototype to create new objects."
Singleton: Ensure that a class has only one instance and provides a global point of
contact.
Structural Patterns
Adapter: Convert a class's interface into another interface that clients expect.
Bridge: Decouple an abstraction from its implementation so that the two can vary
independently.
Composite: Build objects into tree structures to represent part-whole hierarchies.
Decorator: Attach additional responsibilities to an object dynamically.
Façade: a unified interface to a set of interfaces in a subsystem.
Flyweight: Use sharing to efficiently support a large number of fine-grained objects.
Proxy: Serve as a surrogate or placeholder for another object in order to control access to
it.
Behavioral Patterns
Chain of Responsibility: Refrain from coupling the inquiry's sender to its receiver...
Chain receive items and pass the query along through the chain till it is handled by a
component."
Command: Encapsulate an inquiry as an object, allowing you to define customers using
various requests.
5|Page
Interpreter: considering a language, establish a grammar illustration as well as an
interpreter.
Iterator: Offer a method for repeatedly accessing each component of a collection object
with no showing it’s behind representation.
Mediator: Define a thing that encompasses the way a set of objects interact.
Memento: Capture and externalise the internal condition of an object so that it may be
brought back to this indicate later.
Observer: Specify a one-to-many dependability among objects so that when one object
modifications nation, all it’s the dependents get informed as well as updated.
State: Allow an object's behaviour modify when its internal configuration shifts."
Strategy: Identify a group of algorithms, encapsulate every one of them, and then render
these individuals interchangeable," says the.
Template: Identify the basic structure of an algorithm in a procedure, putting off certain
phases to subclasses.
Visitor: Represent a procedure to be carried out on each component of a physical
structure.
We found all of the coupled patterns. All pattern connects or numerous which link together make
up such coupled patterns. We categorised linked pattern sets based on loose vs. tight coupling
and pattern interacting types:
Types of couplings
Loosely Coupled: Patterns are loosely tied together with few connections, making separation
simpler. The organisation should encourage faster and simpler future design modifications
(Bieman).
Tightly Coupled: Patterns are tightly coupled when they have numerous connections as well as
dependence. It will be challenging to make small modifications to one of the patterns without
first impacting any of the others (Bieman).
6|Page
Intersecting patterns demonstrate an exchange of information and communication scheme that
"talks to" or "uses a." While we discovered no proof that the design was both Composite material
and Embedded, we divided it as an intersection type (Bieman).
Demonstrates one of the coupled patterns discovered by Masuda et al. In this example, a
Template Method pattern example is linked to a Builder pattern example via a common class.
Class Translator is used in both patterns. The connection is of the type "talks with a," which
classifies the patterns as "Intersection (Bieman).
The collected information effectively contains additional instances for the intersection category
of pattern connection. The majority of coupled pattern sets are intersections (Bieman).
High coupling indicates that the module is well-versed in the internal operations of various other
modules. Modules that know excessively regarding other modules complicate changes and
render modules brittle. If Module A understands excessively about Module B, changes to
Module B's internals may break Module A's functionality (Montiel, 2018).
By aiming for low coupling, it are capable of making modifications to module internals with no
concern about the way they will affect other modules in the system as a whole. Because our
7|Page
modules are not interrelated, low coupling makes it simpler to create, write, and test code
(Montiel, 2018).
The mediator pattern is used to reduce the complexity of communication among numerous
objects or classes. This pattern offers a mediator class that handles every interaction among
various classes and allows for easy code maintenance through loose coupling. The mediator
pattern is classified as a behavioral pattern (tutorialspoint.com, n.d.).
8|Page
4.2 Class Diagram
The main class is responsible for carrying out the ordering process, which includes showing the
menu, customizing the Ice Cream order, and making payment.
9|Page
Figure 4: Code snippets for main method and menu() method
In the figure above, it shows the code snippets for the main method. As the menu () method is
called to display the menu for customers and to get their order by calling order method as shown
in figure 4.
10 | P a g e
Figure 6: Code snippets for selecting ice cream topping
After customers choose the ice cream flavor, the system will prompt again for the customers to
choose the toppings they need. Customers have 3 options of flavors, so they can select one of the
topping from 1 – 3, and if they don’t need any toppings they can select option 4 “No Toppings”
and then they can proceed to the payment.
11 | P a g e
After customers are done with choosing their ice cream flavor and toppings, the total price of the
ice cream will be calculated, then customers can move to choose the payment method they want
whether using credit card or e-wallet.
The Ice Cream class is abstract, with one abstract method getPrice() that will be modified by its
child classes.
The figure above is showing the code of the ice cream class.
Figure 9: Code snippets for child class that inherits Waffle class
In the figure above is an example of the Vanilla_IceCream class extends the IceCream class and
overrides the parent class's abstract getPrice() method to determine its own behaviour, allowing it
to set the ice cream price for the original flavor to RM 4.
12 | P a g e
Figure 10: Code snippets for add topping methods
Ice cream class also includes methods for adding toppings and defines payment methods.
13 | P a g e
5.0 Refined Solution
5.1 Design issues in simple solution
There are some problems with the first system design, which are primarily due to inheritance
limitations. VanillaIceCream, ChocolateIceCream, and StrawberryIceCream all have a direct
relying on the IceCream class in some situations. This is demonstrated by the fact that they
inherit from the IceCream class and directly access its attributes.
When classes are highly coupled and rely on each other's details of implementation, this is
referred to as high coupling. In this situation, subclasses have immediate knowledge of the
IceCream class that can cause problems if the IceCream class is changed. Any changes or
updates to the IceCream class would necessitate corresponding modifications in the subclasses,
increasing the likelihood of erroneous and upkeep challenges.
It would be preferable to create a layer of abstraction among the subclasses and the IceCream
class to decrease coupling and enhance the design. This can be accomplished by defining an
interface that is common for the ice cream flavors through interfaces or abstract classes. Through
doing that way, the subclasses may depend on the abstraction instead of calling the IceCream
class directly.
We may apply an abstract base class or interface for both components as well as decorator
classes to decrease coupling in the decorator design pattern. As a result, the decorator class will
only contain references to the abstract base class or interface, reducing coupling. The Decorator
pattern is a structural design pattern that allows behaviour to be dynamically added to an object.
It provides a flexible alternative to subclassing for extending an object's functionality. The
14 | P a g e
pattern entails creating a decorator class that wraps the original object and adds or modifies
existing behaviours to provide additional functionality.
The Decorator pattern can be used in the context of Yummy Ice Cream to solve the high
coupling problem among ice cream flavours and toppings. Rather than creating separate classes
for each ice cream flavour and topping combination, we may create a base IceCream interface or
class which represents the fundamental ice cream object. Then, for each topping, we can make
decorators (for example, CaramelDecorator, M&MsDecorator, OreosDecorator) that wrap the
base ice cream object and provide extra topping functionality. The ice cream flavours and
toppings are thus decoupled, allowing us to continually add or remove toppings as required.
The Strategy pattern is a behavioural design pattern that allows for the runtime selection of an
algorithm. It defines a family of interchangeable algorithms and contains each algorithm in its
own class. The pattern enables the algorithms to be chosen and used interchangeably depending
on the context or client's needs.
The Strategy pattern can be used to handle the pricing of different ice cream flavours in the case
of Yummy Ice Cream. Each flavour of ice cream can have its own pricing strategy.
VanillaIceCream, ChocolateIceCream, and StrawberryIceCream, for example, can each have
their own PriceStrategy implementation. The PriceStrategy interface or abstract class can define
methods such as calculatePrice(), which computes the price based on the ice cream flavour. We
can use the Strategy pattern to dynamically choose and implement the right pricing approach
based on the ice cream flavour, supporting loose coupling and flexibility in dealing with pricing
variations.
To summarise, we can dynamically add toppings to the ice cream using the Decorator pattern
without highly coupling flavours and toppings. We are able to manage cost variations for
different ice cream flavours using the Strategy pattern without directly coupling the pricing logic
with the ice cream classes.
15 | P a g e
5.3 Refined class diagram
16 | P a g e
5.4 Refined Java implementation
Java code has been implemented for the refined solution based on the refined class diagram that
shows in the figure12. In the refined class diagram 7 classes has been added and sum up with
total of 7 classes. It is easy to notice that the design of the Main class has not changed much. The
overall programme flow remains the same as in the original design. The distinction is most
noticeable in the Ice Cream class.
17 | P a g e
As it shows in the figures below, the following modification is the inclusion of the Strategy
pattern in the payment part. To allow for various payment methods, a payment strategy interface
is provided.
18 | P a g e
6.0 References
Bieman, W. B. (n.d.). Coupling of Design Patterns: Common Practices and Their Benefits. 25th Annual
International Computer Software and Applications Conference. COMPSAC 2001.
Josikakar. (2021, November 6). Software Engineering | Coupling and Cohesion. Retrieved from
geeksforgeeks: https://www.geeksforgeeks.org/software-engineering-coupling-and-cohesion/
Montiel, I. (2018, Septemebr 18). Low Coupling, High Cohesion. Retrieved from medium:
https://medium.com/clarityhub/low-coupling-high-cohesion-3610e35ac4a6
19 | P a g e