Using Design Patterns in Java Application Development

24
8/14/2019 Using Design Patterns in Java Application Development http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 1/24 Using Design Patterns in Java  Application Development ExxonMobil Research & Engineering Co. Clinton, New Jersey Michael P. Redlich (908) 730-3416 [email protected]

Transcript of Using Design Patterns in Java Application Development

Page 1: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 1/24

Using Design Patterns in Java

 Application Development 

ExxonMobil Research & Engineering Co.

Clinton, New Jersey 

Michael P. Redlich

(908) 730-3416

[email protected]

Page 2: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 2/24

1NYSIA Java Users Group

October 25, 2005

 About Myself 

• Degree

 – B.S. in Computer Science

 – Rutgers University (go Scarlet Knights!)• ExxonMobil Research & Engineering

 – Senior Research Technician (1988-1998, 2004-present)

 – Systems Analyst (1998-2002)

• Ai-Logix, Inc. – Technical Support Engineer (2003-2004)

• ACGNJ

 – Java Users Group Leader 

• Publications – James: The Java Apache Mail Enterprise Server 

+ co-authored with Barry Burd

+ Java Boutique

Page 3: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 3/24

2NYSIA Java Users Group

October 25, 2005

Gang of Four (GoF)

• Erich Gamma

• Richard Helm

• Ralph Johnson• John Vlissides

• Design Patterns – Elements of Reusable Object-Oriented Software

 – Erich Gamma, et. al

 – ISBN 0-201-63361-2 – 1995

Page 4: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 4/24

3NYSIA Java Users Group

October 25, 2005

What are Design Patterns? 

• Recurring solutions to software design problems that are repeatedly

found in real-world application development

• All about the design and interaction of objects• Four essential elements:

 – The pattern name

 – The problem

 – The solution

 – The consequences

Page 5: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 5/24

4NYSIA Java Users Group

October 25, 2005

How Design Patterns Solve Design Problems

• Find appropriate objects

 – Helps identify less obvious abstractions

• Program to an interface, not an implementation

 – Clients should only know about abstract classes that define an

interface

 – Reduces implementation dependencies

• Design for change

 – Avoid creating objects directly

 – Avoid dependencies on specific operations

 – Avoid algorithmic dependencies – Avoid tight coupling

Page 6: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 6/24

5NYSIA Java Users Group

October 25, 2005

Design Pattern Categories

• Creational

 – Abstracts the instantiation process

 – Dynamically create objects so that they don’t have to be instantiateddirectly

• Structural

 – Composes groups of objects into larger structures

• Behavioral

 – Defines communication among objects in a given system

 – Provides better control of flow in a complex application

Page 7: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 7/24

6NYSIA Java Users Group

October 25, 2005

Creational Patterns

• Abstract Factory

 – Provides an interface for creating related objects without specifying

their concrete classes• Builder 

 – Reuses the construction process of a complex object

• Factory Method

 – Lets subclasses decide which class to instantiate from a defined

interface

• Prototype

 – Creates new objects by copying a prototype

• Singleton

 – Ensures a class has only one instance with a global point of access

to it

Page 8: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 8/24

7NYSIA Java Users Group

October 25, 2005

Structural Patterns

• Adapter 

 – Converts the interface of one class to an interface of another 

• Bridge

 – Decouples an abstraction from its implementation

• Composite

 – Composes objects into tree structures to represent hierarchies

• Decorator 

 – Attaches responsibilities to an object dynamically

• Façade

 – Provides a unified interface to a set of interfaces

Page 9: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 9/24

8NYSIA Java Users Group

October 25, 2005

Structural Patters (continued)

• Flyweight

 – Supports large numbers of fine-grained objects by sharing

• Proxy

 – Provides a surrogate for another object to control access to it

Page 10: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 10/24

9NYSIA Java Users Group

October 25, 2005

Behavioral Patterns

• Chain of Responsibility

 – Passes a request along a chain of objects until the appropriate one

handles it• Command

 – Encapsulates a request as an object

• Interpreter 

 – Defines a representation and an interpreter for a language grammar 

• Iterator 

 – Provides a way to access elements of an object sequentially without

exposing its implementation

• Mediator 

 – Defines an object that encapsulates how a set of objects interact

Page 11: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 11/24

10NYSIA Java Users Group

October 25, 2005

Behavioral Patterns (continued)

• Memento

 – Captures an object’s internal state so that it can be later restored to

that state if necessary• Observer 

 – Defines a one-to-many dependency among objects

