Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adapter Pattern Context:

Similar presentations


Presentation on theme: "Adapter Pattern Context:"— Presentation transcript:

1 Adapter Pattern Context:
08/15/09 Adapter Pattern Context: You want to change the interface of an existing class (the adaptee) to a different interface. Forces: You want to be able to use a variety of objects to provide some functionality, without changing the application. But, the objects have different interfaces.

2 Readers as Adapters InputStream reads input as bytes.
08/15/09 Readers as Adapters InputStream reads input as bytes. int b = inputStream.read( ); InputStreamReader interprets the input as characters. InputStreamReader reader = new InputStreamReader( inputStream ); char c = (char) reader.read( ); BufferedReader groups the characters into lines BufferedReader bufReader = new BufferedReader( reader ); String line = bufReader.readLine( );

3 Adapter wraps a component
08/15/09 Adapter wraps a component IInputStream instream = new FileInputStream( "filename" ); InputStreamReader reader = new InputStreamReader( instream ); BufferedReader bufReader = new BufferedReader( reader ); String line = bufReader.readLine( ); BufferedReader (reads strings) InputStreamReader (read chars) InputStream (reads bytes)

4 08/15/09 "Adapters" in Swing A MouseListener (interface) requires you to implement 5 methods. If you only want to use 1 method, you must write 4 empty methods.


Download ppt "Adapter Pattern Context:"

Similar presentations


Ads by Google