Title: Code Reuse in OOP
1Code Reuse in OOP
- Three categories of code reuse
- Inherit an existing class
- SSLServerSocket extends ServerSocket
- Instantiate an existing class
- new Thread(new YourOwnRunnable() )
- Combine instances at runtime
- Decorator design pattern
2When is Instance Combination Useful?
- class VisualString private String string
public void draw() // draw a string on
the screen - You want to draw a box around the string.
- class BoxedString extends VisualString
public void draw() // redefine how to
draw a string - You want to draw a string in color.
- Class ColoredString extends VisualString
public void draw() // redefine how to
draw a string - What if you want to draw a box around a colored
string? - What if you want to draw a box around an icon?
3- class Customer public double getAnnualFee()
- class DomesticCustomer extends Customer
public double getAnnualFee() - class InternationalCustomer extends Customer
public double getAnnualFee() - What if you want to set up
- domestic/international cooperate, individual and
government customers - VIP discount rate employee discount rate?
- What if you want to change the status of a
customer from non-VIP to VIP or domestic to
international?
4Decorator Design Pattern
Customer
Customer
Corporate
Domestic
individual
Intl
VIP
5Sample Code Structure of Classes
entity
ltltinterfacegtgt Entity
getAnnualFee()
Customer
EntityDecorator abstract
getAnnualFee()
getAnnualFee()
DomesticDecorator
IntlDecorator
IndividualDecorator
CorporateDecorator
getAnnualFee()
getAnnualFee()
VIPDecorator
GovDecorator
getAnnualFee()
getAnnualFee()
6Sample Code Instance Combination
- Customer customer new Customer()customer.g
etAnnualFee() --gt 100.0 - Customer customer new DomesticDecorator(
new CorporateDecorator( new
Customer() ) )customer.getAnnualFee() --gt
1000.0 - The kernel 2 skins (decorators)
Customer
Customer
Corporate
Domestic
getAnnualFee()
main()
100
1000
10010
1000
7- new InternationalDecorator( new
GovernmentDecorator( new Customer() ) ) - new VIPDecorator( new IndividualDecorator(
new Customer() ) )
Customer
Gov
Intl
getAnnualFee()
main()
Customer
100
Individual
VIP
300
1003
360
3001.2
getAnnualFee()
main()
100
100
0
1000
8Java I/O API
- InputStream and OutputStream
- Abstract classes that represent input and output
byte streams. - Reader and Writer
- Abstract classes to read and write character
streams - Many filters are defined to tailor specialized
input/output streams and readers/writers.
Characters
Characters
Objects Audio File Chars, etc.
Objects Audio File Chars, etc.
Filters
Filters
Char steam
Reader
Writer
Filters
Filters
Byte steam
InputStream
OutputStream
9InputStream
- InputStream objects can be decorated with an
arbitrary number of filters. - InputStream classes
- AudioInputStream
- FileInputStream
- ObjectInputStream
- PipedInputStream
- etc.
- SocketInputStream (hidden)
in
InputStream abstract
read()close()
FilterInputStream
InputStream classes
read()close()
filters
Data from socket
Files
Audio
Objects
Filters
FileInputStream
InputStream
AudioInputStream
ObjectInputStream
SocketInputStream
10Initialization of InputStream
- InputStream is new FileInputStream(foo.txt)
InputStream is new FileInputStream( System.in)
is.read() - Socket socket new Socket()InputStream is
socket.getInputStream()is.read() - System.out.println(socket.getInputStream().toStrin
g() ) - java.net.SocketInputStream_at_10d81b
Data from socket
Files
Filters
FileInputStream
InputStream
SocketInputStream
11- Some InputStream objects can be connected with
each other. - InputStream is new AudioInputStream( new
FileInputStream( audiofile) ) - InputStream is new AudioInputStream( socket.get
InputSteam() ) - InputStream is new ObjectInputStream( new
FileInputStream( objectfile) )
Audio
Audio
Object
AudioInputStream
AudioInputStream
ObjectInputStream
FileInputStream
SocketInputStream
FileInputStream
12Filters of InputStream
- Filters
- BufferedInputStream
- CheckedInputStream (java.util.zip)
- ZipInputStream (java.util.zip)
- GZIPInputStream (java.util.zip)
- JarInputStream (java.util.zip)
- CipherInputStream (javax.crypto)
- DigestInputStream (java.security)
- etc.
- A single InputStream object can be decorated with
multiple filters.
in
InputStream abstract
read()close()
FilterInputStream
InputStream classes
read()close()
filters
Data from socket
Files
Audio
Objects
Filters
FileInputStream
InputStream
AudioInputStream
ObjectInputStream
SocketInputStream
13- InputStream is new BufferedInputStream( socket.
getInputStream() )is.read() - InputStream in new ZipInputStream ( new
FileInputStream() )in.read() - InputStream in new BufferredInputStream(
new ZipInputStream( socket.getInputStream()
))in.read()
14OutputStream
- OutputStream objects can be decorated with an
arbitrary number of filters. - OutputStream classes
- FileOutputStream
- ObjectOutputStream
- PipedOutputStream
- etc.
- SocketOutputStream (hidden)
out
OutputStream abstract
write()close()
FilterOutputStream
OutputStream classes
write()close()
filters
Data from socket
Files
Objects
Filters
FileOutputStream
OutputStream
ObjectOutputStream
SocketOutputStream
15Initialization of OutputStream
- OutputStream os new FileOutputStream(foo.txt)
OutputStream os new FileOutputStream( System.in
)os.write() - Socket socket new Socket()OutputStream os
socket.getOutputStream()os.write() - System.out.println(socket.getOutputStream().toStri
ng() ) - java.net.SocketOutputStream_at_11d32c
Data from socket
Files
Filters
FileOutputStream
InputStream
SocketOutputStream
16- Some OutputStream objects can be connected with
each other. - OutputStream os new ObjectOutputStream( new
FileOutputStream( objectfile) )
Object
ObjectInputStream
FileInputStream
17Filters of OutputStream
- Filters
- BufferedOutputStream
- PrintStream
- CheckedOutputStream (java.util.zip)
- ZipOutputStream (java.util.zip)
- GZIPOutputStream (java.util.zip)
- JarOutputStream (java.util.zip)
- CipherOutputStream (javax.crypto)
- DifestOutputStream (java.security)
- etc.
- A single OutputStream object can be decorated
with multiple filters.
out
OutputStream abstract
write()close()
FilterOutputStream
OutputStream classes
write()close()
filters
18- OutputStream os new ZipOutputStream( new
FileOutputStream() )os.write() - OutputStream os new BufferredOutputStream( ne
w ZipOutputStream( socket.getOutputStream()
))os.write() - OutputStream os new PrintStream(
socket.getOutputStream() )os.write()
19Readers, Writers and Utility Classes
- PrintWriter
- Scanner
- A text scanner that can parse primitive types and
strings using regular expressions. - FileReader
- Convenience class for reading character files.
- FileWriter
- Convenience class for writing character files.