• State

 – Allows an object to alter its behavior when its internal state changes• Strategy

 – Encapsulates a set of algorithms individually and makes theminterchangeable

• Template Method

 – Lets subclasses redefine certain steps of an algortithm

• Visitor 

 – Defines a new operation without changing the classes on which itoperates

Page 12: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 12/24

11NYSIA Java Users Group

October 25, 2005

Factory Method 

• Intent

 – Defines an interface for creating an object, but lets subclasses

decide which class to instantiate – Lets a class defer instantiation to subclasses

• Also known as

 – Virtual Constructor 

• Motivation

 – To solve the problem of one class knowing when to create a class of 

another type, but not knowing what kind of class to create

• Design Principle

 – Depend upon abstractions; do not depend upon concrete classes

Page 13: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 13/24

12NYSIA Java Users Group

October 25, 2005

Factory Method 

• Use this pattern when:

 – A class can’t anticipate the class of objects is must create

 – A class would prefer for its subclasses to specify the objects itcreates

 – There is a need for a class to localize one of several helper classes

that can be delegated a responsibility

Page 14: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 14/24

13NYSIA Java Users Group

October 25, 2005

Factory Method 

Page 15: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 15/24

14NYSIA Java Users Group

October 25, 2005

Decorator 

• Intent

 – Attaches additional responsibilities to an object dynamically

 – Provides a flexible alternative to subclassing for extendingfunctionality

• Also known as

 – Wrapper 

• Motivation

 – Allows classes to be easily extended to incorporate new behavior 

without modifying existing code

• Design Principle

 – Classes should be open for extension, but closed for modification

Page 16: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 16/24

15NYSIA Java Users Group

October 25, 2005

Decorator 

• Use this pattern:

 – To add responsibilities to individual objects dynamically and

transparently without affecting other objects – For responsibilities that can be withdrawn

 – When extension by subclassing is impractical

Page 17: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 17/24

16NYSIA Java Users Group

October 25, 2005

Decorator 

Page 18: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 18/24

17NYSIA Java Users Group

October 25, 2005

Mediator 

• Intent

 – Defines simplified communication among classes

 – Defines an object that encapsulates how a set of objects interact – Promotes loose coupling by keeping objects from referring to each

other explicitly, and allow the developer to vary their interaction

independently

• Motivation – To avoid the many interconnections among objects that can lead to

a maintenance headache

Page 19: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 19/24

18NYSIA Java Users Group

October 25, 2005

Mediator 

• Use this pattern when:

 – A set of objects communicate in well-defined but complex ways

 – Reusing an object is difficult because it refers to and communicateswith many other objects

 – A behavior that is distributed among several classes should be

customizable without a lot of subclassing

Page 20: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 20/24

19NYSIA Java Users Group

October 25, 2005

Mediator 

Page 21: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 21/24

20NYSIA Java Users Group

October 25, 2005

Observer 

• Intent

 – Defines a one-to-many dependency among objects so that when one

object changes state, all its dependents are notified and updatedautomatically

 – A way of notifying change to a number of classes

• Also known as

 – Dependents – Publish-Subscribe

• Motivation

 – To avoid making classes tightly coupled that would reduce their 

reusability

• Design Principle

 – Strive for loosely coupled designs among objects that interact

Page 22: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 22/24

21NYSIA Java Users Group

October 25, 2005

Observer 

• Use this pattern when:

 – A change to one object requires changing others, and the number of 

objects to be changed is unknown – An object should be able to notify other objects without making

assumptions about who these objects are

+ Avoids having these objects tightly coupled

Page 23: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 23/24

22NYSIA Java Users Group

October 25, 2005

Observer 

Page 24: Using Design Patterns in Java Application Development

8/14/2019 Using Design Patterns in Java Application Development

http://slidepdf.com/reader/full/using-design-patterns-in-java-application-development 24/24

23NYSIA Java Users Group

October 25 2005

Resources

• Design Patterns – Elements of Reusable Object-Oriented Software

 – Erich Gamma, et. al

 – ISBN 0-201-63361-2• Java Design Patterns

 – James W. Cooper 

 – ISBN 0-201-48539-7

• UML Distilled – Martin Fowler (with Kendall Scott)

 – ISBN 0-201-32563-2

• Head First Design Patterns

 – Eric & Elisabeth Freeman (with Kathy Sierra & Bert Bates) – ISBN 0-596-00712-4

• Data & Object Factory

 – http://www.dofactory.com/