Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Chapter 22 Object-Oriented Systems Analysis and Design and UML Systems Analysis and Design Kendall and Kendall Fifth Edition.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
Chapter 6: Using Design Patterns
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Chapter 22 Object-Oriented Design
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Copyright 2002 Prentice-Hall, Inc. Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich Chapter 20 Object-Oriented.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Introduction To System Analysis and Design
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Aniruddha Chakrabarti
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 210 Introduction to Design Patterns September 7 th, 2006.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Introduction to Java Beans CIS 421 Web-based Java Programming.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CSC 480 Software Engineering Design With Patterns.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
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.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
Software Design Refinement Using Design Patterns
MPCS – Advanced java Programming
A Brief Introduction to Design Patterns
Instructor: Dr. Hany H. Ammar
Presented by Igor Ivković
Design Patterns - A few examples
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Behavioral Patterns (1)
Advanced ProgramMING Practices
Advanced ProgramMING Practices
Chapter 8, Design Patterns Introduction
Presented by Igor Ivković
Software Design Lecture : 40.
Presentation transcript:

Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July 14, 1998)

Implementing Design Patterns Using Java What are Design Patterns? A Design Pattern systematically names, explains, and evaluates an important and recurring design in object- oriented systems. –Patterns are also used outside of the software profession Software Design Patterns… –are typically targeted toward object-oriented development –describe time-proven ways in which good OO concepts can be used to solve common problems encapsulation, inheritance, polymorphism –satisfy specific application needs –patterns are not domain specific

Implementing Design Patterns Using Java Four essential elements Pattern Name –a word or two to describe the pattern –allows designers to communicate using a common vocabulary Problem –describes where to apply the pattern Solution –describes the elements that make up a design –does not describe a particular, concrete solution Consequences –results and trade-offs of applying the pattern

Implementing Design Patterns Using Java 3 Basic Types of Patterns Creational –a family of patterns which abstract the creation of new objects Structural –describe how to compose groups of cooperating objects into larger systems Behavioral –characterize patterns of communication between a system of objects

Implementing Design Patterns Using Java How are Patterns Used? Salvaging legacy systems –the Facade pattern can be used to simplify the interface to a complex legacy system –the Adapter pattern allows designers to adapt the interface of an existing class or application to work with newer code Distributed applications –the Observable pattern is described in detail later in this presentation Object-Oriented class libraries –Iterator provides a way to access the elements of a collection without violating encapsulation

Implementing Design Patterns Using Java The Spaghetti Problem As the number of objects increases, the number of potential communication paths increases geometrically If structural and behavioral patterns are not used, code quickly becomes too complex to understand 3 objects4 objects 5 objects 10 objects 3 paths 6 paths 10 paths 45 paths

Implementing Design Patterns Using Java How Not to Do It Arrows indicate "update" messages

Implementing Design Patterns Using Java The Observable Pattern A notification mechanism which allows one object to notify many observers when a state change has occurred Known by many names –Doc-View, Model-View-Controller, Subject-Observer, Publish- Subscribe –the example implementation uses Model and View classes the Model contains data (Subject) one or more Views (Observers) display the data contained in the Model

Implementing Design Patterns Using Java Typical Uses Distributed computing –a server may notify many client machines when data changes –CORBA, RMI, Sockets, or other forms of communication may be utilized - patterns are independent of implementation Graphical User Interfaces –a spreadsheet notifies several different graphical charts whenever a cell is edited –Emacs and other text editors provide paned windows which display different views of the same document –CAD programs allow multiple 3D views of the same data

Implementing Design Patterns Using Java The "Observable" Approach ColorModel setColor() notify() Views are not dependent upon one another –new views can be coded without impacting existing code Views are always displaying the correct data Ownership of the "Color" object is clearly defined in the ColorModel class

Implementing Design Patterns Using Java Implementing the Pattern Extend the Model class or implement the ModelI interface –business data is stored in your subclass –no GUI code is written here –no explicit connections to view subclasses Implement the ViewI interface –GUI panels, Frames, and Dialogs will do this –may hold a reference to one or more Model subclasses –provide a visual representation of business data may be read-only or editable must update display when notified by the Model

Implementing Design Patterns Using Java UML Object Model ColorModel ColorPreviewDialog Model {abstract} addView removeView notifyChanged > ViewI modelChanged * > ModelI addView removeView notifyChanged RGBScrollbarEditorDlg ColorTextEditorDialog extends implements dependency

Implementing Design Patterns Using Java ModelI.java /** * Define a protocol for notifying dependent views whenever a data model * changes. This is an implementation of the Observable pattern. Eric M. Burke, Object Computing, Inc. $Name$ $Revision$ */ public interface ModelI { /** v the view to register. */ void addView(ViewI v); /** v the view to un-register. */ void removeView(ViewI v); /** * Notify all registered views that this model has changed. obj optional data to pass to each view. May be null. */ void notifyChanged(Object obj); }

Implementing Design Patterns Using Java ViewI.java package com.ociweb.patterns.observer; /** * A notification mechanism when a data model changes. Eric M. Burke, Object Computing, Inc. $Name$ $Revision$ */ public interface ViewI { /** * The data model has changed. Views implementing this interface * should update their displays. model the data model which has changed. obj optional data from the model, may be null. */ void modelChanged(ModelI model, Object obj); }

Implementing Design Patterns Using Java ColorModel.java /** * A concrete type of Model. Represents an AWT Color. */ public class ColorModel extends Model { private Color color; /** a reference to a Color object. */ public Color getColor() { return color; } /** * Set a new color and notify dependent views. color the new Color. */ public void setColor(Color color) { this.color = color; notifyChanged(null); }

Implementing Design Patterns Using Java ColorPreviewDialog.java // some details omitted to fit on slide public class ColorPreviewDialog extends Dialog implements ViewI { ColorModel colorModel; public ColorPreviewDialog(Frame parent, ColorModel model) { super(parent, "Color Preview", false); this.colorModel = model; model.addView(this); setBackground(model.getColor()); setSize(150,150); } // implement the ViewI interface public void modelChanged(ModelI model, Object obj) { setBackground(colorModel.getColor()); }

Implementing Design Patterns Using Java JDK Observable The JDK includes a class called java.util.Observable –important methods include: addObserver(Observer o) deleteObserver(Observer o) notifyObservers(Object arg) setChanged(), clearChanged() Why use Model.java and ModelI.java instead of just using java.util.Observable? –Observable requires the extra step of setting the "changed" flag –Observable is a class, which requires inheritance ModelI is an interface, which offers more flexibility

Implementing Design Patterns Using Java Advantages of the Observable Pattern Business data can be coded independently of specific GUI views –developers can work independently –text-only unit tests can be written to test data model before GUI is finished GUI views have no knowledge of each other –allows developers to work independently –allows new views to be added later without breaking existing views –reduces web of interconnected views

Implementing Design Patterns Using Java JDK Patterns Java is full of patterns - look at the source code –Factory Methodjava.util.Calendar –Compositejava.awt.Container –Iteratorjava.util.Enumeration –Observerjava.util.Observer –Strategyjava.awt.LayoutManager The entire JavaBeans and AWT 1.1 event model is based upon a variation of the Observer pattern

Implementing Design Patterns Using Java Learning More Read the book "Design Patterns: Elements of Reusable Object-Oriented Software" Addison-Wesley, Gamma, Helm, Johnson, Vlissides Study the JDK source code Practice –learning to recognize recurrent patterns and apply them to concrete designs takes experience

Implementing Design Patterns Using Java Time for Demo and Questions... Source code for demonstration application can be found at –look for the Knowledge Base, under July, 1998