Design Patterns
Design Patterns
PAGENO:
DESIGN PATTERNS
1. Aim:- Using UML design Abstract factory Design pattern.
Pattern Name:- ABSTRACT FACTORY
Intent:- Provide an interface for creating families of related or dependent
objects without specifying their concrete classes.
Also Known As:- Kit
Motivation:- Consider a user interface toolkit that supports multiple lookand-feel standards , such as Motif and Presentation Manager.Different lookand-feels define different appearances for user interface widgets like scroll
bars,windows and buttons.Instantiating look-and-feel-specific classes of
widgets throughout the application makes it hard to change the look and feel
later.
We can solve this problem b defining an abstract Widget Factory class
that declares an interface for creating each such basic kind of widget.
Theres also an abstract class for each kind of widget,and concrete sub
classes implement widgets for specific look-and-feel standards.Widget
factorys interface has an operation that returns a new widget object or each
abstract widget class.Clients call these operations to obtain widget instances,
but clients arent aware of the concrete classes theyre using.Thus clients
stay independent of the prevailing look and feel.
UML&DP
ROLLNO:11471A0577
PAGENO:
Widget Factory
create scrollbar
create window
window
pmwindow
motif window
pm widget factory
create scroll bar
create window
scroll bar
pm scroll bar
UML&DP
ROLLNO:11471A0577
PAGENO:
AbstrractFactory
createProductA()
createProductB()
clie
AbstractProductA
productA2
ConcreteFactory1
createProductA()
createProductB()
ConcreteFactory2
createProductA()
createProductB()
productA1
AbstractProductB
productB2
productB1
UML&DP
ROLLNO:11471A0577
PAGENO:
UML&DP
ROLLNO:11471A0577
PAGENO:
format should be able to concert RTF to many text formats. The reader
might convert RTF documents into plain ASCII text or into a text widget that
can be edited interactively. The problem, however , is that the number of
possible conversions is open-ended. So it should be easy to add a new
conversion without modifying the reader.
A solution is to configure the RTF reader class with a TextConverter
object that converts RTF to another textual reprsentation.
As the RTFReader recognizes an RTF token , it issues a request to the
TextConverter to convert the token. TextConverter objects are responsible
both for performing the data conversion and for representing the token in a
particular format.
Texconverter
convertcharacter(char)
converter Fontchange(font)
convert paragraph
GetTexText
RTFReader
parseRTF()
TextWidgetconverter
convertcharacter
convertfontchange(font)
convert paragraph
GetTextwidget
TextConverter
convertCharacter
convertFontChange(font)
convertParagraph
GetTextext
ASCIIConverter
convertCharacter
getASCIIText
ASCII Text
TexText
Textwidget
UML&DP
ROLLNO:11471A0577
PAGENO:
object constructed.
Structure:Director
construct()
Buiider
forallobjectsinstructure{
builder->BuilderPart()
}
Buiider
BuildPart()
ConcreteBuilder
BuildPart()
GetResult()
Product
Participants: Builder
ConcreteBuilder
Director
Product
Collaborations: The clients creates the Director object and configures it with the
desired Builder object.
Director notifies the builder whenever a part of the product should be
built.
Builder handles requests from the director and adds parts to the
product.
The client retrieves the product fro the builder.
Consequences:Here are key consequences of the Builder pattern,
It lets you vary a products internal representation.
It isolates cod construction and representation.
It gives you finer control over the construction process.
Known Uses: The parser class in the compiler subsystem is a Director that takes
program Node Builder object as an argument.
Class Builder is a builder that Classes use to create subclasses for
themselves.
ByteCodeStream is a builder that creates a compiled method as a
byte array.
Related Patterns:-
UML&DP
ROLLNO:11471A0577
PAGENO:
Abstract Factory
Composite
UML&DP
ROLLNO:11471A0577
PAGENO:
compiler
compile()
Stream
NewClass7
NewClass8
NewClass9
NewClass10
NewClass11
NewClass12
BytecodeStream
CodeGenerator
NewClass13
StateMachineCodeGenerator
RISCCodeGenerator
UML&DP
NewClass14
New
ROLLNO:11471A0577
PAGENO:
Participants: Facade
Subsystem classes
Collaborations: Clients communicative with the subsystem by spending request to
Facade, which forwards them to the appropriate subsystem objects.
Clients that use the facade dont have to access its subsystem objects
directly.
Consequences:the facade pattern offers the following benefits,
It shields clients from subsystem components, there by reducing the
number of objects deal with and making the subsystem easier to use.
It promotes weak coupling between the subsystems and its clients.
It doesnt prevent applications from using subsystem classes if they
need to.
Known Uses: The compiler was inspired by the ObjectWorks\SmallTalk compiler
system.
In the ET++ application framework , an application can have built in
UML&DP
ROLLNO:11471A0577
PAGENO:
10
window
DrawText()
DrawRect()
iconWindow
DrawBorder()
DrawRect()
DrawText()
TransientWindow
DrawCloseBox()
imp->DevDrawLine()
imp->DevDrawLine()
imp->DevDrawLine()
imp->DevDrawLine()
XWindowimp
DevDrawText()
DevDrawLine()
PMWin
DevDr
DevDr
DrawRect()
XDrawLine()
UML&DP
XDrawString
)
ROLLNO:11471A0577
PAGENO:
11
imp
Abstraction
operation()
implementor
OperationImp()
imp->Operati
onimp()
RefinedAbstraction
ConcreteImplementorA
operationImp()
Participants: Abstraction
RefinedAbstraction
Implementor
ConcreteImplementor
Collaborations: Abstraction forwards client requests to its implementor object.
Consequences:The bridge pattern has the following consequences:
Decoupling interface and implementation.
Improved extensibility.
UML&DP
ConcreteImplemento
operationImp()
ROLLNO:11471A0577
PAGENO:
12
UML&DP
ROLLNO:11471A0577
PAGENO:
13
UML&DP
ROLLNO:11471A0577
PAGENO:
14
visualcom
ponent
draw()
TextView
draw()
Decorator
draw()
ScrollDecorator
draw()
ScrollTo()
component-->draw()
BorderDecorator
draw()
drawBorder()
UML&DP
Decorato::Draw
();
DrawBorder();
ROLLNO:11471A0577
PAGENO:
15
component
operation()
concreteComponent
operation()
component->operati
on()
Decorator
operation()
concreteDecoratorA
operation()
addedState()
concreteDecoratorB
operation()
addedBehaviour()
Decorator::operation(
)
AddedBehaviour();
Participants: Component
ConcreteComponent
Decorator
ConcreteDecorator
Collaborations: Decorator forwards request to its Component object. It may optionally
perform additional operations before and after forwarding the request.
Consequences:The Decorator pattern has at lest two key benefits and two liabilities:
More flexibility that static inheritance.
Avoids feature-laden classes high up in the hierarchy.
A decorator and its component arent identical.
Lots of little objects.
Known Uses: Many object oriented user interface tool kits use decorators to add
graphical embellishments to widgets.
The decorator pattern gives us an elegant way to add responsibilities to
streams.
Related Patterns: Adapter
Composite
Strategy
Narasaraopeta Engineering College
UML&DP
ROLLNO:11471A0577
PAGENO:
16
Application
handler->Han
dlerHelp()
Widget
Button
HandleHelp()
ShowHelp()
Dialog
UML&DP
ROLLNO:11471A0577
PAGENO:
17
Handler
HandleRequest()
client
ConcreteHandler1
HamdleRequest()
ConcreteHandler2
HandleRequest()
Participants: Handler
ConcreteHandler
Client
Collaborations: When a client issues a request, the request propagates along the chain
until a ConcreteHandler object takes responsibility for handling it.
Consequences:Chain of Responsibility has the following benefits and liabilities:
Reduced coupling
Added flexibility in assigning responsibilities to objects.
Receipt isnt guaranteed.
Known Uses: Several class libraries use the chain of responsibility pattern to handle
user events.
The uni-draw framework for graphical editors defines command objects
that encapsulate request to component and component-view objects.
ET++ uses chain of responsibility to handle graphical update.
Related Patterns: Composite
UML&DP