ceg860 (prasad)l156dp1 frameworks and design patterns reusability revisited

27
ceg860 (Prasad) L156DP 1 Frameworks and Design Patterns Reusability Revisited

Upload: jasper-skinner

Post on 24-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 1

Frameworks and Design Patterns

Reusability Revisited

Page 2: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 2

• A toolkittoolkit is a library of reusable classes designed to provide useful, general-purpose functionality.

• E.g., Java APIs (awt, util, io, net, etc)

• An application framework application framework is a specific set of classes that cooperate closely with each other and together embody a reusable design for a category of problems.

• E.g., Java APIs (Applet, Thread, etc)

• E.g., MFC, JFC, etc.

• A design patterndesign pattern describes a general recurring problem in different domains, a solution, when to apply the solution, and its consequences.

Page 3: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 3

A framework embodies a complete design of an application, while a pattern is an outline of a solution to a class of problems.

A framework dictates the architecture of an application and can be customized to get an application. (E.g., Java Applets)

When one uses a framework, one reuses the main body of the framework and writes the code it calls.

When one uses a toolkit, one writes the main body of the application that calls the code in the toolkit. (E.g., Java AWT)

Page 4: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 4

Why catalog/learn design patterns?

• Provides (an application-independent) vocabulary to communicate, document, and explore design alternatives.

• Captures the experience of an expert (especially the rationale behind a design and the trade-offs involved) and codifies it in a form that is potentially “reusable”.

• What, why, how, … Example is not another way to teach, it is the

only way to teach. -- Albert Einstein» (Cf. Vince Lombardi Quote)

Page 5: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 5

Example : The Intermediary Pattern

• A client interacts with an intermediary while the requested services are really carried out by the server/worker. Proxy

• Intermediary acts like a transmission agent.

• E.g., rpcrpc, rmi rmi implementations.

Client ProxyProxy Server

Page 6: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 6

Translator/Adapter• Intermediary acts like a translator between the client

and the server.

• E.g., Format/protocol conversions.

Client AdapterAdapter Server

Page 7: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 7

Facade• Intermediary acts like a focal point distributing

work to other agents.

• E.g., telnet, ftp, … web-browser.

• E.g., local/network files, devices, ... UNIX files

Client FacadeFacade

Server1

Server2

Server3

Page 8: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 8

Bridge/Abstract Factory/Handle• Intermediary defines the interface but not the

implementation.

• E.g., Motif/Mac/Windows look and feel.

• E.g., java.io.InputStream, java.io.OutputStream.

Client BridgeBridge

Impl1

Impl2

Impl3

Page 9: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 9

Example : The Traversal Pattern

• Task Visit every element in an aggregate structure in a well-defined order and perform an action on each element. Iterator

• Defines a mechanism for enumerating elements of an aggregate without exposing the representation (by supporting first(), next(), item(), isDone(), etc.)

• E.g., (pre-, in-) post-order traversals of tree.

(Cf. Higher-order functions in Scheme.)

Page 10: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 10

Model/View/Controller (Smalltalk)

• Pattern for graphical interactive system Model : Application Object View : Screen Presentation Controller : User interaction

MVC pattern decouples these three different categories of objects to increase flexibility and reuse. This facilitates support for multiple views of the same information and multiple ways of interaction.

Page 11: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 11

Java Support for MVC

• The multiple views and the model communicate through a subscribe/notify protocol.– Java 1.1 Delegation-based Event Model

– java.beans classes• PropertyChangeEvent, PropertyChangeListener, PropertyChangeSupport, etc.

• Controller specifies the way a view responds to user input.– Java AWT classes

– Buttons, Pulldown menus, Pop-up menus, Keyboard shortcuts, etc.

Page 12: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 12

Multi-Panel Interactive Systems

Functional Decomposition vs Object-Oriented Decomposition

Page 13: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 13

Multi-Panel System : Pattern• Session:

sequence of states

• In each state: display panel seeking

user input / new request read user input/query

checking for consistency process user request

update database transition to next state

Example– Airline Reservation

– States User Identification Enquiry on flights

