design patterns structuralpatterns(theadapterpattern)

28
Design Patterns Chapter 8 A 1 Jul 3, 2022 Structural Patterns The Adapter Pattern Design Patterns Chapter 8A

Upload: apu

Post on 26-May-2015

387 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 1Apr 12, 2023

Structural PatternsThe Adapter Pattern

Design PatternsChapter 8A

Page 2: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 2Apr 12, 2023

Objectives

In this lecture, we will• Review structural design patterns• Discuss why such patterns are useful• Introduce the Adapter pattern• Discuss examples that take advantage of the

adapter pattern

Page 3: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 3Apr 12, 2023

Structural Patterns

• Structural Patterns are used when it is necessary to build larger structures that are composed of other existing classes and objects– Re-use of existing implementations

• Structural class patterns use inheritance • Structural object patterns use aggregation

• This lecture introduces the adapter pattern

Page 4: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 4Apr 12, 2023

What is an Adapter?

• A simple example of an adapter is an electric razor adapter

• Most British domestic sockets are standard three pin sockets– live, neutral and earth

• Most electric razors come with a two pin plug– That will not push into a three pin socket

• An electric razor adapter provides the link between the two– Modifies the interface of the razor to make it

compatible with the three pin socket

• What about British and Continental sockets?

Page 5: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 5Apr 12, 2023

Software Adapters

• In object oriented programming the same type of problem often arises

• A class, XClass, has been built and works well• A system has been built that works with objects that

satisfy a certain interface• The methods of the XClass provide all the

functionality required of the objects that work with the system

• BUT the interface of the XClass is not the interface required by the system

• What is the easiest solution that re-uses the XClass?– Write an adapter!

Page 6: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 6Apr 12, 2023

Dialog Boxes

• How is a dialog box displayed in Java?JOptionPane.showMessageDialog ( null, “This is a TEST” );

• What is displayed?

• Where does the image come from?• Can the image be changed?

Page 7: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 7Apr 12, 2023

The showMessageDialog method

• The declaration of this method is:public static void showMessageDialog

{Component parent,Object message,String title,int messageType,Icon anIcon

}• The last parameter is of type Icon; an interface

– Any object that implements the Icon interface can be used

Page 8: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 8Apr 12, 2023

Using an ImageIcon

• The ImageIcon class implements the Icon interface and can build an icon from a GIF file

