3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

30
3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer

Transcript of 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Page 1: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

3-1

Three Design Patterns:Adapter, Façade, Flyweight

C Sc 335

Rick Mercer

Page 2: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Adapter Design Pattern

Gang of Four state the intent of Adapter is to Convert the interface of a class into another interface that

the clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.

Page 3: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Adapter Design Pattern

Problem: An "off the shelf" component offers compelling functionality that you would like to reuse, but its "view of the world" is not compatible with the philosophy and architecture of the new system

Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system

Use Adapter when you need a way to create a new interface for an object that does the right stuff but has the wrong interface

Page 4: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Adapter

Can be thought of as a Wrapper

Page 5: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Example Object Adapter

Before Java 5.0, we often adapted an ArrayList or HashMap to have an easier to use collection Used a Containment Relationship with a collection

using an ArrayList or HashMap instance variable Put the cast in the method once instead of everywhere

http://www.refactoring.com/catalog/encapsulateDowncast.html Add Employees rather than Objects (type safe) Method names then mean more to the clients

Employee getEmployeeWithID (String) good Object getEmployeeWithID (String) bad

Page 6: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Object Adapters

Object Adapters rely on one object (the adapting object) containing another (the adapted object)

A Stack class should have a Vector and use only Vector’s add, get, and size methods (aka Wrapper) Stack should not extend Vector:

java.lang.Object java.util.AbstractCollection<E>

java.util.AbstractList<E> java.util.Vector<E> java.util.Stack<E>

Page 7: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Class Adapters

Class Adapters also come about by extending a class or implementing an interface used by the client code

You may have used class adapters already Adapt a song collection so it could be stored in a ListModel object, which in turn was used by a JList to show a graphical view of the list elements

JList needs the methods defined in the ListModel interface: getSize() and getElementAt(int) Adapt a model to the interface needed by JList

Page 8: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

interface TableModel also adapts

A JTable requires a TableModel object that represents the data (model)

JTable expects your model to have getColumnCount, getRowCount, getValueAt JTable uses these methods to display view

Need to adapt the model to what JTable expects Adapt the model class to the interface expected by JTable by implementing all 10 methods in the model

Page 9: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Adapt a collection into a TableModel

JTable can shows a list of Employees like this

Page 10: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

EmployeeList adapted to TableModel

public class EmployeeList implements TableModel {

private ArrayList<Employee> data = new ArrayList<Employee>();

public EmployeeList() { data.add(new Employee("Devon", 40, 15.75, 3, "M")); data.add(new Employee("Kim", 0, 12.50, 1, "S")); data.add(new Employee("Chris", 35, 20.50, 2, "M")); }

public void add(Employee employee) { data.add(employee); }

public Iterator<Employee> iterator() { return data.iterator(); }}

Page 11: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

A TabelModel method helps adapt

// Adapt tax and pay methods to getValueAt(int column)public Object getValueAt(int rowIndex, int columnIndex) { Employee currentEmployee = data.get(rowIndex); double totalTaxes = currentEmployee.incomeTax() + currentEmployee.medicareTax() + currentEmployee.socialSecurityTax(); switch (columnIndex) { case 0: return currentEmployee.getName(); case 1: return currentEmployee.grossPay(); case 2: return totalTaxes; case 3: return data.get(rowIndex).grossPay() - totalTaxes; default: return null; }}

Page 12: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

A View, which is the Client

class EmployeeFrame extends JFrame { public static void main(String[] args) { new EmployeeFrame().setVisible(true); }

private EmployeeList threeEmps;

public EmployeeFrame() { threeEmps = new EmployeeList(); EmployeeList threeEmps = new EmployeeList(); setSize(300, 120); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable view = new JTable(threeEmps); this.add(view, BorderLayout.CENTER); }}

Page 13: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Adapter Example & General Form

Client: EmployFrame Adapter: JTable Adaptee: EmployeeList

Page 14: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Other Adapter Classes

The WindowListener interface has seven methods that you must implement

If you only need to respond to one window event, you can extend WindowAdapter and override whatever methods you need to

