1 CSE 331 Design Patterns 2: Prototype, Factory slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

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.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
GUI Tutorial Images. Useful Info – not on final exam.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
Building Java Programs Graphics Reading: Supplement 3G.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Building Java Programs
Building Java Programs
A note on Programming Assignment
Building Java Programs
Chapter 10 Graphics.
Basic Graphics Drawing Shapes 1.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Building Java Programs
Building Java Programs
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

1 CSE 331 Design Patterns 2: Prototype, Factory slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer

2 Pattern: Prototype An object that serves as a basis for creation of others

3 Objects as prototypes prototype: An object that serves as a template or model to assist in the creation of other similar/equal objects. Problem: Client wants another object similar to an existing one, but doesn't care about the details of the state of that object.  Sometimes client doesn't even care about the object's exact type. Solution: Clone or copy the object's state into a new object, modify as needed, then use it.  Often closely related to Java's clone method.  Sometimes done with producer methods that return new objects. (Prototype is a creational pattern.)

4 Scenario: Store products Suppose a store has a hierarchy of classes representing products. public class Product {...} public class Book extends Product {...} public class DVD extends Product {...}  The store keeps a large catalog of all products by ID. Customers want to buy products from the catalog and put them into their shopping carts.  The add-to-cart code doesn't want to worry about what kind of product is being bought, its state, etc.  We don't want to add the original product to the customer's cart, because it is mutable and they will modify its price/status/etc.

5 Prototype store products The following code gives each customer his own product copy: // maps from product IDs to the products themselves private Map catalog;... public void addToCart(ShoppingCart cart, int id, double price) { Product p = catalog.get(id); p = p.clone(); // make a copy for this user p.setPrice(price); cart.add(p); }

