principles of software construction: objects, design,...

16
Fall 2014 School of Computer Science © 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis Principles of Software Construction: Objects, Design, and Concurrency Design Case Study: Stream I/O Some answers… Charlie Garrod Jonathan Aldrich

Upload: vuongliem

Post on 06-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

     

Fall  2014  

School of Computer Science

© 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis

Principles of Software Construction: Objects, Design, and Concurrency Design Case Study: Stream I/O Some answers…

Charlie Garrod Jonathan Aldrich

Page 2: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 2 15-­‐214

A challenge for you

• Identify the design patterns in this lecture §  For each design pattern you recognize, write:

• The class name • The design pattern • If you have time: At least one design goal or principle achieved by the pattern in this context

§ Hints: • Use the slides online to review the lecture • Design patterns include at least:

• Adapter • Decorator • Iterator • Marker Interface • Template Method

Page 3: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 3 15-­‐214

The stream abstraction

• A sequence of bytes

• May read 8 bits at a time, and close

java.io.InputStream  void                    close();  abstract  int    read();  int                      read(byte[]  b);  

• May write, flush and close

java.io.OutputStream  void                    close();  void                    flush();  abstract  void  write(int  b);  void                    write(byte[]  b);  

Page 4: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 4 15-­‐214

The stream abstraction

• A sequence of bytes

• May read 8 bits at a time, and close

java.io.InputStream  void                    close();  abstract  int    read();  int                      read(byte[]  b);  

• May write, flush and close

java.io.OutputStream  void                    close();  void                    flush();  abstract  void  write(int  b);  void                    write(byte[]  b);  

Template Method

Template Method

Iterator?

Page 5: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 5 15-­‐214

The reader/writer abstraction

• A sequence of characters in some encoding

• May read one character at a time and close

java.io.Reader  void                    close();  abstract  int    read();  int                      read(char[]  c);    

• May write, flush and close

java.io.Writer  void                    close();  void                    flush();  abstract  void  write(int  c);  void                    write(char[]  b);  

Page 6: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 6 15-­‐214

The reader/writer abstraction

• A sequence of characters in some encoding

• May read one character at a time and close

java.io.Reader  void                    close();  abstract  int    read();  int                      read(char[]  c);    

• May write, flush and close

java.io.Writer  void                    close();  void                    flush();  abstract  void  write(int  c);  void                    write(char[]  b);  

Template Method

Template Method

Iterator?

Page 7: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 7 15-­‐214

Implementing streams

• java.io.FileInputStream  § Reads from files, byte by byte

• java.io.ByteArrayInputStream  §  Provides a stream interface for a byte[]  

• Many APIs provide streams for network connections, database connections, … §  e.g., java.lang.System.in,  Socket.getInputStream(), Socket.getOutputStream(), …

Page 8: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 8 15-­‐214

Implementing streams

• java.io.FileInputStream  § Reads from files, byte by byte

• java.io.ByteArrayInputStream  §  Provides a stream interface for a byte[]  

• Many APIs provide streams for network connections, database connections, … §  e.g., java.lang.System.in,  Socket.getInputStream(), Socket.getOutputStream(), …

Adapter

Page 9: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 9 15-­‐214

Implementing readers/writers

• java.io.InputStreamReader  §  Provides a Reader interface for any InputStream, adding additional functionality for the character encoding • Read characters from files/the network using corresponding streams

• java.io.CharArrayReader  §  Provides a Reader interface for a char[]

• Some convenience classes: FileReader, StringReader, …

Page 10: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 10 15-­‐214

Implementing readers/writers

• java.io.InputStreamReader  §  Provides a Reader interface for any InputStream, adding additional functionality for the character encoding • Read characters from files/the network using corresponding streams

• java.io.CharArrayReader  §  Provides a Reader interface for a char[]

• Some convenience classes: FileReader, StringReader, …

Adapter

Adapter

Adapter

Page 11: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 11 15-­‐214

A better design to add functionality to streams

+write()+close()+flush()

«interface»OutputStream

+write()+close()+flush()

-­‐file

FileOutputStream

+write()-­‐buffer

ByteArrayOutputStream

+write()+close()+flush()

FilterOutputStream

+compress()

GZipOutputStream

+encrypt()

AESEncryptionStream

+writeInt()+writeString()+writeFloat()

DataOutputStream

1

-­‐out

1

delegating  allcalls  to  otheroutput  stream

Page 12: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 12 15-­‐214

A better design to add functionality to streams

+write()+close()+flush()

«interface»OutputStream

+write()+close()+flush()

-­‐file

FileOutputStream

+write()-­‐buffer

ByteArrayOutputStream

+write()+close()+flush()

FilterOutputStream

+compress()

GZipOutputStream

+encrypt()

AESEncryptionStream

+writeInt()+writeString()+writeFloat()

DataOutputStream

1

-­‐out

1

delegating  allcalls  to  otheroutput  stream

Decorator

Page 13: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 13 15-­‐214

To read and write arbitrary objects

• Your object must implement the java.io.Serializable interface § Methods: none

• If all of your data fields are themselves Serializable, Java can automatically serialize your class §  If not, will get runtime NotSerializableException!

• Can customize serialization by overriding special methods

See QABean.java and FileObjectExample.java

Page 14: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 14 15-­‐214

To read and write arbitrary objects

• Your object must implement the java.io.Serializable interface § Methods: none

• If all of your data fields are themselves Serializable, Java can automatically serialize your class §  If not, will get runtime NotSerializableException!

• Can customize serialization by overriding special methods

See QABean.java and FileObjectExample.java

Marker Interface

Page 15: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 15 15-­‐214

The java.util.Scanner  

• Provides  convenient  methods  for  reading  from  a  stream  

java.util.Scanner:  Scanner(InputStream  source);  Scanner(File  source);  void        close();  boolean  hasNextInt();  int          nextInt();  boolean  hasNextDouble();  double    nextDouble();  boolean  hasNextLine();  String    nextLine();  boolean  hasNext(Pattern  p);  String    next(Pattern  p);  …  

Page 16: Principles of Software Construction: Objects, Design, …charlie/courses/15-214/2014-fall/slides/14... · Principles of Software Construction: Objects, Design, and Concurrency

toad 16 15-­‐214

The java.util.Scanner  

• Provides  convenient  methods  for  reading  from  a  stream  

java.util.Scanner:  Scanner(InputStream  source);  Scanner(File  source);  void        close();  boolean  hasNextInt();  int          nextInt();  boolean  hasNextDouble();  double    nextDouble();  boolean  hasNextLine();  String    nextLine();  boolean  hasNext(Pattern  p);  String    next(Pattern  p);  …  

Iterator