1. SOFTWARE DESIGN AND ARCHITECTURE
LECTURE 24-26
Disclaimer: The contents of this
presentation have been taken from
multiple sources, i.e. Books,
Research articles, Thesis
publications, and websites.
2. The material used in this presentation i.e., pictures/graphs/text, etc. is solely
intended for educational/teaching purpose, offered free of cost to the students for
use under special circumstances of Online Education due to COVID-19 Lockdown
situation and may include copyrighted material - the use of which may not have
been specifically authorised by Copyright Owners. It’s application constitutes Fair
Use of any such copyrighted material as provided in globally accepted law of many
countries. The contents of presentations are intended only for the attendees of the
class being conducted by the presenter.
Fair Use Notice
4. Why study design pattern?
Experts in software architecture and design are highly paid, because they know how to create
designs that are flexible, elegant, and reusable.
You become an expert through experience, reusing solutions that worked for you before.
Patterns describe solutions to design problems that occur over and over again.
5. SINGLETON Pattern
Intent: Ensure a class only has one instance, and provide a global point of access to it.
Motivation: In many systems, there should often only be one object instance for a given class
Print spooler
File system
Window manager
How do we ensure that a class has only one instance and that the instance is easily accessible?
the singleton pattern is a software design pattern that restricts the instantiation of a class to
one "single" instance. This is useful when exactly one object is needed to coordinate actions
across the system.
6. Creating A Single Instance
This maybe necessary because:
More than one instance will result in incorrect program behavior
More than one instance will result in the overuse of resources
There is a need for a global point of access
8. SINGLETON Pattern
Applicability
Use the Singleton pattern when
there must be exactly one instance of a class, and it must be accessible to clients from a well-known
access point
Make the class of the single instance object responsible for creation, initialization, access, and
enforcement. Declare the instance as a private static data member. Provide a public static member
function that encapsulates all initialization code, and provides access to the instance.
10. SINGLETON Pattern: Consequences
Controlled access to sole instance
As the constructor is private, the class controls when an instance is created
Reduced name space
Eliminates the need for global variables that store single instances
Permits refinement of operations and representations
You can easily sub-class the Singleton
11. SINGLETON PATTERN: Implementation
public class ClassicSingleton
{
private static ClassicSingleton instance = null; variable
private ClassicSingleton() { // Exists only to defeat instantiation. } constructor outsider
accesss na krske
public static ClassicSingleton getInstance() { method
if(instance == null)
{ instance = new ClassicSingleton(); }
return instance;
}
}
13. Singleton Pattern
Known use: A known use for singleton pattern is where a global resource is shared, Also used
with metaclasses.
Related Patterns: Many patterns can be implemented using the Singleton pattern.
AbstractFactory, Builder, Prototype.
There are two forms of singleton design pattern
Early Instantiation: creation of instance at load time.
Lazy Instantiation: creation of instance when required.
14. Example
The Singleton pattern ensures that a class has only one instance and provides a global point of
access to that instance. It is named after the singleton set, which is defined to be a set
containing one element. The office of the President of the United States is a Singleton. The
United States Constitution specifies the means by which a president is elected, limits the term of
office, and defines the order of succession. As a result, there can be at most one active
president at any given time. Regardless of the personal identity of the active president, the title,
"The President of the United States" is a global point of access that identifies the person in the
office.
15. Example 2 - Configuration Classes
The Singleton pattern is used to design the classes which provides the configuration settings for an
application. By implementing configuration classes as Singleton not only that we provide a global access
point, but we also keep the instance we use as a cache object. When the class is instantiated( or when a
value is read ) the singleton will keep the values in its internal structure. If the values are read from the
database or from files this avoids the reloading the values each time the configuration parameters are
used.
16. Abstract Factory Pattern
The Abstract Factory design pattern falls under the Creational design pattern category and it
provides a way to encapsulate a group of factories that have a common link without highlighting
their concrete classes.
In Abstract Factory pattern an interface is responsible for creating a factory of related objects
without explicitly specifying their classes. Each generated factory can give the objects as per the
Factory pattern.
18. Abstract Factory Pattern Participants
•AbstractFactory : Declares an interface for operations that create abstract product
objects.
•ConcreteFactory : Implements the operations declared in the AbstractFactory to
create concrete product objects.
•Product : Defines a product object to be created by the corresponding concrete
factory and implements the AbstractProduct interface.
•Client : Uses only interfaces declared by AbstractFactory and AbstractProduct
classes.
22. How Factory Pattern works in Real Life ?
1 Orders a Dish from Menu
2
Receives the Order
Creates the Dish
4 Delivers the Dish
3 Outsources to Chef
Abstract Factory Pattern
23. Motivation
Consider a garments factory specialized in creating trousers and shirts. Now the Parent company which
is a famous Retail brand is now venturing into Gadget section. They are also planning to expand their
Factories having one centre in US and another one in UK. The client should be completely unaware of
how the objects are created. What is the best design pattern we can use to resolve this requirement?
Abstract Factory Pattern
24. Applicability
Use the Abstract Factory pattern when
a system should be independent of how its products are created, composed, and represented.
a system should be configured with one of multiple families of products.
you want to provide a class library of products, and you want to reveal just their interfaces, not their
implementations.
You can be sure that the products you’re getting from a factory are compatible with each other.
The code may become more complicated than it should be, since a lot of new interfaces and classes are
introduced along with the pattern.
Abstract Factory Pattern
26. Builder Pattern
Builder is a creational design pattern that lets you construct complex objects step by step. The
pattern allows you to produce different types and representations of an object using the same
construction code.
Motivation
For example, you can consider construction of a home. Home is the final end product (object) that is to
be returned as the output of the construction process. It will have many steps, like basement
construction, wall construction and so on roof construction. Finally the whole home object is returned.
Here using the same process you can build houses with different properties.
Consider a class which is used to create Cake, now you need number of items like egg, milk, flour to
create cake. many of them are mandatory and some of them are optional like cherry, fruits etc.
27. What is the difference between abstract
factory and builder pattern?
Abstract factory may also be used to construct a complex object, then what is the difference
with builder pattern?
In builder pattern emphasis is on ‘step by step’.
Builder pattern will have many number of small steps. Those every steps will have small units of logic
enclosed in it.
There will also be a sequence involved.
It will start from step 1 and will go upto step n and the final step is returning the object.
In these steps, every step will add some value in construction of the object. That is you can imagine that
the object grows stage by stage.
Builder will return the object in last step, but in abstract factory how complex the built object might be,
it will not have step by step object construction.
29. Applicability
Use the Builder pattern when
the algorithm for creating a complex object should be independent of the parts that make up the object and how
they're assembled.
the construction process must allow different representations for the object that's constructed.
When you want to hide creation process from the user.
Builder Pattern
35. The following interaction diagram illustrates how Builder and Director cooperate with a client
Builder Pattern
36. Consequences
It lets you vary a product's internal representation.
It isolates code for construction and representation.
It gives you finer control over the construction process.
Builder Pattern
37. Prototype Pattern
A prototype is a template of any object before the actual object is constructed.
Prototype is a creational design pattern that lets you copy existing objects without making your
code dependent on their classes.
Prototype design pattern is used in scenarios where application needs to create a number of
instances of a class, which has almost same state or differs very little.
Intent
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying
this prototype.
39. Prototype Pattern
Motivation
Today’s programming is all about costs. Saving is a big issue when it comes to using computer resources,
so programmers are doing their best to find ways of improving the performance When we talk about
object creation we can find a better way to have new objects: cloning.
To this idea one particular design pattern is related: rather than creation it uses cloning. If the cost of
creating a new object is large and creation is resource intensive, we clone the object.
40. Say you have an object, and you want to create an exact copy of it. How would you do it? First, you have to create a
new object of the same class. Then you have to go through all the fields of the original object and copy their values
over to the new object.
Nice! But there’s a catch. Not all objects can be copied that way because some of the object’s fields may be private
and not visible from outside of the object itself.
42. Prototype Pattern
This pattern involves implementing a prototype interface which tells to create a clone of the
current object.
For example, a object is to be created after a costly database operation. We can cache the
object, returns its clone on next request and update the database as and when needed thus
reducing database calls.
43. Prototype Pattern
Applicability
Use the Prototype pattern
when a system should be independent of how its products are created, composed, and represented; and
when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
when instances of a class can have one of only a few different combinations of state. It may be more convenient to
install a corresponding number of prototypes and clone them rather than instantiating the class manually, each
time with the appropriate state.
45. Prototype Pattern
Implementation (tips and problems)
The prototype design pattern mandates that the instance which you are going to copy should provide
the copying feature. It should not be done by an external utility or provider.
Also make sure that instance allows you to make changes to the data. If not, after cloning you will not
be able to make required changes to get the new required object.
Using a prototype manager (a registry service)