• For example: JOptionPane.showMessageDialog ( null, // parent

"Hello", // message "Testing GIF", // title JOptionPane.INFORMATION_MESSAGE, // messageType

new ImageIcon("c:\\temp\\duke.gif") // anIcon );

Page 9: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 9Apr 12, 2023

What is the Icon interface?

• The definition of an Icon interface is:public interface Icon{

int getIconWidth () ; int getIconHeight ();

void paintIcon ( Component c, Graphics g, int x, int y);

}

Page 10: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 10Apr 12, 2023

A Simple Icon Class

public class PeteIcon implements Icon { private int size; public PeteIcon(int aSize) { size = aSize; } public int getIconWidth() { return size; } public int getIconHeight() { return size; } public void paintIcon (Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double e = new Ellipse2D.Double

(x,y,size,size); g2.setColor (Color.RED); g2.fill(e); g2.setColor(Color.BLACK); g2.drawString("Pete",size/3,size/2); }}

Page 11: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 11Apr 12, 2023

Using the Icon Class

• An instance of PeteIcon can be used anywhere an Icon is expected:

JOptionPane.showMessageDialog (

null, "Hello", "Testing PeteIcon", JOptionPane.INFORMATION_MESSAGE, new PeteIcon(100)

);

Page 12: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 12Apr 12, 2023

Building a User Interface

• In building a user interface frames are often used to contain a set of user interface components JFrame frame = new JFrame();

Container contentPane = frame.getContentPane();

JButton b1 = new JButton("Push");

contentPane.add(b1,BorderLayout.NORTH);

JButton b2 = new JButton("STOP"); contentPane.add(b2,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.show();

• The frame has a container that manages a collection of components; a button is an example of a JComponent

Page 13: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 13Apr 12, 2023

What is a JComponent

• JComponent is a superclass used in developing user interface components– JButton extends JComponent

• JComponent provides implementations for the methods that are used by a container

• Two key methods are:– paintComponent that draws the component– getPrefferedSize that returns the preferred

dimension of a component• A PeteIcon knows how to draw itself and can return

both a width and a height– But it cannot be used in place of a JComponent – The interface does not match

Page 14: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 14Apr 12, 2023

Using an Adapter

• An adapter is needed to use an instance of an Icon to fulfil the role of a JComponent– An Icon has the appropriate functionality but an

inappropriate interface

• The adapter must extend the JComponent class and contain an instance of an icon to do the work– Calling a method of the adapter may result in a call

to a method of the contained icon

Page 15: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 15Apr 12, 2023

An Icon Adapter Class

public class IconAdapter extends JComponent { private Icon icon; public IconAdapter (Icon aIcon) { this.icon = aIcon; } public void paintComponent(Graphics g) { icon.paintIcon(this,g,0,0); } public Dimension getPreferredSize() { return new Dimension( icon.getIconWidth(),

icon.getIconHeight()); }

}

Page 16: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 16Apr 12, 2023

Using the Adapter

• An instance of an IconAdapter can be used as a component– Built from an instance of any Icon

• For example: JComponent c = new IconAdapter ( new PeteIcon(100)) ; contentPane.add(c,BorderLayout.CENTER);

• Although a PeteIcon is used here any Icon could be used– The IconAdapter adapts an Icon

Page 17: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 17Apr 12, 2023

Using the Adapter Class

JFrame

«interface»Icon+paintComponent()

+getPreferredSize()

JComponent

+paintComponent()+getPreferredSize()

IconAdapter PeteIcon

1 1

Page 18: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 18Apr 12, 2023

The Adapter Pattern

• This is an example of the adapter pattern– Used as a structural object pattern

• The situation in which the pattern is useful can be summed up as:– You want to use an existing class, called the

adaptee, without modifying it– The system in which the class is to be used requires

conformance to a target interface that is different to the interface of the adaptee

– The adaptee has the functionality required by the target interface

Page 19: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 19Apr 12, 2023

The Adapter Pattern

• Intent– Convert the interface of a class into another

interface; the interface expected by a client– The adapter lets classes work together that could not

do so otherwise because of incompatible interfaces

• Also known as Wrapper• Applicability Use the adapter pattern when:

– You want to use an existing class, and its interface does not match the one that you need

– You want to create a re-useable class that co-operates with unrelated or unforeseen classes

Page 20: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 20Apr 12, 2023

The Adapter Pattern

Participants• Target

– Defines the domain-specific interface that the Client uses

• Client– Collaborates with objects that conform to the Target

interface

• Adaptee– Defines an existing interface that needs to be

adapted

• Adapter– Adapts the interface of the Adaptee to the Target

interface

Page 21: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 21Apr 12, 2023

The Adapter Pattern

• Structure– An object adapter relies on object composition:

Page 22: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 22Apr 12, 2023

The Adapter Pattern

• Structure– A class adapter uses multiple inheritance

Page 23: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 23Apr 12, 2023

Consequences – The Class Adapter

• A class Adapter adapts an Adaptee to a Target by commiting to a concrete Adaptee class – As a consequence it is not possible to adapt a class

and all of its subclasses

• A class Adapter allows the Adapter to override some of the Adaptee’s behaviour – because the Adapter is a subclass of the Adaptee

• A class Adapter introduces only one object and therefore no additional runtime overhead for method invocation

• Now compare these trade offs with an object adapter

Page 24: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 24Apr 12, 2023

Consequences – The Object Adapter

• An Object Adapter allows a single adapter to work with many Adpatees– The Adaptee itself and any subclasses of the

Adaptee– The Adapter can add functionality to all Adaptees at

once

• An Object Adapter makes it harder to override Adaptee behaviour– It is necessary to subclass the Adpatee and ensure

that the Adapter refers to the subclass rather than to the original Adaptee

Page 25: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 25Apr 12, 2023

How much adapting does an Adapter do?

• In the example seen earlier in this lecture the adapter did little more than interface conversion– It changed the name of one method– It implemented another method of the target

interface by calling two methods of the adaptee interface

• In other examples an adapter may do much more– It may use several methods of an adaptee plus some

additional code to implement a method of the target interface

– It may add additional behaviour, not provided in the adaptee, in order to fulfil the interface requirements of the target

Page 26: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 26Apr 12, 2023

Java and Design Patterns

• The Java language contains many examples of the use of design patterns

• For example consider the case where you have an input stream and you need a reader– An input stream reads bytes– A reader reads characters– The difference between the two is significant in

many languages

• In Java an InputStreamReader adapter is used– Reader myReader = new InputStreamReader

(System.in);

Page 27: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 27Apr 12, 2023

Java InputStreamReader Adapter

• In this case:

Adaptee InputStreamTarget ReaderAdapter InputStreamReaderClient the class that wants to read text from

an input stream

• As an exercise search for as many examples of the use of the adapter pattern as you can find in Java

Page 28: Design patterns structuralpatterns(theadapterpattern)

Design Patterns Chapter 8A 28Apr 12, 2023

Summary

In this lecture we have:• Reviewed structural design patterns• Discussed why such patterns are useful• Introduced the Adapter pattern• Discussed examples that take advantage of the

adapter pattern