(for certain time) Display flights Enquiry on seats Reserve seat Help, Exit, ...

Page 14: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 14

Encoding FSM using go toBenquire_flights :

display_panel(“Enquire_flights”); do { read_input(Input, okay); } while (!okay); Process(Input, Next); switch (Next) {... case 1: go to Bexit; case 2: go to Bhelp; case 3: go to Benquiry_on_seats; ...}

Each block encodes a state. “Spaghetti” code. One module : Unsuitable to maintain and reuse.

Page 15: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 15

Functional Top-Down Solution• Introduce transition function to localize gotos.• Turn each state into a module (routine).

execute_session

init transition execute_state final

display read_input process

Page 16: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 16

void execute_session() { int state, next; state = initial;

do { execute_state(state,next); state = transition(state,next); } while (! is_final(state));}

void execute_state(int state,int next) { TT input; int next;

display(state); read_input(state); process(state,next); }

Page 17: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 17

Limitations of functional decomposition• The various modules (routines: display, read_input, process) are tightly coupled via the input argument (state).

• Thus, each module (routine) has information about all possible variants (states).

• Remedy (Inversion) Instead of building modules around operations

and distributing data structures between the resulting routines, use the data types as a basis for modularization, attaching each routine to the corresponding data structure.

Page 18: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 18

Law of Inversion Illustrated (Functional vs Object-Oriented Decomposition)

Data 1Data 2

Data 3

Data 2

Data 1

Data 3

Proc 1Proc 2Proc 3

Proc 1

Proc 2

Proc 3

Data Structure

Routine

Date Type

Page 19: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 19

Object-Oriented Architecture

abstract class State { int next; T input; abstract void display(); abstract void read_input(); abstract void process(); void execute(next) { display(); read_input(); process();}}

Page 20: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 20

class Application { State[][] transition; State[] associated_state; Application(int nn, int mm) { transition = new State[nn][mm]; associated_state = new State[nn]; } void put_state(State s, int i){…} void put_transition(State src, State dst, int choice){…} int initial; void choose_initial(int i){…} void execute { State s; int stn = initial; while ( !stn ) { s = associated_state(stn); s.execute(); stn = transition(stn,s.next)}}}

Page 21: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 21

Building an Interactive ApplicationApplication airline_reservation = new new Application(num_states,

num_choices); ...airline_reservation.put_state(s,i) ...airline_reservation.put_transition(s,d,c) ... airline_reservation.choose_initial(i) airline_reservation.execute_session() ...

• Class Application is reusable in building other multi-panel interactive applications.

Page 22: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 22

Undo-Redo Facility (using history)

Inheritance, Dynamic Binding, Polymorphism

Page 23: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 23

Requirements

• Applicable to wide class of interactive applications.

• Incremental w.r.t. command additions.

• Use “reasonable” amount of storage.

• Support arbitrary-level of undoing.

Practical Issues: Part of the User Interface. Some commands undoable.

Page 24: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 24

abstract class Command { abstract void execute(); abstract void undo(); void redo() { execute(); }}

• Commands can be undone/redone.• undo and redo are operations that cannot

be undone/redone.• Each subclass of class Command adds

application specific details.

Page 25: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 25

A History List

... ...

isFirst() isLast()

countitem()

cursorprev next

isItem()

! isLast()

Page 26: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 26

List History = new List();Read_decode_request;if (request instanceOf Command) {

if (! History.isLast()) History.removeAllItemsToRight(); History.addItem(request);

request.execute();} else if (requested instanceOf Undo) if (History.isItem()) {

History.item.undo(); History.prev; } else ;// nothing to undo else if (requested instanceOf Redo) if (! History.isLast()) {

History.next;History.item.redo();

} else ; // nothing to redo

Page 27: Ceg860 (Prasad)L156DP1 Frameworks and Design Patterns Reusability Revisited

ceg860 (Prasad) L156DP 27

Reusability and Extendibility

• class Command can be subclassed to incorporate new commands.

• class Command can be modified to incorporate additional functionality such as adding help documentation, monitoring statistics, etc, for each command.

• This pattern can be used in an entirely new application, to support undo/redo capability.