0% found this document useful (0 votes)
43 views13 pages

BITS Pilani: Object Oriented Programming (CS F213) Design Patterns

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

Object Oriented Programming (CS F213)

Design Patterns
Dr. Barsha Mitra
BITS Pilani CSIS Dept, BITS Pilani, Hyderabad Campus
Hyderabad Campus
Design Pattern
•general repeatable solution to a commonly occurring
problem in software design
•isn't a finished design that can be transformed directly into
code
•description or template for how to solve a problem that can
be used in many different situations

BITS Pilani, Hyderabad Campus


Parts of a Design Pattern

• A short name
• A brief description of the context
• A lengthy description of the problem
• A prescription for a solution

BITS Pilani, Hyderabad Campus


Uses of a Design Pattern

• can speed up the development process


• by providing tested, proven development paradigms
• provide general solutions, documented in a format that
doesn't require specifics tied to a particular problem
• allow developers to communicate using well-known, well
understood names for software interactions

BITS Pilani, Hyderabad Campus


Singleton Pattern
Context
1. All clients need to access a single shared instance of a class.
2. You want to ensure that no additional instances can be created
accidentally.
Solution
1. Define a class with a private constructor.
2. The class constructs a single instance of itself.
3. Supply a static method that returns a reference to the single instance.

BITS Pilani, Hyderabad Campus


Singleton Pattern
public class DBManager
{
private static DBManager dbm=null;
public static DBManager getInstance()
{ if(dbm==null)
{
dbm= new DBManager();
}
return dbm;
}
private DBManager()
{
}
}

BITS Pilani, Hyderabad Campus


Iterator Pattern
Context
1. An object (which we’ll call the aggregate) contains other objects
(which we’ll call elements).
2. Clients (that is, methods that use the aggregate) need access to
the elements sequentially.
3. The aggregate should not expose its internal structure.
4. There may be multiple clients that need simultaneous access.

BITS Pilani, Hyderabad Campus


Iterator Pattern
Solution
1. Define an iterator class that fetches one element at a time.
2. Each iterator object needs to keep track of the position of the
next element to fetch.
3. If there are several variations of the aggregate and iterator
classes, it is best if they implement common interface types. Then
the client only needs to know the interface types, not the concrete
classes.

BITS Pilani, Hyderabad Campus


Iterator Pattern

• ConcreteAggregate’s responsibility
is to instantiate a ConcreteIterator
that can iterate over its collection
of objects.
• Client class refers (1) to the
Aggregate interface for creating an
Iterator object (createIterator())
and (2) to the Iterator interface for
traversing an Aggregate object
(next()).

BITS Pilani, Hyderabad Campus


Composite Pattern
Context
1. Primitive objects can be combined into composite objects.
2. Clients treat a composite object as a primitive object.
Solution
1. Define an interface type that is an abstraction for the primitive
objects.
2. A composite object contains a collection of primitive objects.
3. Both primitive classes and composite classes implement that
interface type.
4. When implementing a method from the interface type, the
composite class applies the method to its primitive objects and
combines the results.
BITS Pilani, Hyderabad Campus
Composite Pattern

BITS Pilani, Hyderabad Campus


References
• https://sourcemaking.com/design_patterns
• www.geeksforgeeks.org
• wikipedia

BITS Pilani, Hyderabad Campus


BITS Pilani, Hyderabad Campus

You might also like