Common Design Patterns

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Graphical User Interfaces (Part IV)
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Design Patterns and Graphical User Interfaces Horstmann ,
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Chapter 11 Arrays Continued
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 219 Patterns in Programming More Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
Iteration Abstraction SWE Software Construction Fall 2009.
Session 30 Final Review. Final Details Wednesday, December 14 at 8 AM Wright 5 (same classroom) Final will be comprehensive Open book Open notes Test.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Collections Dwight Deugo Nesa Matic
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Using the Java Collection Libraries COMP 103 # T2
EECE 310: Software Engineering
A First Look at GUI Applications
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
Ellen Walker Hiram College
Design Patterns in Swing and AWT
Design Patterns in Swing and AWT
08/15/09 Design Patterns James Brucker.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Graphical User Interface
Iteration Abstraction
Advanced Programming in Java
Data Structures & Algorithms
Model, View, Controller design pattern
Software Design Lecture : 39.
Graphical User Interface
Presentation transcript:

Common Design Patterns Some we’ll look at: The Command Pattern The Factory Pattern The Flyweight Pattern The Iterator Pattern The Observer Pattern The Singleton Pattern The State Pattern The Strategy Pattern Others: The Adapter Pattern The Bridge Pattern The Composite Pattern The Decorator Pattern The Proxy Pattern The Visitor Pattern In addition, different technologies have their own patterns: Servlet patterns, GUI patterns, etc …

Command Abstraction For many GUIs, a single function may be triggered by many means (e.g., keystroke, menu, button, etc…) we want to link all similar events to the same listener The information concerning the command can be abstracted to a separate command object Common Approach: specify a String for each command have listener respond to each command differently ensure commands are handled in a uniform way commands can be specified inside a text file The Command Pattern

Example Suppose I wanted to create a simple GUI: 1 colored panel 2 buttons, yellow & red 2 menu items, yellow & red clicking on the buttons or menu items changes the color of the panel Since the buttons & the menu items both perform the same function, they should be tied to the same commands I could even add popup menu items Example

