Abstract Factory Pattern

Similar documents
Abstract Factory Pattern (Creational Pattern) Week 05 (2nd Half) Report 05 A. Firuza Aibara P11119

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

EVENTS, EVENT SOURCES AND LISTENERS

Java - Applets. public class Buttons extends Applet implements ActionListener

GUI Event Handlers (Part I)

MIT AITI Swing Event Model Lecture 17

Chapter 12 Advanced GUIs and Graphics

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Computer Science 210: Data Structures. Intro to Java Graphics

Swing from A to Z Some Simple Components. Preface

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java Event Handling -- 1

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Lecture 5: Java Graphics

CHAPTER 2. Java Overview

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSE 331 Software Design & Implementation

Introduction. Introduction

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Graphical Applications

Event Driven Programming

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

GUI Forms and Events, Part II

Lecture 3: Java Graphics & Events

8. Polymorphism and Inheritance

SD Module-1 Advanced JAVA

CS 134 Programming Exercise 7:

SD Module-1 Advanced JAVA. Assignment No. 4

GUI in Java TalentHome Solutions

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

Programming Languages and Techniques (CIS120)

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

The AWT Event Model 9

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

CP122 CS I. Abstract Interfaces, Graphical User Interfaces, Inheritance

Lecture 19 GUI Events

Programming Languages and Techniques (CIS120)

SINGLE EVENT HANDLING

Graphical Interfaces

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

CS2110. GUIS: Listening to Events

Chapter 17 Creating User Interfaces

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Class 16: The Swing Event Model

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Graphical User Interfaces. Comp 152

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

To gain experience using GUI components and listeners.

G51PRG: Introduction to Programming Second semester Applets and graphics

Programming Exercise. Scribbler

Chapter 1 GUI Applications

7. Program Frameworks

COMP 401 Recitation 8. Observer Pattern

Graphical User Interface (GUI)

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Agenda. Container and Component

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

Programming Language Concepts: Lecture 8

Dr. Hikmat A. M. AbdelJaber

Packages: Putting Classes Together

M257 Past Paper Oct 2008 Attempted Solution

Implementing Graphical User Interfaces

Original GUIs. IntroGUI 1

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Example: Building a Java GUI

Advanced Java Programming (17625) Event Handling. 20 Marks

Example: Building a Java GUI

Design Patterns: Prototype, State, Composite, Memento

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

Bean Communication. COMP434B Software Design. Bean Communication. Bean Communication. Bean Communication. Bean Communication

Design Patterns and Frameworks Command

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Chapter 14 Abstract Classes and Interfaces

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Graphical User Interfaces 2

Chapter 5: Enhancing Classes

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

AP CS Unit 11: Graphics and Events

CS2110. GUIS: Listening to Events

Graphical Interfaces

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Tecniche di Progettazione: Design Patterns

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Midterm assessment - MAKEUP Fall 2010

Transcription:

Abstract Factory Pattern Firuza Aibara IIT Bombay 25 August 2012 Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 1 / 20

Outline 1 Introduction 2 Scenario 3 Class Diagram 4 Interface 5 Attribute Class 6 Concrete Factory Classes 7 Abstract Driver Program Initialization/Definition Constructor Item Listener Action Listener Methods for Actions Display 8 What is achieved 9 References Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 2 / 20

Introduction Introduction Factory object that returns one of several group of classes.[1, p. 33]. Used to return any object of a class which in turn can return different objects. E.g. DocumentCreator class to create different documents (letters, reports, resume, thesis, etc.) Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 3 / 20

Scenario Scenario Electronic Device (Computer): Monitor, keyboard, mouse, printer, etc.. Size, color, and type More attributes/specifications can be added later. Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 4 / 20

Class Diagram Class Diagram Fig1: Abstract Factory Pattern Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 5 / 20