private class Terminator extends WindowAdapter { // This is a WindowAdapter, methods do nothing public void WindowClosing(WindowEvent e) { System.exit(0); } // the other 6 methods are in WindowAdapter

// and they are set to do nothing }

Page 15: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Besides WindowListener/WindowAdapter,Java has lots of Listener/Adapter pairs

package java.awt.event ComponentListener/ComponentAdapterContainerListener/ContainerAdapter FocusListener/FocusAdapterHierarchyBoundsListener/HierarchyBoundsAdapter KeyListener/KeyAdapter MouseListener/MouseAdapter MouseMotionListener/MouseMotionAdapter WindowListener/WindowAdapter

package java.awt.dnd DragSourceListener/DragSourceAdapter DragTargetListener/DragTargetAdapter

package javax.swing.event InternalFrameListener/InternalFrameAdapter MouseInputListener/MouseInputAdapter

Page 16: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Java Data Base Connectivity (JDBC) Adapter Write code in Java

using the methods of the JDBC Adapter

The Adapter creates SQL commands for you

Picture from IBM

Page 17: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

The Façade Design Pattern

Page 18: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Façade is closely related to Adapter

18

Provide a unified interface to a set of interfaces in a System. Façade defines a higher level interface that makes the subsystem easier to use GangOf4

Facade takes a "riddle wrapped in an enigma shrouded in mystery", and interjects a wrapper that tames the amorphous and inscrutable mass of software.SourceMaking

Page 19: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

A more UML-like view

Page 20: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Façade

Façade is used to Create a simpler interface Reduce the number of objects that a client deals with Hide or encapsulate a large system Decouples a client from a subsystem of components

Page 21: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Example of Façade

A CSc 436 student proposal to build a Façade …creating an open source library to introduce people to

the power of the OpenCL API. Why? Many people complain about the various intricacies of

the "boiler plate" code just to get things working. This library will handle all this for the user so they can focus on learning the techniques of OpenCL.

OpenCL™ is the first open, royalty-free standard for cross-platform, parallel programming of modern processors found in personal computers, servers and handheld/embedded devices.

Page 22: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Differences between Adapter, Façade

Whereas Adapter reuses an old interface, Facade defines a new interface

Adapter makes two existing interfaces (a bunch of methods) work together as opposed to defining a new one

Page 23: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Flyweight Design Pattern

Do we need one tree image or hundreds?

Page 24: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Flyweight Design Pattern

Do we need 40,621 String objects that equals “University of Arizona”?

CSc127A University of Arizona CSC 335

Page 25: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Flyweight Design Pattern

Could each tile use the same terrain instance?

Page 26: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Flyweight Design Pattern

Problem: Constructing many objects is a relatively slow operation for a computer to perform and/or it takes CPU time and memory to store objects therefore

In large programs we should try to reduce the number of objects we create while the program is running

Page 27: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

ImageIO.read(aFile)

Rumor has it that read will not read the image file if it has already been created

File f = new File(“megaman.gif"); BufferedImage sprites = ImageIO.read(f);

Rumor has it that read will not read the image file if it has already been created Read is a Flyweight

Page 28: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

String is a FlyWeight

Consider new String, ==, equals, and “a literal” Which assertions pass?String a = "horse";String b = "horse";assertTrue(a == b); // Pass? ______String c = new String("horse");assertTrue(a == c); // Pass? ______assertTrue(b == c); // Pass? ______assertTrue(b.equals(c)); // Pass? ______

Page 29: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Code demo

Flyweight images so there is only one image for each character to spell G A M E O V E R

Page 30: 3-1 Three Design Patterns: Adapter, Façade, Flyweight C Sc 335 Rick Mercer.

Flyweights often have a HashMap and check to see if the object existspublic class LetterFlyweight implements LetterInterface { private static final HashMap<Character, LetterFlyweight> myLetterTable = new HashMap<>(); private final Image myImage; private final char myChar;

private LetterFlyweight(Image aLetter, char aChar) { myImage = aLetter; myChar = aChar; }

public static LetterFlyweight getLetter(char key) { if (myLetterTable.containsKey(key)) return myLetterTable.get(key); else { String path = key + ".JPG";