0% found this document useful (0 votes)
63 views6 pages

Sadp Unit 3 Modified

The document discusses design patterns including creational patterns like the Abstract Factory, Builder, Factory Method, and Prototype patterns. It provides examples of how each pattern works, describing the problem it addresses, its structure involving abstract and concrete classes, and consequences of using the pattern like increased flexibility. The Factory Method pattern for example defines an interface for creating an object but lets subclasses decide which object to create.

Uploaded by

Kesava 1642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

Sadp Unit 3 Modified

The document discusses design patterns including creational patterns like the Abstract Factory, Builder, Factory Method, and Prototype patterns. It provides examples of how each pattern works, describing the problem it addresses, its structure involving abstract and concrete classes, and consequences of using the pattern like increased flexibility. The Factory Method pattern for example defines an interface for creating an object but lets subclasses decide which object to create.

Uploaded by

Kesava 1642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT-II:

Patterns: Pattem Description, Organizing catalogs, role in solving design problems, Selection
and usage. Creational Patterns: Abstract factory. Builder, Factory method, Prololype. Singleton

1. Introduction to design patterns or whatisa design pattern(DP)2


Design patterms are repeatable / reusable solutions to commonly occuring problems in a certain
context in software design. There are four essential elements of a patterm,
Patterm name
Problem
Solution
Consequences
Need for design patterns
Designing reusable object-orientedsoftware (APl's) is hard.
Experienced designers Vs novice designers.
Patterns make object-oriented software lexible, elegant and reusable.
Solved a problem previously but don't remember when or how?
Use of design patterns
Make it easier to reuse successful designs and architectures.
Make it easy for the new developers to design software.
Allows choosing different design alternatives to make the software reusable
Helps in documenting and maintaining the software
Why should we use DP's?
These are already tested and proven solutions used by many experienced designes.
MVC Architecture
Model View Controller or MVC as it is popularly called, is a software design pattern for
developing web applications. A Model View Controller pattem is made up of the following three
parts.

Model -the lowest level of the pattern which is responsible for maintaining dala
View-this IS responsible tor displaying all or a portion ot the data to the user
Controller- Soflware Code that controls the interactions between the Model and View

MVC as it the
is popular isolates the application logic
concems. Here the Controller receives all
from user
interface layer and and
supports
separation of requests for the application then
works with the Model to prepare any data needed by the View. The View then uses the data
prepared by the Controller to generate a final presentable response. The MVC abstraction can be
graphically represented as follows.
Model
tate query State change
Encapsulates application state
-Responds tostate queries
nange
-
Exposes application functionality
otthcation
Nounes
views of changes

View Controller
Renders the models
Requestsupdates from models
View selection Definesapplication behavior
Maps user actions to model updates
Sends user gestures to controller Selects view for response
Allows controller to select view User gestures One for each functionality
******* ***

= Method Invocations

=Events

Describing design patterns


1. Pattern name and classification 8. Collaborations
2. Intent 9. Consequences
3. Also known as 10. Implementation
4. Motivation 11. Sample code
Applicability 12. Known uses
6. Structure 13. Related patterns
7. Participants

Organizing catalogs
Purpose
Creational Structural Behavioral
Scope Class Factory Method Adapter (class) Interpreter
Template Method
Object Abstract Factory Adapter (object) Chain of Responsibility
Builder Bridge Command
Prototype Composite lterator
Singleton Decorator Mediator
Fecade Memento
lyweight Obeerver
Proxy State

Strategy
Visitor
2ABSTRACT EACTORYE Provides an interface to create a family of related objects, without
explicitly specifying their concrete class.
Pattern name and classification
Intent: Provide an interface for creating families of related or dependent objects without
specifying their concrete classes.
Also known as: Kit
Motivation: Creating interface components for different look and feels.

whdgeiFectory CHent
CreateSerollBant) Window
CreateWindow)

PMWindow MotirWindow