Interface Interface Interface / Base Class Defines methods of concrete factory classes Interface interface Computer { public Attribute gettype(); public Attribute getcolor(); public Attribute getsize(); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 6 / 20

Attribute Class Attribute Class Basic Class Returns name invoked by concrete factory class Methods of the concrete classes have return type of this class Attribute class Attribute { private String name; public Attribute(String pname) { name = pname; public String getattribute() { return name; Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 7 / 20

Concrete Factory Classes Concrete Factory Classes - I Subclasses that implements Base Class Returns instance of the basic class with some parameter/value Monitor class Monitor implements Computer { public Attribute gettype() { return new Attribute("LED Monitor"); public Attribute getcolor() { return new Attribute("Black"); public Attribute getsize() { return new Attribute("26 inches"); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 8 / 20

Concrete Factory Classes Concrete Factory Classes - II Speaker class Speaker implements Computer { public Attribute gettype() { return new Attribute("Satellite Speaker"); public Attribute getcolor() { return new Attribute("Pink"); public Attribute getsize() { return new Attribute("Big"); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 9 / 20

Concrete Factory Classes Concrete Factory Classes - III Mouse class Mouse implements Computer { public Attribute gettype() { return new Attribute("Bluetooth Mouse"); public Attribute getcolor() { return new Attribute("Silver"); public Attribute getsize() { return new Attribute("Mini"); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 10 / 20

Abstract Driver Program GUI(Output) - Abstract Driver Program Main Class Contains GUI, Listeners Returns one of the computer objects based on the radio button selected and button clicked Uses Frames Responds to various events(listeners) Invokes methods of the concrete class Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 11 / 20

Abstract Driver Program Initialization/Definition Initialization/Definition Begin public class configuration extends Frame implements ActionListener {... Initialization/Definition private Checkbox monitor, speaker, mouse; private Button Type, Color, Size, Quit; private Computer comp = null; private ComputerPanel cp; private String sizedevice = "", colordevice = "", typedevice = ""; Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 12 / 20

Constructor Abstract Driver Program Constructor Constructor public configuration() { super("abstract Factory Pattern"); setgui(); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 13 / 20

Item Listener - I Abstract Driver Program Item Listener Invoked when radio button is selected Invokes itemstatechanged() Creates object of concrete factory class class MonitorListener implements ItemListener { public void itemstatechanged(itemevent e) { comp = new Monitor(); clearcanvas(); class MouseListener implements ItemListener { public void itemstatechanged(itemevent e) { comp = new Mouse(); clearcanvas(); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 14 / 20

Item Listener - II Abstract Driver Program Item Listener class SpeakerListener implements ItemListener { public void itemstatechanged(itemevent e) { comp = new Speaker(); clearcanvas(); private void clearcanvas() { typedevice = ""; colordevice = ""; sizedevice = ""; cp.repaint(); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 15 / 20

Action Listener Abstract Driver Program Action Listener Invoked when command button is clicked Invokes actionperformed() Calls appropriate internal methods public void actionperformed(actionevent e) { Object obj = e.getsource(); if (obj == Type) settype(); if (obj == Color) setcolor(); if (obj == Size) setsize(); if (obj == Quit) System.exit(0); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 16 / 20

Abstract Driver Program Methods for Actions Methods for Actions Invokes particular method of a particular concrete factory class private void setcolor() { if (comp!= null) colordevice = comp.getcolor().getattribute(); cp.repaint(); private void setsize() { if (comp!= null) sizedevice = comp.getsize().getattribute(); cp.repaint(); private void settype() { if (comp!= null) typedevice = comp.gettype().getattribute(); cp.repaint(); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 17 / 20

Display Abstract Driver Program Display class ComputerPanel extends Panel { public void paint(graphics g) { Dimension sz = getsize(); g.drawrect(0, 0, sz.width - 1, sz.height - 1); g.drawstring("select a device", 50, 20); g.drawstring("click button to view values", 15, 40); g.drawstring(typedevice, 10, 140); g.drawstring(colordevice, 10, 180); g.drawstring(sizedevice, 10, 220); Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 18 / 20

What is achieved What is achieved Avoid hardcoding of individual concrete classes Separates functionality from client s view and code Subclasses(Concrete factory) can be added easily Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 19 / 20

References References [1] James W. Cooper. JAVA TM Design Patterns, A Tutorial. Pearson Education. Nov 1999. pp. 1-24. Indian Reprint 2004 Firuza Aibara (IIT Bombay) Abstract Factory Pattern 25 August 2012 20 / 20