Design Patterns
Design Patterns
Design Patterns
Design Patterns
(SE – 404)
Name
Roll No
Batch
Year
Department
Workbook
Design Patterns
(SE – 404)
Prepared by
Approved by
Chairman
Department of Computer Science & Software Engineering
For instance, if you want to write a code to access a description of shapes that were stored
in a database and then display them. The solution might contain following steps:
1. Locate the list of shapes in the database.
2. Open up the list of shapes.
3. Sort the list according to some rules.
4. Display the individual shapes on the monitor.
Step # 4 can further be broken down into following steps.
4a. Identify the type of shape.
4b. Get the location of the shape.
4c. Call the appropriate function that will display the shape, giving its shape
location.
1. Form a group of 3-5 students and write a module on step 4c “Call the appropriate
function that will display the shape (square, rectangle, triangle), giving its shape
location.” Use functional decomposition approach.
2. Add at least 10 functions of different shapes in your pseudo code.
The best way to think about an object is to think of something with responsibilities. A
good design rule is that objects should be responsible for themselves and should have
those responsibilities clearly defined. Remember that objects have data to tell the object
about itself and methods to implement required functionality. Many methods of an object
will be identified as callable by other objects. The collection of these methods is called
the object’s public interface.
Consider the classroom example of previous lab; you could write the Student object with
the method gotoNextClassroom(). There is no need to pass any parameters because each
Student object would be responsible for itself. That is, it would know:
What it needs to be able to move
How to get any additional information it needs to perform this task
Initially, there was only one kind of student – a regular student who goes from one class
to class. Note that there would be many of these “regular students” in the classroom
(system). There should have an object of each student, to track the state of each student
easily and independently of other students. However it is insufficient to require each
Student object to have its own set of methods to tell it what it can do and how to do it,
especially for the tasks that are common to all students.
A more efficient approach would be to have a set of methods associated with all students
that each one could use or tailor to his or her own needs. ‘General student’ can be defined
Sheet Metal
Theory:
Design patterns are part of the cutting edge of object-oriented technology. Object-oriented
analysis tools, books, and seminars are incorporating design patterns. The most
commonly stated reasons for studying design patterns are because patterns enable us to;
Reuse Solutions – By reusing already established designs, get a head start on your
problems and avoid designing solution from scratch. You should get the benefits
from other’s experiences and do not have to reinvent solutions. Experienced
designers reuse solutions that have worked in the past.
Establish Common Terminology – Communication and teamwork require a
common base vocabulary and a common view point of the problem. Design
patterns provide a common point of reference during the analysis and design
phase of a project.
The Gang of four1 suggests a few strategies for creating good object-oriented designs. In
particular, they suggest the following:
1. Design to interfaces.
2. Favor aggregation over inheritance.
3. Find what varies and encapsulate it.
1
Gamma, Helm, Johnson and Vlissides (Writers of Design Patterns: Elements of Reusable Object-Oriented
Software) .
FACADE
Theory:
The Adapter Pattern
Convert the interface of a class into another interface that the clients expect. Adapter let
classes work together that could not otherwise because of incompatible interfaces.
Target
Client
+request() Adaptee
+specificRequest()
Adapter
Adaptee->specificRequest()
+request()
Object:
Understanding the Strategy Pattern.
Theory:
The Strategy Pattern
Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.
Intent- Enables you to use different business rules or algorithms depending on the
context in which they occur.
Problem- The selection of an algorithm that needs to be applied depends on the client
making the request or the data being acted on. If you just have a rule in place that does
not change, you do not need a strategy pattern.
Solution- Separate the selection of an algorithm from the implementation of the
algorithm. Allows for the selection to be made based upon context.
Participants and Collaborators
Strategy specifies how the different algorithms are used.
ConcereteStrategies implement these different algorithms.
Context uses a specific ConcreteStrategy with a reference of type Strategy.
Strategy and Context interact to implement the chosen algorithm. (Sometimes
Strategy must query Context.) The Context forwards requests from its client to
Strategy.
Consequences-
The Strategy pattern defines a family of algorithms.
Switches and/or conditionals can be eliminated.
You must invoke all algorithms in the same way. (They must all have the same
interface.) The interaction between the ConcreteStrategies and the Context may
require the addition of methods that get state to the Context.
Implementation- Have the class that uses the algorithm (Context) contain an abstract
class (Strategy) that has an abstract method specifying how to call the algorithm. Each
derived class implements the algorithm as needed.
using PayPal. Elaborate the implementation of Strategy pattern with respect to this
example system and also draw diagram. Justify using the key features of Strategy pattern.
Object:
Understanding the Bridge Pattern.
Theory:
The Bridge Pattern
According to the Gang of four, the intent of the Bridge pattern is to;
“Decouple an abstraction from its implementation so that the two can vary
independently”
Intent- Decouple a set of implementations from the set of objects using them.
Problem- The derivations of an abstract class must use multiple implementations without
causing an explosion in the number of classes.
Solution- Define an interface for all implementations to use and have the derivations of
the abstract class use that.
Participants and Collaborators- Abstraction defines the interface for the objects being
implemented. Implementor defines the interface for the specific implementation classes.
Classes derived from Abstraction use classes derived from Implementor without
knowing which particular ConcreteImplementor is in use.
Consequences- The decoupling of the implementations from the objects that use them
increases extensibility. Client objects are not aware of implementation issues.
Implementation- 1) Encapsulate the implementations in an abstract class. 2) Contain a
handle to it in the base class of the abstraction being implemented.
Object:
Understanding the Abstract Factory pattern.
Theory:
The Abstract Factory Pattern
According to the Gang of Four, the intent of the Abstract Factory pattern is to;
“Provide an interface for creating families of related or dependent objects without
specifying their concrete classes.”
Intent- You want to have families or sets of objects for particular clients (or cases).
Problem- Families or related objects need to be instantiated.
Solution- Coordinates the creation of families of objects. Gives a way to take the rules of
how to perform the instantiation out of the client object that is using these created objects.
Participants and Collaborators- The AbstractFactory defines the interface for how to
create each member of the family of objects required. Typically, each family is created by
having its own unique ConcreteFactory.
Consequences- The pattern isolates the rules of which objects to use from the logic of
how to use these objects.
Implementation- Define an abstract class that specifies which objects are to be made.
Then implement one concrete class for each family. Tables or files can also be used to
accomplish the same thing.