Beruflich Dokumente
Kultur Dokumente
Reusability Revisited
ceg860 (Prasad)
L156DP
etc)
An 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, E.g., MFC, JFC, etc.
Thread,
etc)
A design pattern describes a general recurring problem in different domains, a solution, when to apply the solution, and its consequences.
ceg860 (Prasad) L156DP 2
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)
ceg860 (Prasad) L156DP 3
Example is not another way to teach, it is the only way to teach. -- Albert Einstein
(Cf. Vince Lombardi Quote)
ceg860 (Prasad) L156DP 4
Client
ceg860 (Prasad)
L156DP
Translator/Adapter
Intermediary acts like a translator between the client and the server. E.g., Format/protocol conversions.
Client
Adapter
Server
ceg860 (Prasad)
L156DP
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
ceg860 (Prasad)
L156DP
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. Impl1 Impl2 Impl3
Client
Bridge
ceg860 (Prasad)
L156DP
etc.)
E.g., (pre-, in-) post-order traversals of tree. (Cf. Higher-order functions in Scheme.)
ceg860 (Prasad) L156DP 9
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.
ceg860 (Prasad) L156DP 10
ceg860 (Prasad)
L156DP
12
Example
Airline Reservation States User Identification Enquiry on flights (for certain time) Display flights Enquiry on seats Reserve seat Help, Exit, ...
13
In each state:
j display panel seeking user input / new request j read user input/query checking for consistency j process user request
j update database j transition to next state
ceg860 (Prasad)
L156DP
Each block encodes a state. Spaghetti code. One module : Unsuitable to maintain and reuse.
ceg860 (Prasad) L156DP 14
display
read_input
process
ceg860 (Prasad)
L156DP
15
void execute_session() { int state, next; state = initial; execute_state(state,next); state = transition(state,next); } while (! is_final(state)); } void execute_state(int state,int next) { T input; int next; display(state); read_input(state); process(state,next); }
ceg860 (Prasad) L156DP 16
do {
ceg860 (Prasad)
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(); }}
ceg860 (Prasad) L156DP 19
class Application { State[][] transition; State[] associated_state; Application(int n, int m) { transition = new State[n][m]; n m associated_state = new State[n]; n } 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) }}}
ceg860 (Prasad) L156DP 20
ceg860 (Prasad)
ceg860 (Prasad)
L156DP
22
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.
ceg860 (Prasad) L156DP 23
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.
ceg860 (Prasad) L156DP 24
A History List
isItem()
item()
count
...
isFirst() prev cursor
...
next isLast()
! isLast()
ceg860 (Prasad) L156DP 25
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
ceg860 (Prasad) L156DP 26
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.
L156DP 27
ceg860 (Prasad)