6 Prototype producer method Sometimes the object serves as a prototype by supplying producer method(s) that return new objects similar to itself: public class Product implements Cloneable {... public Product clone() {... } // a new product like this one, but half price public Product halfPrice() { Product copy = this.clone(); copy.setPrice(this.getPrice() / 2); return copy; }

7 Drawing fonts/colors Suppose we want to draw fonts/colors on a graphical window.  We will make use of a CSE 142/143 class, DrawingPanel. To create a window: DrawingPanel name = new DrawingPanel( width, height ); Example: DrawingPanel panel = new DrawingPanel(300, 200); The window has nothing on it.  We draw shapes / lines on it with another object of type Graphics.

8 Graphics "Pen" or "paint brush" object to draw lines and shapes  import java.awt.*; // needed to use Graphics  Access it by calling getGraphics on your DrawingPanel. Graphics g = panel.getGraphics(); Draw shapes by calling methods on the Graphics object. g.fillRect(10, 30, 60, 35); g.fillOval(80, 40, 50, 70);

9 Graphics methods Method nameDescription g.drawImage( Image, x, y, [w, h], panel ); an image at the given x/y position and size g.drawLine( x1, y1, x2, y2 ); line between points (x1, y1), (x2, y2) g.drawOval( x, y, width, height ); outline largest oval that fits in a box of size width * height with top-left at (x, y) g.drawRect( x, y, width, height ); outline of rectangle of size width * height with top-left at (x, y) g.drawString( text, x, y ); text with bottom-left at (x, y) g.fillOval( x, y, width, height ); fill largest oval that fits in a box of size width * height with top-left at (x, y) g.fillRect( x, y, width, height ); fill rectangle of size width * height with top-left at (x, y) g.setColor( color ); paint any following shapes in the given color g.setFont( font ); draw any following text with the given font

10 Specifying colors Pass a Color to Graphics object's setColor method.  Specified by constructor, using Red-Green-Blue (RGB) values 0-255: Color brown = new Color(192, 128, 64);  Or use predefined Color class constants: Color. CONSTANT_NAME where CONSTANT_NAME is one of: BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW  Or create a new color, using an existing color as a prototype: public Color brighter() public Color darker()

11 Specifying fonts Pass a Font to Graphics object's setFont method.  Specified by the Font constructor: public Font(String name, int style, int size) Styles are represented as integer constants in the Font class: public static final int PLAIN public static final int BOLD public static final int ITALIC  Or create a new font, using an existing font as a prototype: public Font deriveFont(float size) public Font deriveFont(int style, float size)

12 Pattern: Factory A method or object that creates other objects

13 Factory pattern factory: A method or object whose primary purpose is to manage the creation of other objects (usually of a different type). Problem: Object creation is cumbersome or heavily coupled for a given client. Client needs to create but doesn't want the details. Factory Method pattern  A helper method that creates and returns the object(s).  Can return subclass objects if so desired (hidden from client). Abstract Factory pattern  A hierarchy of classes/objects, each of which is a factory for a type.  Allows hot-swappable factory to be used by a given client. (Factory is a creational pattern.)

14 DateFormat as a factory DateFormat class knows how to format dates/times as text  Options: Just date? Just time? Date+time? Where in the world?  Instead of passing all options to constructor, use factories.  The subtype created doesn't need to be specified. DateFormat df1 = DateFormat.getDateInstance(); DateFormat df2 = DateFormat.getTimeInstance(); DateFormat df3 = DateFormat.getDateInstance( DateFormat.FULL, Locale.FRANCE); Date today = new Date(); System.out.println(df1.format(today)); // "Apr 20, 2011" System.out.println(df2.format(today)); // "10:48:00 AM" System.out.println(df3.format(today)); // "mecredi 20 avril 2011"

15 Border factory Java graphical components like DrawingPanel can have borders: public void setBorder(Border border) But Border is an interface; cannot construct a new Border.  There are many different kinds of borders (classes). Instead, use the provided BorderFactory class to create them: public static Border createBevelBorder(...) public static Border createEtchedBorder(...) public static Border createLineBorder(...) public static Border createMatteBorder(...) public static Border createTitledBorder(...)  Avoids a constructor that takes too many "option / flag" arguments.

16 Scenario: Drawing images Suppose we want to display images on a graphical window. The Graphics object has a drawImage method:  public void drawImage(Image img, int x, int y, panel)  public void drawImage(Image img, int x, int y, int w, int h, panel) Images are hard drive files in a given format:  GIF, JPEG, PNG, BMP, TIFF,... So how do we get an Image object to draw? Can't simply say new Image :  Image img = new Image("bobafett.gif"); // error

17 Toolkits Toolkit is a class for GUI system info and resource loading. Java handles loading of images through Toolkit s:  public Image getImage(String filename)  public Image getImage(URL url) Can't simply say new Toolkit :  Toolkit tk = new Toolkit(); // error Have to call a static method to get a toolkit (Why? What is this?):  public static Toolkit getDefaultToolkit()  Toolkit tk = Toolkit.getDefaultToolkit(); // ok

18 Buggy image client The following well-intentioned client does not show the images: public static void main(String[] args) { Toolkit tk = Toolkit.getDefaultToolkit(); Image img1 = tk.getImage("calvin.gif"); Image img2 = tk.getImage("cuteicecream.jpg"); Image img3 = tk.getImage("tinman.png"); DrawingPanel panel = new DrawingPanel(600, 500); Graphics g = panel.getGraphics(); g.drawImage(img1, 0, 0, panel); g.drawImage(img2, 200, 50, panel); g.drawImage(img3, 400, 200, panel); }

19 Media trackers When you tell a toolkit to load an image, it doesn't actually do so.  It simply buffers a request to eventually load the image.  If you try to draw the image too quickly, it won't be loaded yet. Java uses media tracker objects to wait for an image to load:  public MediaTracker( panel )  public void addImage(Image img, int id)  public void removeImage(Image img)  public void removeImage(Image img, int id)  public void waitForAll() **  public void waitForAll(long ms) **  public void waitForID(int id) **  public void waitForID(int id, long ms) ** ** throws InterruptedException

20 Media tracker example public static void main(String[] args) { Toolkit tk = Toolkit.getDefaultToolkit(); Image img1 = tk.getImage("calvin.gif"); Image img2 = tk.getImage("cuteicecream.jpg"); Image img3 = tk.getImage("tinman.png"); MediaTracker mt = new MediaTracker(panel); mt.addImage(img1, 1); mt.addImage(img2, 2); mt.addImage(img3, 3); try { mt.waitForAll(); } catch (InterruptedException e) {} DrawingPanel panel = new DrawingPanel(600, 500); Graphics g = panel.getGraphics(); g.drawImage(img1, 0, 0, panel); g.drawImage(img2, 200, 50, panel); g.drawImage(img3, 400, 200, panel); }

21 Image loading factory The preceding code is too cumbersome to write every time we want to load an image.  Let's make a factory method to load images more easily: public static Image loadImage( String filename, DrawingPanel panel) { Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.getImage(filename); MediaTracker mt = new MediaTracker(panel); mt.addImage(img, 0); try { mt.waitForAll(); } catch (InterruptedException e) {} return img; }

22 A factory class Factory methods are often put into their own class for reusability: public class ImageFactory { public static Image loadImage( String filename, DrawingPanel panel) { Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.getImage(filename); MediaTracker mt = new MediaTracker(panel); mt.addImage(img, 0); try { mt.waitForAll(); } catch (InterruptedException e) {} return img; } public static Image loadImage( File file, DrawingPanel panel) { return loadImage(file.toString(), panel); }

23 Exercise: Caching factory Loading large images from the disk repeatedly can be slow. Write a modified version of the image factory that caches images so that it does not ever have to re-load the same image file twice. Things to think about:  Can you think of any cases where caching would not be desired?  How could we provide the client the ability to turn caching on/off?  Can we make this decision transparent to most client code, settable in a single place? Another possibility: A factory that loads images from URLs.

24 Abstract Factory pattern abstract factory: A superclass factory that can be extended to provide different sub-factories, each with different features.  Often implemented with an abstract superclass.  Idea: Client is given an instance of ImageFactory, which will actually be a Standard or Caching ImageFactory.  Client just uses it and doesn't worry about which one it was given.

25 Abstract factory code public abstract class ImageFactory { public abstract Image loadImage( String filename, DrawingPanel panel); } public class StandardImageFactory extends ImageFactory { public Image loadImage(String filename, DrawingPanel panel) {... } } public class CachingImageFactory extends ImageFactory { public Image loadImage(String filename, DrawingPanel panel) {... } } public class WebImageFactory extends ImageFactory { public Image loadImage(String filename, DrawingPanel panel) {... } }