Download + String toString()

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Sometimes a design technique is used so often that it is
called a design pattern.
Design patterns are generalized approaches that apply to a wide
variety of situations.
This lecture examines the ______________ design pattern.
But first, an example...
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
NaturalNumber
«constructor»
+ NaturalNumber()
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
NaturalNumber ADT Specifications
Domain
Integers
Invariant (for every NaturalNumber)
1 <= this
Constructor
public NaturalNumber()
post: this == 1
Query Methods
public int toInt()
post: result == this
public String toString()
post: result == a String representation of this
Update Methods
public void set( int j)
post: this == j
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
public void incrementByOne()
post: this == this@pre + 1
NaturalNumber
# SomeNumericType thisValue
«constructor»
+ NaturalNumber()
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
RomanNumber
BigNumber
- byte[ ] digits
«constructor»
+ RomanNumber()
«query»
+ String toString()
«update»
+ void setFromRoman( String s )
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
«constructor»
+ BigNumber()
«query»
+ int toInt()
+ String toString()
+ int digitCount()
«update»
+ void set( int j )
+ void incrementByOne()
Code
(specs are omitted)
public class RomanNumber
extends NaturalNumber {
public class NaturalNumber {
public RomanNumber() {
super();
}
protected int thisValue;
public NaturalNumber() {
thisValue = 1;
}
public String toString() {
int partValue = thisValue;
String str = "";
while (partValue >= 1000) {
str = str + ”M";
partValue = partValue - 1000;
}
...
}
public int toInt() {
return thisValue;
}
public String toString() {
return ""+thisValue;
}
public void setFromRoman(String s) {
thisValue = 0;
for (int k=0; k!=s.length(); k++) {
if (s.charAt(k)=='M') {
thisValue = thisValue + 1000;
...
}
public void set( int j) {
thisValue = j;
}
public void incrementByOne() {
thisValue++;
}
}
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
}
/** class invariant
*
digits != null
*
and forAll (j : 0 <= j < digits.length | 0 <= digits[j] <= 9 ) */
public class BigNumber extends NaturalNumber {
private byte[] digits;
public BigNumber() {
super();
digits = new byte[1];
digits[0] = 1;
}
public int toInt() { ...
}
public String toString() { ...
}
public void set( int j) { ...
}
public void incrementByOne() { ...
}
public int digitCount() { ...
}
}
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
Consider how the following method uses its NaturalNumber parameter.
private void manipulateNumber( NaturalNumber num ) {
num.set(4879);
for (int j=1; j<=100; j++) {
num.incrementByOne();
}
System.out.println(num);
}
Below is an example of client code using the manipulateNumber method.
NaturalNumber myNum;
myNum = new NaturalNumber();
manipulateNumber(myNum);
myNum = new RomanNumber();
manipulateNumber(myNum);
myNum = new BigNumber();
manipulateNumber(myNum);
RomanNumber romanNum = new RomanNumber();
romanNum.setFromRoman("MMII");
System.out.println(romanNum);
System.out.println(romanNum.toInt());
BigNumber bigNum = new BigNumber();
bigNum.set(2147483647); // 2,147,483,647 is maximum int value
bigNum.incrementByOne();
System.out.println(bigNum);
System.out.println(bigNum.toInt());
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
The members of a superclass form a template that is inherited by
all subclasses.
Sometimes the primary goal of a class is to serve as a template for
subclasses
(i.e., the template defines what subclasses must implement)
The Template Pattern
TemplateClass
The true template class forces its
subclasses to implement methods.
• abstract classes (Java)
• interfaces (Java)
SomeSubclass
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
NaturalNumberTemplate {abstract}
# SomeNumericType thisValue
«constructor»
+ NaturalNumberTemplate()
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
NaturalNumber
«constructor»
+ NaturalNumber()
«query»
+ int toInt()
+ String toString()
«update»
+ void incrementByOne()
BigNumber
- byte[ ] digits
«constructor»
+ BigNumber()
«query»
+ int toInt()
+ String toString()
+ int digitCount()
«update»
+ void set( int j )
+ void incrementByOne()
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
RomanNumber
«constructor»
+ RomanNumber()
«query»
+ String toString()
«update»
+ void setFromRoman( String s )
public abstract class NaturalNumberTemplate {
protected int thisValue;
public NaturalNumberTemplate() {
thisValue = 1;
}
public abstract int toInt();
public abstract String toString();
public void set( int j) {
thisValue = j;
}
public class NaturalNumber
extends NaturalNumberTemplate {
public NaturalNumber() {
super();
}
public abstract void incrementByOne();
}
public int toInt() {
return thisValue;
}
public String toString() {
return ""+thisValue;
}
public void incrementByOne() {
thisValue++;
}
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
}
«interface»
NaturalNumberInterface
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
BigNumber
- byte[ ] digits
«constructor»
+ BigNumber()
«query»
+ int toInt()
+ String toString()
+ int digitCount()
«update»
+ void set( int j )
+ void incrementByOne()
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
NaturalNumber
# SomeNumericType thisValue
«constructor»
+ NaturalNumber()
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
RomanNumber
«constructor»
+ RomanNumber()
«query»
+ String toString()
«update»
+ void setFromRoman( String s )
public interface NaturalNumberInterface {
public int toInt();
public String toString();
public void set( int j);
public void incrementByOne();
}
public class NaturalNumber
implements NaturalNumberInterface {
protected int thisValue;
public NaturalNumber() {
thisValue = 1;
}
public int toInt() {
return thisValue;
}
public String toString() {
return ""+thisValue;
}
public void set(int j) {
thisValue = j;
}
public void incrementByOne() {
thisValue++;
}
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
}
Abstract Class
Inheritance syntax
extends
Interface
implements
Mark methods as “abstract”?
Non-deferred methods possible?
Subclass method code required?
Instance variables possible?
Can be inherited multiply?
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
Related documents