Pattren 1
Pattren 1
This course is all about Design Patterns. In this course, we will present to you, the
most useful and famous design patterns.
In this lesson, first we will see what really are the Design Patterns. What is their
use? Why one should really use them, and how to use them?
Later, we will also see how patterns are organized, and categorized into different
groups according to their behavior and structure.
In the next several lessons, we will discuss about the different design patterns
one by one. We will go into depth and analyze each and every design pattern, and
will also see how to implement them in Java.
1
04/06/1445
INTRODUCTION
2
04/06/1445
Coupling
Degree with which methods of different modules are dependent on each other
A loose coupling is good quality
3
04/06/1445
ECOSYSTEM OF PATTERNS
Design Patterns
A design pattern provides a scheme for refining the subsystems or components of a software
system, or the relation ships between them. It describes a commonly-recurring structure of
communicating components that solves a general design problem within a particular context.
Architectural Patterns
fundamental structural organization schema for software systems. It provides a set of
predefined subsystems, their responsibilities, and includes rules and guidelines for organizing
the relationships between them.
e.g. layers, distribution, security, MVC…
4
04/06/1445
Design patterns are ―descriptions of communicating objects and classes that are
customized to solve a general design problem in a particular context.‖
In general, a pattern has four essential elements:
Pattern name,
The problem: describes when to apply the pattern.
The solution: describes the elements that make up the design, their relationships,
responsibilities, and collaborations
The results and consequences of applying the pattern: Costs and benefits of applying the
pattern.
5
04/06/1445
Have a very deep understanding of them in order to implement the correct design
pattern for the specific design problem.
First, you need to identify the kind of design problem you are facing. A design
problem can be categorized into creational, structural, or behavioral. Based to this
category you can filter the patterns and selects the appropriate one.
Recognizing when and where to use design patterns requires familiarity &
experience
6
04/06/1445
Creational • create objects for you, rather than having you instantiate objects directly.
This gives your program more flexibility in deciding which objects need to
patterns be created for a given case.
• help you compose groups of objects into larger structures, such as complex
Structural user interfaces or accounting data.
patterns • is particularly useful for making independently developed class libraries work
together.
Behavior • help you define the communication between objects in your system and
patterns how the flow is controlled in a complex program.
7
04/06/1445
CREATIONAL PATTERNS
These patterns control the way we define and design the objects, as well as how we instantiate
them. Some encapsulate the creation logic away from users and handles creation
(Factory and Abstract Factory), some focus on the process of building the objects themselves
(Builder), some minimize the cost of creation (Prototype) and some control the number of
instances on the whole JVM (Singleton).
8
04/06/1445
CREATIONAL PATTERNS
The Factory Pattern - provides a simple decision making class that returns one of
several possible subclasses of an abstract base class depending on the data that
are provided.
The Abstract Factory Pattern - Encapsulate a set of analogous factories that
produce families of objects.
The Builder Pattern - Encapsulate the construction of complex objects from their
representation; so, the same building process can create various representations
by specifying only type and content.
The Prototype Pattern - A fully initialized instance to be copied or cloned
The Singleton Pattern - Ensure that only a single instance of a class exists and
provide a single method for gaining access to it
FACTORY METHOD
A Factory design pattern also known as the ―Factory Method pattern‖ is a type of
Creational design patterns. By the name we can guess it produces or creates
something, in our case objects.
Advantage of Factory Design Pattern
Factory Method Pattern allows the sub-classes to choose the type of objects to create.
9
04/06/1445
• Product
• defines the interface of objects the factory method creates
• Concrete Product
• implements the Product interface
• factory
• declares the factory method, which returns an object of type
Product.
THE IMPLEMENTATION:
The client needs a product, but instead of creating
it directly using the new operator, it asks the
factory object for a new product, providing the
information about the type of object it needs.
10
04/06/1445
11
04/06/1445
Now let’s use factory class to create and get an object of concrete class by passing some information.
12
04/06/1445
The client code doesn't directly instantiate objects using the `new` operator. Instead, the Factory
takes the responsibility of determining which object to create based on the parameters it receives.
This superclass defines the API the factory-produced objects will adhere to, maintaining uniformity.
Benefits
Creation of different types of objects is possible at run time
It separates the object creation logic from the object usage logic
Removes duplicate code
Thus, makes changing or addition to object creation easier
Limitations
The different types of objects created must have the same parent class
The addition of new classes and interfaces could increase the complexity of the code
ABSTRACT FACTORY
Abstract Factory pattern adds a ―Factory‖ that brings together all these factories. Further, it
decides at run time which factory should be invoked. This later creates an object of a specific
family. Therefore, this pattern is also known as ―A Factory of Factories‖.
Abstract Factory Pattern in java encapsulates a group of factories in the process of object creation
When can we use Abstract Factory Pattern?
The system needs to create or compose objects at run time according to the user input
13
04/06/1445
STRUCTURE
AbstractFactory declares an interface for operations that create abstract product objects.
ConcreteProduct defines a product object to be created by the corresponding concrete factory. implements the
AbstractProduct interface.
For example we have two groups of utensils Microwave safe and non microwave safe products. If we need microwave
safe products we should use microwave safe bowl, plate and cup. We can not mix microwave safe and non microwave
safe. When we need to avoid mixing we can use abstract factory pattern by creating factories for each.
Implement Abstract Factory Pattern:
Find out the different object types in the application
Create Interface and implementing classes for each type
For eg, Utensils : Plate, Bowl, Cup, Plate_MW, Bowl_MW, Cup_MW ( _MW is used for microwave safe products)
Create factory classes to group the classes
Microwave safe: Plate_MW, Bowl_MW, Cup_MW
Non Microwave safe: Plate, Bowl, Cup
Declare Abstract factory interface and declare all required methods from factory
Implement Abstract factory interface by created families
Create code which will use Abstract factory to get factory and then call the methods on that factory
Use the abstract factory in the code instate of objects directly
14
04/06/1445
ConcreteFactory
ConcreteFactory
AbstractProduct
ConcreteProduct
15
04/06/1445
We have two different type of utensils microwave safe and non microwave safe, as a result, we create factory
classes
we will create our AbstractUtensilFactory interface. It groups the other factories together.
Now we will implement the AbstractUtensilFactory interface by the factories created earlier.
16
04/06/1445
Finally, all code is in place and we need to use it. Because, we have two factories, we need to get the
appropriate factory first. Once we get the factory we can call the require methods from the factory .
Let’s create a class FactoryProducer which will provide the factory instance as per the requirement.
if("Microwave".equalsIgnoreCase(choice)){
return new MicrowaveSafeFactory();
}
else if("Non-Microwave".equalsIgnoreCase(choice)){
return new NonMicrowaveSafeFactory();
}
return null;
}
}
Now, we can start using the produces and factory classes where ever we need.
17
04/06/1445
Limitations
Only useful when we have to group processes or objects
Before getting object or calling the function we need to get the factory which adds one more
processes
Adds more classes and abstraction hence code could become complex
CONCLUSION
Software Design Pattern Descriptions of reusable solutions to common software design problems.
18
04/06/1445
SOME BOOKS
QUESTIONS……
http://sourcemaking.com/design_patterns
http://java.sun.com/blueprints/patterns/index.html
http://www.codeproject.com/KB/architecture/#Design Patterns
http://msdn.microsoft.com/en-us/magazine/cc301852.aspx
http://www.javacamp.org/designPattern/
http://www.javaworld.com/channel_content/jw-patterns-index.html
http://www.ibm.com/developerworks/java/tutorials/j-patterns201
19