Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
Computer Science 209 Software Development Equality and Comparisons.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
GUI and event-driven programming An introduction.
Applets, AWTS CompSci 230 Software Construction.
Design Patterns and Graphical User Interfaces Horstmann ,
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Interfaces & Polymorphism part 2:
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Field Trip # 21 Creating PDFs with Java By Keith Lynn.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Computer Science 209 GUIs Model/View/Controller Layouts.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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:
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Chapter 7 A First Look at GUI Applications Layout Managers.
Chapter 5 Patterns and GUI Programming -Part 2-. STRATEGY Pattern Layout Managers What if we need to specifies pixel position of components when  User.
“Form Ever Follows Function” Louis Henri Sullivan
GUIs Model/View/Controller Layouts
Software Development Java Classes and Methods
Java GUI.
University of Central Florida COP 3330 Object Oriented Programming
Graphical User Interface (pronounced "gooey")
Chapter 4 Interface Types and Polymorphism Part 1
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Interfaces and Inheritance
Graphical User Interfaces -- Introduction
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Tim McKenna Layout Mangers in Java Tim McKenna
Panels & Layout Managers
A Quick Java Swing Tutorial
Border Layout, using Panels, Introduction to PinBallGame
Computer Science 312 What’s New in Java 8? 1.
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Computer Science 209 The Strategy Pattern I: Comparisons and Layouts

The Context of the Strategy Pattern I want to supply an algorithm or set of algorithms to be run in the context of a server ’ s class Example: I want to sort elements in a list according to several different fields (title, author, ISBN, etc.)

Solution of the Strategy Pattern I implement an interface whose methods specify the algorithms I want to be run in the server ’ s class The server provides one or more methods that accept parameters of my interface type The server runs the algorithms in that interface by running the appropriate methods on the parameter objects that I have passed to it

Example: Comparator I supply a comparison algorithm for a distinct ordering of elements by –Creating a class that implements Comparator –Implementing the compare method –Passing an instance of that class to the server The server, such as Collections.sort, applies my algorithm by running my object ’ s compare method with the collection ’ s elements

Distinct Responsibilities > Comparator a Comparator object > List a List object Collections.sort(aList, aComparator) The sort method implements the sorting strategy The Comparator object implements the comparison strategy in the method Compare

Comparable and compareTo public interface Comparable { // Returns 0 if receiver equals other // Returns < 0 if receiver is less than other // Returns > 0 if receiver is greater than other public int compareTo(T other); } public class Student implements Comparable { public int compareTo(Student other){ return name.compareTo(other.name); } Students are by default used in contexts wherein they are ordered by name.

Multiple Orderings A client might ask for a list of students sorted by average grade or another list of students sorted by highest grade compareTo provides just one, default ordering Use a Comparator object other orderings

Using a Comparator public static void sort(List list, Comparator c) A second Collections.sort method expects an object whose class implements the Comparator interface. The sort method uses the comparator object ’ s compare method to sort the list.

Using a Comparator List list = new ArrayList (); Student s1 = new Student("Ken", 4); Student s2 = new Student("Simon", 4); list.add(s1); list.add(s2); Collections.sort(list, new ComparatorByHighScore()); Collections.sort(list, new ComparatorByAverageScore()); Each comparator object specifies a distinct ordering for comparisons.

public interface Comparator { // Returns 0 if obj1 equals obj2 // Returns < 0 if obj1 is less than obj2 // Returns > 0 if obj1 is greater than obj2 public int compare(T obj1, T obj2); } import java.util.Comparator; public class ComparatorByHighScore implements Comparator { public int compare(Student s1, Student s2){ return s1.getHighScore() - s2.getHighScore(); } Comparator and compare

public interface Comparator { // Returns 0 if obj1 equals obj2 // Returns < 0 if obj1 is less than obj2 // Returns > 0 if obj1 is greater than obj2 public int compare(T obj1, T obj2); } import java.util.Comparator; public class ComparatorByAverageScore implements Comparator { public int compare(Student s1, Student s2){ return s1.getAverage() - s2.getAverage(); }

Using Comparators We need a new comparator class for each new possible ordering We might need to use each comparator just once in our code Lots of new named classes add clutter to our system

Anonymous Classes An anonymous class is a class without a name It ’ s usually defined at the point where it ’ s instantiated An anonymous class is like inline code – it ’ s written at the place where it needs to be used and there is just one such place

Format for Creating an Anonymous Class new (){ } Creates an instance of an anonymous class that can be used wherever an object of the interface type is expected.

Example: Compare by Average Comparator comp1 = new Comparator (){ public int compare(Student s1, Student s2){ return s1.getAverage() - s2.getAverage(); } }; Collections.sort(list, comp1); Create the comparator and assign it to a variable. Then use the variable/comparator to sort the list.

Example: Compare by Title Collections.sort(list, new Comparator (){ public int compare(Student s1, Student s2){ return s1.getAverage() - s2.getAverage(); } }); You don ’ t even need the extra variable. Just create the comparator when it ’ s needed.

Components, Containers, and Layouts Some GUI components are primitives, such as buttons and fields Others are containers in which components can be placed, such as frames and panels (panels can be nested recursively) The manner of organizing components can vary with the container and with the application

Layout Managers Components are added to a container under the influence of a layout manager The default layout manager for frames and dialogs is BorderLayout The default layout manager for panels and applets is FlowLayout

The Layout Strategy The different layout managers implement the LayoutManager interface A container calls methods in this interface to lay out the components The user of the container supplies an instance of this interface for a particular type of layout Strategy pattern!

Common Layouts FlowLayout Wrap around effect BorderLayout 5 distinct areas GridLayout Two-dimensional grid of equal-sized areas GridBagLayout Allows stretching of cells across rows and columns

Grid Layouts public GridExample(){ Container c = getContentPane(); c.setLayout(new GridLayout(2, 2)); c.add(new JButton("One")); c.add(new JButton("Two")); c.add(new JButton("Three")); c.add(new JButton("Four")); } Cells are filled in row major order Components stretch to fill their cells