Sei sulla pagina 1di 12

18 - Patterns and Frameworks

Patterns and Frameworks

» Purpose of this lecture


» to discuss how reuse can be increased through
the use of frameworks and design patterns
» to introduce MVC (Model - View - Controller)
programming as a powerful metaphor for
Smalltalk application design

OOP - Carine Lucas 18- 1

Patterns and Frameworks:


Working together

» A class is seldom the solution to the whole


problem
» Often classes need to work together
» Frameworks and Design Patterns are two ways of
describing the solution of a problem as the
organisation of many classes working together

OOP - Carine Lucas 18- 2

1
18 - Patterns and Frameworks

Application Framework

» A framework is a series of classes that describe a


“skeleton” application
» The framework provides structure, but no content
» Normally, the content is provided by subclassing
elements from the framework, overriding key
methods
» By using inheritance, user only needs to specify
what is new about the application

OOP - Carine Lucas 18- 3

Two views of the Same Thing


An important application of overriding is the
development of software frameworks such as
GUI’s
class application {
public:
virtual void mouseDown(int,int);
};

class drawingApplication: public application {


public:
virtual void mouseDown(int,int);
};
OOP - Carine Lucas 18- 4

2
18 - Patterns and Frameworks

An Upside Down Library


» Frameworks are sometimes described as
upside-down libraries
» Role of user code and library code is
reversed, inversion of control
Conventional library Framework
framework
user supplied main program Framework library
code

library user supplied


code code

OOP - Carine Lucas 18- 5

Example: The Java API

» In the Java API (Application Programming Interface) the


user subclasses from Applet, then defines the following
methods:

init() Invoked when the applet is initialised


start() Invoked when the application is started
paint(Graphics) Invoked when window is to be redrawn
mouseDown(Event, int, int)
Invoked when the mouse is pressed
keyDown(Event, int) Invoked when key is pressed
stop() Invoked when the window is removed

OOP - Carine Lucas 18- 6

3
18 - Patterns and Frameworks

Event-Driven Execution

program main
begin
initialise application;
while not finished do
begin
if user has done something interesting
respond to it
end;
clean up application;
end

OOP - Carine Lucas 18- 7

Three categories of methods


» Base methods: provide essential functionality that is
useful to the customisation process, but will probably not
be overridden (e.g. drawing lines).
» Template methods (algorithm methods): describe an
abstract algorithm for which specific details are left to
other methods (e.g. the event loop).
» Abstract methods: do the actual work of the application,
by overriding them, actions specific to a new application
are provided (e.g. repaint the window).
» See Java API, Smalltalk MVC.
OOP - Carine Lucas 18- 8

4
18 - Patterns and Frameworks

Application frameworks
» Allows the programmer to carry whole algorithms
from one project to the next, is designed to help
solve a narrow range of problems
» High-level details are shared, while traditional
libraries only permit sharing low-level functions
» Frees the designer from much work, but narrows
the range of variation by laying out the overall
design (e.g. single window applications).
» Inheritance is the primary technique to specialise
framework
OOP - Carine Lucas 18- 9

Design Patterns

» Design patterns are more amorphous. A design


pattern captures the elements of a solution to a
problem, but in a language independent fashion
» An example pattern might be “standing in place
of”, which may be manifest in many ways:
Facade, Adapter, Proxy, Decorator, Bridge
Patterns.
Worker /
Client Intermediary Implementation/
Server
OOP - Carine Lucas 18- 10

5
18 - Patterns and Frameworks

Background

» Designing reusable software is difficult


» Finding good objects and abstractions
» Flexibility, Modularity, Elegance
» Takes time to emerge, trial and error
» Successful designs exist
» How to describe recurring structures
» Deja-Vu feeling, don’t reinvent the wheel, don’t
reinvent the flat tire.

OOP - Carine Lucas 18- 11

A Design Pattern

» Describes a recurring design structure


» Used twice rule
» Names, abstracts from concrete designs
» Identifies classes, collaborations, responsibilities
» Applicability, trade-offs, consequences,
language issues
» Is discovered, not invented

OOP - Carine Lucas 18- 12

6
18 - Patterns and Frameworks

Design Patterns vs Frameworks

» Design patterns are not frameworks


» Frameworks codify designs for solving a family of
problems in a specific domain
» Frameworks are instantiated by subclassing and
composition of objects
» Frameworks can contain several instances of
multiple design patterns

OOP - Carine Lucas 18- 13

Why Use Design Patterns ?

» To reuse proven design techniques


» Gain design confidence
» To define common vocabulary amongst designers
» Within and across teams
» To structure the design
» To increase reuse of design
» To provide reuse documentation

OOP - Carine Lucas 18- 14

7
18 - Patterns and Frameworks

Practical Experience

» Design Patterns are based on practical solutions


found in main-stream applications implemented in
Smalltalk and C++
» Windowing Systems
» CAD
» Banking
» Persistent Objects
» Distributed Systems
» ...

OOP - Carine Lucas 18- 15

Describing Design Patterns

» Pattern name and » Participants


classification » Collaborations
» Intent » Consequences
» Also Known As » Implementation
» Motivation » Sample Code
» Applicability » Known Uses
» Structure » Related Patterns

OOP - Carine Lucas 18- 16

8
18 - Patterns and Frameworks

Catalogue of Design Patterns

» Purpose
» Creational
» Structural
» Behavioural
» Scope
» Class
» Object

OOP - Carine Lucas 18- 17

Bridge

» Object Structural
» Intent: Decouple an abstraction from its
implementation so that the two can vary
independently
» Motivation: Window Implementations

OOP - Carine Lucas 18- 18

9
18 - Patterns and Frameworks

Bridge: Motivation
Imp
Window WindowImp

DrawText() DrawText()
DrawRect DrawRect

IconWindow TransientWindow XWindowImp Window95Imp

DrawBorder() DrawCloseBox DrawText() DrawText()


DrawRect DrawRect

OOP - Carine Lucas 18- 19

Bridge: Applicability

» Use the Bridge Pattern to


» Avoid a permanent binding between an
abstraction and its implementation
– Separation of Concerns
» Both the abstractions and their implementations
should be extensible by subclassing
» To avoid proliferation of classes
» To share an implementation among multiple
objects

OOP - Carine Lucas 18- 20

10
18 - Patterns and Frameworks

Bridge: Participants

» Abstraction (e.g. Window)


» RefinedAbstraction (e.g. IconWindow)
» Implementor (e.g. WindowImp)
» ConcreteImplementor (e.g. XWindowImp)

OOP - Carine Lucas 18- 21

Bridge: Structure

Client

Abstraction Implementor
imp
Operation() OperationImp()

ConcreteImpA ConcreteImpB

RefinedAbstraction OperationImp() OperationImp()

OOP - Carine Lucas 18- 22

11
18 - Patterns and Frameworks

Bridge: Consequences

» Decoupling interface and implementation


» Improved extensibility

OOP - Carine Lucas 18- 23

Conclusion

» Both frameworks and design patterns are


ways of describing and documenting
solutions to common problems
» Frameworks are more “shrink-wrapped”,
ready for immediate use
» Patterns are more abstract - many patterns
are involved in the solution of one problem

OOP - Carine Lucas 18- 24

12

Potrebbero piacerti anche