Using Command Strings public class ColorCommandFrame1 extends JFrame implements ActionListener { private Toolkit tk = Toolkit.getDefaultToolkit(); private ImageIcon yellowIcon = new ImageIcon(tk.getImage("yellow_bullet.bmp")); private ImageIcon redIcon = new ImageIcon(tk.getImage("red_bullet.bmp")); private JPanel coloredPanel = new JPanel(); private JButton yellowButton = new JButton(yellowIcon); private JButton redButton = new JButton(redIcon); private JMenuBar menuBar = new JMenuBar(); private JMenu colorMenu = new JMenu("Color"); private JMenuItem yellowMenuItem = new JMenuItem(yellowIcon); private JMenuItem redMenuItem = new JMenuItem(redIcon); private JPopupMenu popupMenu = new JPopupMenu(); private JMenuItem yellowPopupItem = new JMenuItem(yellowIcon); private JMenuItem redPopupItem = new JMenuItem(redIcon); private static final String YELLOW_COMMAND = "YELLOW_COMMAND"; private static final String RED_COMMAND = "RED_COMMAND";

public ColorCommandFrame1() { super("ColorCommandFrame1"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setExtendedState(JFrame.MAXIMIZED_BOTH); initButtons(); initPopupMenu(); initMenu(); } public void initButtons() { yellowButton.setActionCommand(YELLOW_COMMAND); redButton.setActionCommand(RED_COMMAND); yellowButton.addActionListener(this); redButton.addActionListener(this); coloredPanel.add(yellowButton); coloredPanel.add(redButton); Container contentPane = getContentPane(); contentPane.add(coloredPanel);

public void initPopupMenu() { yellowPopupItem.setActionCommand(YELLOW_COMMAND); redPopupItem.setActionCommand(RED_COMMAND); yellowPopupItem.addActionListener(this); redPopupItem.addActionListener(this); popupMenu.add(yellowPopupItem); popupMenu.add(redPopupItem); coloredPanel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popupMenu.show(e.getComponent(), e.getX(), e.getY()); });

public void initMenu() { yellowMenuItem.setActionCommand(YELLOW_COMMAND); redMenuItem.setActionCommand(RED_COMMAND); yellowMenuItem.addActionListener(this); redMenuItem.addActionListener(this); colorMenu.add(yellowMenuItem); colorMenu.add(redMenuItem); menuBar.add(colorMenu); setJMenuBar(menuBar); } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); if (command.equals(YELLOW_COMMAND)) coloredPanel.setBackground(Color.YELLOW); else if (command.equals(RED_COMMAND)) coloredPanel.setBackground(Color.RED);

Note: we did not use The Command Pattern We used separate event handler objects for each type of interaction (IMO this is better) Some would call the command pattern an anti-pattern What’s an anti-pattern? a pattern to avoid What’s wrong with it? makes for a long link of if statements can make for a long method/easy to make a mistake

The Factory Pattern A factory method creates an object of some class. A factory class can provide many such methods, each which may produce different objects that may all implement a similar interface Hides object creation An iterator uses a factory method it creates a generator object. The iterator hides the exact type of generator being used. private inner class that implements an interface the using code depends only on the Iterator interface. we can replace the inner class and all those using the iterator will not need to be changed Ex. Use of factory pattern in Java API: BorderFactory.createXXXBorder All methods return Border objects (which is actually an interface) Object returned may actually be a BevelBorder, EtchedBorder, etc … lots of factory classes in security packages

Border Example … JPanel panel = new JPanel(); Border border = BorderFactory.createEtchedBorder(); panel.setBorder(border); JPanel panel2 = new JPanel(); Border border2 = BorderFactory.createTitledBorder("Title"); Panel2.setBorder(border2);

How could we implement a Factory Pattern? public class BorderFactory { public static Border createEtchedBorder() { return new EtchedBorder(); } public static Border createTitledBorder(String title) { return new TitledBorder(title); … public class EtchedBorder implements Border { // Border Methods } public class TitledBorder implements Border { // Border Methods }

Factory Pattern – What’s the point? The programmer using the Factory class never needs to know about the actual class/type Simplifies use for programmer Ex: Using BorderFactory, one only needs to know Border & BorderFactory not TitledBorder, BeveledBorder, EtchedBorder, etc.

The Flyweight Pattern A “neat hack” Allows one object to be used to represent many identical instances Flyweights must be immutable. Flyweights depend on an associated table maps identical instances to the single object that represents all of them Used in processing many large documents search engines a document as an array of immutable Strings repeated Words would share objects just one object for “the” referenced all over the place use static Hashtable to store mappings use javax.naming.Context to provide String to Object binding

Iteration What’s the problem? Solution: you have to perform some operation on a sequence of elements in a given data structure Solution: Iterator Pattern a.k.a. Iteration Abstraction iterate over a group of objects without revealing details of how the items are obtained Think of someone else using a class you define

Iterator An Iterator produces proper elements for processing Defining an Iterator may be complex Using an Iterator must be simple they’re all used in the same way E.g. update() all elements of List list: Iterator it; for (it=list.listIterator(); it.hasNext(); ) it.next().update(); Makes iteration through elements of a set “higher level” Separates the production of elements for iteration from the operation at each step in the iteration.

Iterator (cont’d) Iterator is a design pattern that is encountered very often. Problem: Mechanism to operate on every element of a set. Context: The set is represented in some data structure (list, array, hashtable, etc.) Solution: Provide a way to iterate through every element. Common Classes using Iterators in Java API StringTokenizer Vector, ArrayList, etc … Even I/O streams work like Iterators

Iterator (in Java) public interface Iterator { // Returns true if there are more // elements to iterate over; false // otherwise public boolean hasNext(); // If there are more elements to // iterate over, returns the next one. // Modifies the state “this” to record // that it has returned the element. // If no elements remain, throw // NoSuchElementException. public Object next() throws NoSuchElementException; public void remove(); }

Iterator vs. Enumeration Java provides another interface Enumeration for iterating over a collection. Iterator is newer (since JDK 1.2) has shorter method names has a remove() method to remove elements from a collection during iteration Iterator Enumeration hasNext() hasMoreElements() next() nextElement() remove() - Iterator is recommended for new implementations.

Example Loop controlled by next() private Payroll payroll = new Payroll(); ... public void decreasePayroll() { Iterator it = payroll.getIterator(); while (it.hasNext()) { Employee e = (Employee)it.next(); double salary = e.getSalary(); e.setSalary(salary*.9); }

Implementing an Iterator public class Payroll { private Employee[] employees; private int num_employees; ... // An iterator to loop through all Employees public Iterator getIterator() { return new EmplGen(); } private class EmplGen implements Iterator { // see next slide

Implementing an Iterator private class EmplGen implements Iterator { private int n = 0; public boolean hasNext() { return n < num_employees; } public Object next() throws NoSuchElementException { Object obj; if (n < num_employees) { obj = employees[n]; n++; return obj; else throw new NoSuchElementException ("No More Employees"); state of iteration captured by index n returns true if there is an element left to iterate over returns the next element in the iteration sequence

More Complex Controls Controls like tables, trees, lists, combo boxes may contain much data as well as functionality For controls like these, data is managed separate from the view What type of design pattern could we use? many similar types of software problems solution: Observer pattern – MVC Must be used for GUI controls when contents change Ex: combo box for browser address bar ability to add more addresses

Observer Pattern (MVC) Model data structure, no visual representation notifies views when something interesting happens Views visual representations views attach themselves to model in order to be notified Controllers handling of events listeners that are attached to view in order to be notified of user interaction (or otherwise) MVC Interaction controller updates model model tells view that data has changed view redrawn

Controller Model View updateData notify repaint return return

MVC Architecture Model View Controller The model passes its data to the view for rendering Model View The view determines which events are passed to the controller The controller updates the model based on events received Controller

Common Model Interfaces BoundedRangeModel for JSlider, JProgressBar, JScrollBar ComboBoxModel for JComboBox ListModel for JList TableModel for JTable TreeModel for JTree

Trees Used to display a hierarchical structure File structure, browsing history, etc…

Editing To edit the tree, you must go through the model: JTree tree = new JTree(… TreeModel model = tree.getModel(); // Insert Node model.insertNodeInto(… // Remove Node model.removeNodeFromParent(… // Change Node model.nodeChanged(… // UPDATING THE MODEL WILL NOW AUTOMATICALLY UPDATE THE // VIEW (JTree) THANKS TO MVC!

The Singleton Pattern The singleton pattern is used when a type may only have one object of that type constructed: to enforce this condition, the constructor must be made private. singleton object favorable to fully static class, why? can be used as a method argument class can be extended the single object is created by calling a static method that creates the object the first time it is called same method also typically retrieves object

Example: Making FilmReviewArchiver a Singleton public class FilmReviewArchiver { private static FilmReviewArchiver fra = null; private FilmReviewArchiver() {} public static FilmReviewArchiver getFRA() { if (fra == null) { fra = new FilmReviewArchiver(); } return fra; public void update() // PROVIDE PUBLIC METHODS FOR OTHERS WHO // CAN GET THIS OBJECT TO UPDATE THE APP …

Why is this so good? Why is this so great? Other classes may now easily USE a FilmReviewArchiver Ex: FilmReviewArchiver fra = FilmReviewArchiver.getFRA(); fra.update(); Don’t have to worry about passing objects around Don’t have to worry about object consistency Note: the singleton is of course only good for classes that will never need more than one instance in an application

State Pattern Dynamically change the representation of an object. also called Data Abstraction Users of the object are unaware of the change. Example: Implement a set as a Vector if the number of elements is small Implement a set as a Hashtable if the number of elements is large State pattern is used only on mutable objects.

Example: Set public class Set { private Object elements; public boolean isIn(Object member){ if (elements instanceof Vector) // search using Vector methods else // search using Hashtable methods } public void add(Object member){ // add using Vector methods // add using Hashtable methods

Using the state pattern: SetRep public interface SetRep { public void add(object member); public boolean isIn(Object member); public int size(); public void remove(); } public class SmallSet implements SetRep { private Vector set; public void add(Object element) { ... } public void remove() { ... } ... public class LargeSet implements SetRep { private Hashtable set;

Using the state pattern: a new Set public class Set { private SetRep rep; private int threshold; public void add(Object element) { if (rep.size() == threshold) rep = new LargeSet(rep.elements()); rep.add(element); } public void remove(Object element) { rep.remove(elem); if (rep.size == threshold) rep = new SmallSet(rep.elements());

Layout Managers A layout manager uses an algorithm to position and size GUI components for a given container When the user resizes the container, the layout manager automatically reflows the components to fill the available space LayoutManager An interface in the Java class library Describes how a Container and a layout manager communicate. It describes several methods which: Handle resizing Handle components added to or removed from the container. Size and position the components it manages.

Strategy Pattern Place essential steps for an algorithm in a strategy interface different methods represent different parts of the algorithm classes implementing this interface customize methods LayoutManagers use this pattern you may use a pre-existing LayoutManager you may create your own LayoutManager define a class that implements LayoutManager Define abstract methods from LayoutManager interface however you want your manager to behave addLayoutComponent layoutContainer minimumLayoutSize preferredLayoutSize removeLayoutComponent you may use to setLayout for Java Components to use your layout Java Components use LayoutManager methods to draw themselves