software reuse means to “borrow” existing code. how can you reuse an existing class to make a...
TRANSCRIPT
Software reuse means to “borrow” existing code.
How can you reuse an existing class to make a new one?
Such reuse is really an adaptation -- using the adapter design pattern
Example:Create a SetOf3Doublets class by adapting a SimpleStringOfSet
class.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Domain set of StringInvariant (for every SetOfDoublets) this->size() == 3 and exists( e : this->includes(e) | e.length() == 2 )
Constructor public SetOfDoublets(String s1, String s2, String s3)
pre: s1.length()==2 and s2.length()==2 and s3.length==2 and s1!=s2 and s1!=s3 and s2!=s3post: this == set{ s1, s2, s3 }
Query Methods public boolean isIn(String s) post: result == this->includes(s) public boolean equals(SetOf3Doublets s) post: result == ( this = s ) “=“ denotes set equality
Update Methods public void replace( String oldS String newS)
pre: newS.length()==2 and isIn(oldS)(throws IllegalStateException)
post: this == this@pre->excluding(oldS)->including(newS)
SetOf3Doublets ADT Specifications
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
SetOf3Doublets
«constructor» + SetOf3Doublets(String s1, String s2, String s3)
«query» + boolean isIn(String s) + String equals(SetOf3Doublets s)
«update» + void replace(String oldS, String newS)
How would you reuse SimpleSetOfString to design SetOf3Doublets?
SimpleSetOfString
«constructor» + SimpleSetOfString()
«query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s)
«update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss)The Object of Data Abstraction
and Structure, David D. Riley© Addison Wesley pub.
Solution 1: Use Inheritance
SetOf3Doublets
«constructor» + SetOf3Doublets(String s1, String s2, String s3)
«query» + boolean isIn(String s) + String equals(SetOf3Doublets s)
«update» + void replace(String oldS, String newS)
SimpleSetOfString
«constructor» + SimpleSetOfString()
«query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s)
«update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss)
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
public class SetOf3Doublets extends SimpleSetOfString { public SetOf3Doublets(String s1, String s2, String s3) {
super();add(s1);add(s2);add(s3);
}
public void replace(String oldS, String newS) {remove(oldS);add(newS);
}
}
This is an example of the adapter design pattern, because SetOf3Doublets is adapting SimpleSetOfString via inheritance.
Methods that must be coded: SetOf3Doublets replace
Methods that are reused without change: isIn equals
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Potential Problem with the inheritance version of the adapter design pattern
SetOf3Doublets
«constructor» + SetOf3Doublets(String s1, String s2, String s3)
«query» + boolean isIn(String s) + String equals(SetOf3Doublets s)
«update» + void replace(String oldS, String newS)
SimpleSetOfString
«constructor» + SimpleSetOfString()
«query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s)
«update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss)
Unwanted members are inherited:sizeaddremoveunionintersection
inheriting size not harmful
use of add, remove, and union might violate invariant
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Solution 2: Proxy Design Pattern
SetOf3Doublets - SimpleSetOfString theSet«constructor» + SetOf3Doublets(String s1, String s2, String s3)
«query» + boolean isIn(String s) + String equals(SetOf3Doublets s)
«update» + void replace(String oldS, String newS)
SimpleSetOfString
«constructor» + SimpleSetOfString()
«query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s)
«update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss)
theSet is a “proxy” that allows SetOf3Doublets to access SimpleSetOfString members.
The proxy design pattern uses aggregation to accomplish the adapter design pattern.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
public class SetOf3Doublets { private SimpleSetOfString theSet;
public SetOf3Doublets(String s1, String s2, String s3) {theSet = new SimpleSetOfString();theSet.add(s1);theSet.add(s2);theSet.add(s3);
}
public void replace(String oldS, String newS) {theSet.remove(oldS);theSet.add(newS);
}
public boolean isIn(String s) {return theSet.isIn(s);
}
public boolean equals(SetOf3Doublets ss) {return theSet.equals(ss);
}
}
Proxy disadvantages: 1) must “reimplement” methods (see isIn & equals)
2) slightly messier notation “theSet.”
Proxy advantage: provides maximum control over class members