MotirWidgetFectory PMWIdoetFectory - - -

CreateScrolBar() CreateScrollBar)
CreateWindow) CreateWindow() ScroBar -

PMScrollBer MotirscrollBar-

Applicability
We should use Abstract Factory design pattern when:
T h e system needs to be independent of the products it works with are created.
The system should be configured to work with multiple families of products.
A family of products is designed to be used together and this constraint is needed to be
enforced.
A library of products is to be provided and only their interfaces are to be revealed but not
their implementations.
Structure
Clet
AbatractFectory
CreateProductAO AbstrectProductA
CreateProdtctB0

ProductA2 ProductA

concretFectory Concretefectorya
CresteProductAO CreateProducA)
CroateProductB0 CreateProduciBK) ADetractProducte

Product81
Product82

Participants: The classes that participate in the Abstract Factory are: AbstractFactory.
ConcreteFactory, AbstractProduct, Product, Client
Collaborations
The ConcreteFactory class creates products objects having a particular implementation.
To create different product, the client should use a different concrete factory.
AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

Consequences
It isolates concrete classes.
t makes exchanging product families easy.
It creates consistency among products.

Implementation
abstract class AbstractProductA

public abstract void operationA1 (0;


public abstract void operationA2()

Sample code
public interface Window

public void setTitle(String text);


public void repaint();

Known uses
ET++ [WGM88] uses the Abstract Factory pattern to achieve portability across different window
systems (X Windows and SunView, for example).

Related patterns

AbstractFactory classes are often implemented with factory methods but they can also be
implemented using Prototype.
A concrete factory is often a Singleton.
4.FACTORYMETHOD
Pattern name and classification: Factory method and Creational
Intent: Define an interface for creating an object, but let subelasses decide which
Also known as: Virtual Constructor
Motivation: Frameworks use abstract classes to define and maintain relationships between
objects. A framework is often responsible for creating these objects as well.

Document CoAppcetion
Opend CreatoDocument)
Close0 NewDocumend) o Document doc CreateDocumenQ:
docs.Add(doc):
Seve) OpenDocument0 doc->Opon();
Revert)

MyDocument MyApplicato

Create Document) o -retum new MyDocument

Applicability: Use the Factory Method pattern when


A class can't anticipate the class of objects it must create.
A class wants its subclasses to specify the objects it creates.
Classes delegate responsibility to one of several helper subclasses, and you want to
localize the knowledge of which helper subclass is the delegate.
Structure
Creator

Produet FactoryMethod) product FectoryMethod)


AnOperation)

ConerelePoduct ConereteCreator

FactoryMethod0 retum new ConoreleProducat

Participants: Product, ConcreteProduct, Creator, ConcreteCreator


Collaborations: Creator relies on its subclasses to define the factory method so that it returns an
instance of the appropriate ConcreteProduct.
Consequences: Here are two additional consequences of the Factory Method pattern
Provides hooks for subclasses
Connects parallel class hierarchies
Implementation
abstract class AbstractProduct implements Cloneable

public static AbstractProduct thePrototype:


public static AbstractProduct makeProduct()
return (AbstractProduct) the Prototype.clone(;

catch(CloneNotSupportedException e)

return null;

Sample code
class MazeGame ( public:
Maze* CreateMaze():
I factory methods:
virtual Maze* MakeMaze() const
return new Maze: }
virtual Room* MakeRoom(int n) const
{return new Room(n); }
virtual Wall* Make Wall) const
return n ew Wall; }
virtual Door* MakeDoor(Room* rl, Room* r2) const
( return new Door(rl, 12);} }:
Known uses
Factory methods pervade toolkits and framework s.The preceding document example is a typical
use in MacApp and ET++. The manipulator example is from Unidraw.

Related patterns
Abstract Factory is often implemented with factory methods. as well.
.Factory methods are usually called within Template Methods
Prototypes don't require subclassing Creator.

You might also like