Module 5
Module 5
Module 5
Module Number: 05
•Prototype pattern refers to creating duplicate object while keeping performance in mind.
•This pattern involves implementing a prototype interface which tells to create a clone of the current
object. This pattern is used when creation of object directly is costly.
•Singleton
A class of which only a single instance can exist.
• The most common use of a Singleton pattern is for server applications like a Java based Remote Method
Invocation (RMI) server application.
Structural design patterns
� These design patterns are all about Class and Object composition.
� Structural class-creation patterns use inheritance to compose interfaces.
� Structural object-patterns define ways to compose objects to obtain new
functionality.
When to use Structural Design Patterns
Adapting to Interfaces: Use structural patterns like the Adapter pattern when you need to make
existing classes work with others without modifying their source code.
Organizing Object Relationships: Structural patterns such as the Decorator pattern are useful
when you need to add new functionalities to objects by composing them in a flexible and reusable
way, avoiding the need for subclassing.
Simplifying Complex Systems: When dealing with complex systems, structural patterns like the
Facade pattern can be used to provide a simplified and unified interface.
Managing Object Lifecycle: The Proxy pattern is helpful when you need to control access to an
object, either for security purposes, to delay object creation, or to manage the object’s lifecycle.
Hierarchical Class Structures: The Composite pattern is suitable when dealing with hierarchical
class structures where clients need to treat individual objects and compositions of objects
uniformly.
Advantages of Structural Design Patterns
❖ Flexibility and Adaptability Disadvantages of Structural Design Patterns
❖ Complexity
❖ Code Reusability
❖ Overhead
❖ Simplified Integration
❖ Maintenance Challenges
❖ Easier Maintenance:
❖ Limited Applicability
❖ Solves Recurring Design Problems
• Adapter
Match interfaces of different classes
• Bridge
Separates an object’s interface from its implementation
• Composite
A tree structure of simple and composite objects
• Facade
A single class that represents an entire subsystem
• Proxy
An object representing another object
• Decorator
Decorator pattern allows to add new functionality an existing object without altering its structure
• Flyweight Pattern
Flyweight pattern is primarily used to reduce the number of objects created, to decrease memory footprint and increase performance
• Adapter
Match interfaces of different classes
• In the Adapter pattern, an object provides an implementation of an interface used by other objects in a
consistent way.
� This pattern involves a single class which is responsible to join functionalities of independent or incompatible
interfaces.
A real life example could be a case of card reader which acts as an adapter between memory card and a laptop.
� You plugins the memory card into card reader and card reader into the laptop so that memory card can be read
via laptop.
� We are demonstrating use of Adapter pattern via following example in which an audio player device can play
mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files.
•Proxy
An object representing another object
•A Proxy pattern constitutes use of proxy objects during object interaction.
•A proxy object acts as a substitute for the actual object.
•Use of proxy objects is prevalent in remote object interaction protocols.
As an example, when an object needs to interact with a remote object, say across a network, the most
preferred way of encapsulating and hiding the interaction mechanism is by using a proxy object that
mediates communication between the requesting object and the remote object.
Composite Pattern
� Composite pattern is used where we need to treat a group of objects in
similar way as a single object.
� This pattern creates a class contains group of its own objects. This class
provides ways to modify its group of same objects.
https://www.tutorialspoint.com/design_pattern/design_pattern_quick_guide.htm
Bridge Pattern
� Bridge is used where we need to decouple an abstraction from its
implementation so that the two can vary independently.
� This type of design pattern comes under structural pattern as this
pattern decouples implementation class and abstract class by providing
a bridge structure between them.
� This pattern involves an interface which acts as a bridge which
makes the functionality of concrete classes independent from interface
implementer classes.
� Both types of classes can be altered structurally without affecting each
other.
� We are demonstrating use of Bridge pattern via following example in
which a
circle can be drawn in different colors using same abstract class method
but different bridge implementer classes.
Facade Pattern
� Facade pattern hides the complexities of the system and provides an
interface to the client using which the client can access the system.
� This type of design pattern comes under structural pattern as this
pattern adds an interface to exiting system to hide its
complexities.
� This pattern involves a single class which provides simplified
methods which are required by client and delegates calls to existing
system
Flyweight Pattern
� Flyweight pattern is primarily used to reduce the number of objects
created, to decrease memory footprint and increase performance.
� This type of design pattern comes under structural pattern as this pattern
provides ways to decrease objects count thus improving application required
objects structure.
We'll demonstrate this pattern by drawing 20 circle of different locations but we'll
creating only 5 objects.
� Invoker object looks for the appropriate object which can handle this command and
pass the command to the corresponding object and that object executes the
command.
Interpreter Pattern
� Interpreter pattern provides way to evaluate language grammar or
expression.
� This type of pattern comes under behavioral patterns.
� This pattern involves implementing a expression interface which tells to
interpret a particular context.
� This pattern is used in SQL parsing, symbol processing engine etc.
Iterator Pattern
� Iterator pattern is very commonly used design pattern in Java and .Net
programming environment.
� This pattern is used to get a way to access the elements of a
collection object in sequential manner without any need to know
its underlying representation.
� Iterator pattern falls under behavioral pattern category.
Mediator Pattern
� Mediator pattern is used to reduce communication complexity
between multiple objects or classes.
� This pattern provides a mediator class which normally handles all the
communications between different classes and supports easy
maintainability of the code by loose coupling.
� Mediator pattern falls under behavioral pattern category.
Memento Pattern
Observer Pattern Memento pattern is used to reduce where we
Observer pattern is used when there is one to want to restore state of an object to a previous
many relationship between objects such as if one state.
object is modified, its depenedent objects are to be
notified automatically.
Memento pattern falls under behavioral pattern
category.
Observer pattern falls under behavioral pattern
category.
State Pattern
� In State pattern a class behavior changes based on its state. This type
of design pattern comes under behavior pattern.
� In State pattern, we create objects which represent various states and a
context object whose behavior varies as its state object changes.
Template Pattern
In Template pattern, an abstract class exposes defined way(s)/template(s) to
execute its methods.
Its subclasses can overrides the method implementations as per need basis
but the invocation is to be in the same way as defined by an abstract class.
This pattern comes under behavior pattern category.
Strategy Pattern
In Strategy pattern, a class behavior or its algorithm can be changed at run
time. This type of design pattern comes under behavior pattern.
The strategy object changes the executing algorithm of the context object.
Visitor Pattern
� In Visitor pattern, we use a visitor class which changes the executing
algorithm of an element class.
� By this way, execution algorithm of element can varies as visitor varies.
This pattern comes under behavior pattern category.
� As per the pattern, element object has to accept the visitor object so
that visitor object handles the operation on the element object.
MVC Pattern
� MVC Pattern stands for Model-View-Controller Pattern. This pattern is
used to separate application's concerns.
• Model - Model represents an object or JAVA POJO carrying data. It
can also have logic to update controller if its data changes.
• View - View represents the visualization of the data that model
contains.
• Controller - Controller acts on both Model and view. It controls the
data flow into model object and updates the view whenever data
changes. It keeps View and Model separate.
CASE STUDIES
ATM Modelling:
• The Object-Oriented Modelling assists the programmer to address the complexity of a problem domain
by considering the problem not as a set of functions that can be performed but primarily as a set of
related, interacting Objects.
• This Modelling is based on the approach of Object-Oriented Modelling through Unified Modelling
Language (UML) for an ATM system in the Banking sector.
• The main aim of this Modelling is to provide a flexible and faithful environment for a customer, who
wants to do online banking transactions.
• After successful authentication, the customer can withdraw the desired amount (within the prescribed
limit) from the ATM machine or can transfer amount to another account. A user can also change the pin
code.
• The ATM has solved many problems, and now the customer can have money transaction 24/7. In this
article UML Class, Interaction diagram, Activity diagram and Use Case diagram are also designed for
the ATM System.
https://www.startertutorials.com/uml/category/case-studies 33
ATM Modelling:
38
Checkout use case also includes Payment use case
which could be done either by using credit card and
external credit payment service or with PayPal.
39
What is cruise control?
•The purpose of a cruise control system is to accurately maintain a speed set by the driver without any
outside intervention by controlling the throttle-accelerator pedal linkage.
•The earliest variants of cruise control were actually in use even before the creation of automobiles.
•The inventor and mechanical engineer James Watt developed a version as early as the 17th century,
which allowed steam engines to maintain a constant speed up and down inclines.
•Cruise control as we know it today was invented in the late 1940s, when the idea of using an
electrically-controlled device that could manipulate road speeds and adjust the throttle accordingly
was conceived.
40
How does cruise control work?
•The cruise control system controls the speed of your car the same way
you do – by adjusting the throttle (accelerator) position.
•The throttle valve controls the power and speed of the engine by
limiting how much air it takes in (since it’s an internal combustion
engine).
41
•The driver can set the cruise control with the cruise switches, which usually consist of ON,
OFF, RESUME, SET/ACCEL and COAST.
•These are commonly located on the steering wheel or on the windshield wiper or turn signal
stalk.
•The SET/ACCEL knob sets the speed of the car.
•One tap will accelerate it by 1 mph, two by 2 mph and so on.
•Tapping the knob in the opposite direction will decelerate the vehicle.
•As a safety feature, the cruise control system will disengage as soon as you hit the brake
pedal.
42
Fig: Class diagram
43
cellular network