Title: ObjectOriented Composition Untangled
1Object-Oriented Composition Untangled
2A Little Story About Recent Moves ...
It was really frustrating...
It was really frustrating...
- My bank account changed
- formerly Sparkasse Bonn, now Sparkasse Munich
- had to update numerous bank details
- My email address changed
- formerly ostermann_at_cs.uni-bonn.de, now
Klaus.Ostermann_at_mchp.siemens.de - had to inform many friends and colleagues
- My ACM email address did not change!
- still kos_at_acm.org
- Mail is automatically forwarded to my actual
address
3Transparent Redirection is Ubiquitous
- e-mail
- phone
- symbolic links in Unix file systems
- URL redirection
- many mechanisms in distributed systems
- However
- no explicit notion of transparent redirection in
OOPL
4Account Example
- Modifying a persons account invalidates standing
orders - No satisfactory OO solution available (see paper)
5Core of the ProposalRedirection with Compound
References
Account
jack.account ubsAccount anAccount
jack.account jacksAccount jacklt-account //
xxx jack.account dbAccount
Account
Person
6Core of the Proposal Redirection with Compound
References
Account
jack.account ubsAccount anAccount
jack.account jacksAccount jacklt-account //
xxx jack.account dbAccount
Account
Person
7Account Example Reconsidered
class Person Account account Account
getAccount() return account Account
getPersonsAccount() return thislt-account
void setAccount(Account newAccount)
account newAccount
8Observation Inheritance/Delegation Imply
Redirection
- Inheritance
- Method calls automatically redirected to
superclass - Delegation
- Even closer to CR Parent objects can be
exchanged, similar to accounts with CRs - However, delegation is more than plain CRs
- CRs seem to be in the middle of the path
between object composition and inheritance/delegat
ion
9Composition Properties
Modules M and B, Composition M(B)
- Overriding In M(B) definitions in M override
definitions in B with the same name - Transparent Redirection Within B this refers to
M(B) - Acquisition Bs features can be used in M(B) as
if they were M(B)s features. - Subtyping M(B) can be used wherever B is
expected. - Polymorphism The ability to (dynamically or
statically) apply M to subtypes of B.
10Mapping of Composition Properties
11The TextJustifier Example
TextJustifier Example
12TextJustifier Example
compose
13TextJustifier ExampleComposition by Inheritance
- TextJustifier is subtype of TreeIterator (no
subtyping) - Methods of TreeIterator pollute the TextJustifier
interface (no acquisition) - Design gets complex if different traversal
strategies, should be selected at runtime
(polymorphism)
14TextJustifier ExampleDesign for Dynamic
Composition
- Different from previous design
- Complex !
- See paper for more details
15Field Methods and Overriding
- class TextJustifier
- private Iterator it
- public void justify() ... it.doAll() ...
- // begin of implicitly available field
methods - private void it.doAll() field.doAll()
- ...
- private void it.action(Item x)
field.action(x) - private boolean it.test(Item x)
- return field.test(x)
-
- // end of implicitly available field methods
Alias for it field
16Field Methods and Overriding
class TextJustifier private redirect
TreeIterator it private void it.action() ...
private boolean it.test(Item x) ...
public void justify() ... it.doAll(...)
class TreeIterator public void doAll() ...
this.action() ... ...
17The Stream Example
Stream Example
18The Stream Example
- BufferedOS requires redirection
- LoggingOS does not work with redirection
19Acquisition and Subtyping
BOS acquires all methods of OutputStream by
removing the field method prefixes
- class OutputStream
- ...
-
- class BufferedOutputStream subtypeof OutputStream
- acquire redirect OutputStream stream
- ...
-
- class LoggingOutputStream subtypeof OutputStream
- acquire OutputStream stream
- ...
-
BOS is subtype of OutputStream
20Type Safety
- Threatened by subtle combinations of CRs,
subtyping, and dynamic binding. - Solution If critical CRs are created, the
assignment historyis stored. Calls are
dispatched to history objects if necessary. - Invariant CR refers to the current value of the
corresponding variable is violated.
... a.b new B1() ... alt-b.foo() ... a.b new
B2()
21Problem Summary
- Frequently needed Composition semantics not
provided by any of the standard mechanisms. - Consequence Simulation by architectures that
- are complex,
- are fragile with respect to requirement changes,
- are difficult to change without invalidating
existing clients, - restrain readability and maintainability of the
code.
22Independent Applicability of Properties
23Conclusions
- Composition mechanisms are untangled by
rendering each property independently
applicable. - Complicated simulation architectures no longer
nedded. - Instead of expensive refactorings it suffices to
add/delete property modifiers. - Overriding and redirection of multiple fields
without multiple inheritance conflicts. - Model complicated by mechanisms to retain static
type safety