How To Design Frameworks
How To Design Frameworks
net/publication/243763805
CITATIONS READS
24 8,653
1 author:
Ralph Johnson
University of Illinois, Urbana-Champaign
215 PUBLICATIONS 42,584 CITATIONS
SEE PROFILE
All content following this page was uploaded by Ralph Johnson on 22 May 2015.
Ralph E. Johnson
University of Illinois at Urbana-Champaign
[email protected]
(217) 244-0093
1
Frameworks
Frameworks
Not just the classes, but the way instances of the classes collaborate.
• shared invariants that objects must maintain, and how they maintain
them
• framework imposes a collaborative model that you must adapt to.
Framework
• is result of domain analysis
• describes how to decompose problem
• represented by a program
Reuse of
• analysis
• design
• code
Generalization proceeds by
• finding things that are given different names but are really the same,
• parameterizing to eliminate differences,
• breaking large things into small things so that similar components
can be found, and
• categorizing things that are similar.
Example-driven Design
Generalization
Examples Framework
System
size
Time
To generalize faster:
• get different points of view
• explain/defend current design
7
Designing Abstractions
Good designers know many design patterns, techniques that they know tend
to lead to good designs.
• implies that experience is needed
10
Pick two similar applications that need to be developed and that are
obviously in your application domain.
Make sure your staff has some people who developed earlier applications in
the same domain.
11
... 12
Conflicting goals
• get system out on time
• make it reusable
13
Ward Cunningham
• portfolio -- collection of instruments
• instrument -- stock, bond, cash fund
• has value, which can change daily
• transactions affect it, and it causes transactions
• transactions
• have a date
• can affect several instruments at once
14
Solution:
• cache - keeps value at a particular date
• calculator - given a cache and a set of transactions, computes another
cache
- calculator can compute interest, taxes, etc. as a side effect of
processing transactions.
15
Another Example
Accounting at a store
• journal -- collection of accounts
• account -- inventory, accounts payable (vender)
• has value, which can change daily
• transactions affect it, and it causes transactions
• transactions
• have a date
• can affect several accounts at once
16
Obvious Similarities
17
An Inventory Account
An invoice for a purchase increases inventory and increases amount owed to vender.
Inventory #384 on hand sold this month this year purchased this month
30 100 1000 30
sales #6 (4) 26 104 1004 30
sales #8 (1) 25 105 1005 30
sales #13 (2) 23 107 1007 30
invoice #5 (50) 73 107 1007 80
sales #24 (1) 72 108 1008 80
18
Attributes
19
Transactions
Transactions are just records: set of fields, some of which are multivalued.
Example: VendorInvoice
Vendor,
Date,
set of (Inventory type, Quantity, Price)
Payment due date, discount, etc.
Purchase Order number,
Total (can be computed from other fields)
20
Shrinkage
Inventory (IJ) Sales (SJ)
Journal Journal
Invoice
22
23
Examples
24
Finding Examples
25
Once Accounts can handle several small examples well, scale up.
26
27
Scheduling Development
28
Design Patterns
Superclass/subclass
Part/whole (component/container)
Builder/product
Delegator/delegatee
Double-dispatching
29
Inheritance
• easy to override inherited methods
• static (easier to reason about)
Composition
• less programming
• change behavior at run-time
30
Multiple inheritance!
31
Idea:
Suppose C is a subclass of S1, S2, ..., Sn. Give C components C1, C2, ...,
Cn, where Ci is a subclass of Si for 0<i<n. Each Ci has a reference to
C, which has a reference to all the Ci, so each component can access
any other.
For Accounts:
Instead of making Invoice be a subclass of InventoryTransaction and
PayablesTransaction, make it be a composite transaction with
subtransactions that are InventoryTransactions and
PayableTransactions.
32
Composites
33
CompositeAccount
The transaction "Bob buys 100 shares of IBM stock" is posted to Bob's
Portfolio, which in turn posts a transaction to an IBM stock account
that is part of the portfolio.
The value of Bob's portfolio is the sum of the values of the accounts in it.
34
For Accounts:
Post transaction to journal -> post transaction to account
Result: CompositeAccount is an account made up of other
accounts.
35
Recursion Introduction
36
Strategy Object
37
Attributes as a Strategy
38
Options:
1) Factor commonality into functions in Account, and have each function
representing an attribute call them. Attributes are then simple and
stylized, and so easy to implement.
39
If some feature varies from class to class, but some classes have the
feature in common, then represent the feature as a component. Make
a class hierarchy to represent the variations in the feature.
40
Example:
Attribute -> MTDAttribute, YTDAttribute, TotalAttribute
41
Advantages
• instead of modifying case statements, add a new subclass
• easier to parameterize
• can use inheritance to make new options
Disadvantages
• program is spread out,
+ harder to understand
+ harder to replace algorithm
• state of object can change, but class can not
This means that changing an Attribute from MTD to
YTD requires replacing it.
42
Attribute
YTDAttribute
MTDAttribute
TotalAttribute
When objects do not come directly from the problem domain, it is often
hard to design good abstract classes from first principles.
43
Usually code in subclasses is almost, but not quite, the same. Must
abstract out differences, and move the rest to superclass.
44
Decompose Functions
• Turn each code sequence into a new function in the same class as the
function containing the code sequence.
45
Example
x->foo->fee(a, b) => x->fee(a, b)
Define fee function in class of x that performs fee() on
component.
46
Idea:
1) make a class hierarchy that represents nodes in abstract syntax tree
(/, -, +, attribute)
2) define a function value(Date) for each class
An AttributeNode knows an Account and Attribute on that Account,
and value(Date) will read the Attribute on that Account for that
date.
value(Date aDate) for a PlusNode will return the sum of the values
for aDate for its two descendents.
3) define +, -, /, etc functions in common superclass to return PlusNode,
DifferenceNode, RatioNode, etc.
47
Factory
What should a composite account do if it can't find a component with a
given name?
• report an error -- the transaction is invalid
Example: invoice has an illegal inventory number
• create a component account
Example: buying some stock when you've never owned it before
Journal Account
Account (with subaccounts) SimpleAccount
CompositeAccount
(parameterized by its
Portfolio component Accounts and the
Account names it gives them)
49
Summary of Refactoring
Lower level
• moving functions and variables to components
• moving functions and variables to superclass
• breaking functions into smaller functions
• renaming functions, classes, and variables
Higher level
• finding abstract superclasses
• breaking class into components or subclasses
• reuse by inheritance to reuse by components
• eliminating case analysis
50
Summary of Patterns
There are a set of higher-level design patterns that make systems more reusable
and that are goals of refactorings.
Grand Summary
52