design patterns based on book of gang of four (gof) erich gamma, richard helm, ralph johnson, and...

33
Design Patterns Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Elements of Reusable Object- Oriented Software Robert Papp ([email protected])

Upload: jillian-dyess

Post on 15-Dec-2015

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Design PatternsDesign Patternsbased on book of Gang of Four (GoF)Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Elements of Reusable Object-Oriented Software

Robert Papp ([email protected])

Page 2: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

INTRODUCTIONINTRODUCTION

2

Page 3: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

UML class diagram recallUML class diagram recall

Concrete

Abstract

Interface

# private* protected+ publicstatic

# private()* protected()+ public()abstract()static()name(Type1, Type2) : RetType

code

3

PackageClass1

Class2ClassN

derived

aggregatorcreator

caller/user

base

aggregateeproduct

callee/used

Page 4: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

What is a Design Pattern?What is a Design Pattern?A design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

4

Page 5: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

OK, but what is it?OK, but what is it?Pattern Name and Classification: A descriptive and unique name that helps in identifying and referring to the pattern.Intent: A description of the goal behind the pattern and the reason for using it.Also Known As: Other names for the pattern.Motivation (Forces): A scenario consisting of a problem and a context in which this pattern can be used.Applicability: Situations in which this pattern is usable; the context for the pattern.Structure: A graphical representation of the pattern. Class diagrams and Interaction diagrams may be used for this purpose.Participants: A listing of the classes and objects used in the pattern and their roles in the design.Collaboration: A description of how classes and objects used in the pattern interact with each other.Consequences: A description of the results, side effects, and trade offs caused by using the pattern.Implementation: A description of an implementation of the pattern; the solution part of the pattern.Sample Code: An illustration of how the pattern can be used in a programming language.Known Uses: Examples of real usages of the pattern.Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns.

5

Page 6: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

ClassificationClassificationCreational Structural Behavioral

Abstract Factory Adapter Null Object

Factory method Bridge Command

Builder Composite Interpreter

Lazy instantiation Decorator Iterator

Object pool Façade Mediator

Prototype Flyweight Memento

Singleton Proxy Observer

Multiton State

Resource acquisition is initialization

Chain of responsibility

Strategy

Specification

Template method

Visitor

6

Page 7: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Classification / 2Classification / 2Concurrency J2EE Architectural

Active Object Business Delegate Layers

Balking Composite Entity Presentation-abstraction-control

Double checked locking Composite View Three-tier

Guarded suspension Data Access Object Pipeline

Monitor object Fast Lane Reader Implicit invocation

Reactor Front Controller Blackboard system

Read/write lock Intercepting Filter Peer-to-peer

Scheduler Model-View-Controller

Event-Based Asynchronous

Service Locator Service-oriented architecture

Thread pool Session Facade Naked objects

Thread-specific storage Transfer Object

Value List Handler

View Helper

7

Page 8: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

ContentsContents• Simple patterns• Singleton• Template Method• Factory Method• Adapter• Proxy• Iterator• Complex patterns• Abstract Factory• Strategy• Mediator• Façade• J2EE patterns• Data Access Objects• Model-View-Controller• Session Façade• Front Controller

8

Page 9: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

SIMPLE PATTERNSSIMPLE PATTERNS

9

Page 10: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

SingletonSingletonEnsure a class has only one instance, and provide a global point of access to it.public class Singleton { private Singleton() {} private static Singleton _instance = null; public static Singleton getInstance () { if (_instance == null) _instance = new Singleton(); return _instance; } ...}

Lazy instantiationTactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

10return _instance;

Page 11: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Template MethodTemplate MethodDefine the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

11

...operation1();

...operation2();

...operationN();

...

Page 12: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Factory MethodFactory MethodDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

12

return new ConcreteProduct();

...Product product = CreateProduct();

...

Page 13: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

AdapterAdapterConvert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

13

_adaptee.SpecificRequest();

Page 14: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

ProxyProxyProvide a surrogate or placeholder for another object to control access to it.

14

_realService.someOperation();

Page 15: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

IteratorIteratorProvide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

15return ConcreteIterator(this);

Page 16: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

COMPLEX EXAMPLESCOMPLEX EXAMPLES

16

Page 17: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Abstract FactoryAbstract FactoryProvide an interface for creating families of related or dependent objects without specifying their concrete classes.

17

return new ProductB1();

return new ProductA1();

return new ProductB2();

return new ProductA2();

Page 18: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

StrategyStrategyDefine a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

18

_strategy.Algorithm();

Page 19: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

MediatorMediatorDefine an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

19

Page 20: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

FaFaççadeade

20

Class c1 = new Class1();Class c2 = new Class2();Class c3 = new Class3();c1.doStuff(c2);c3.setProp(c1.getProp2());return c3.doStuff2();

Package1Class1

Package2Class3

Package3Class2

Page 21: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

J2EE PATTERN J2EE PATTERN EXAMPLESEXAMPLES

http://java.sun.com/blueprints/patterns/catalog.html

21

Page 22: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Data Access Objects (DAO)Data Access Objects (DAO)Code that depends on specific features of data resources ties together business logic with data access logic. This makes it difficult to replace or modify an application's data resources.This pattern• separates a data resource's client

interface from its data access mechanisms;• adapts a specific data resource's access

API to a generic client interface;• allows data access mechanisms to

change independently of the code that uses the data.

22

Page 23: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Data Access Objects (DAO) / Data Access Objects (DAO) / 22BusinessObject: represents the data client.

DataAccessObject: abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source.DataSource: represents a data source implementation. TransferObject: the data carrier, DAO may use it to return data to the client.

23

Page 24: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Model-View-Controller (MVC)Model-View-Controller (MVC)Application presents content to users in numerous ways containing various data. The engineering team responsible for designing, implementing, and maintaining the application is composed of individuals with different skill sets.

24

Page 25: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Model-View-Controller / 2Model-View-Controller / 2

25

State change

View selection

User gestures

Change notification

State query

Page 26: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Front ControllerFront ControllerThe presentation-tier request handling mechanism must control and coordinate processing of each user across multiple requests. Such control mechanisms may be managed in either a centralized or decentralized manner.Problems:

◦Each view is required to provide its own system services, often resulting in duplicate code.

◦View navigation is left to the views. This may result in commingled view content and view navigation.

◦Distributed control is more difficult to maintain: changes will need to be made in numerous places.

26

Page 27: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Front Controller / 2Front Controller / 2

27

Web container

incoming request

return response create

model

render response

return control

delegate rendering of response

handle request

Page 28: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Session FaçadeSession FaçadeEnterprise beans encapsulate business logic and business data and expose their interfaces, and thus the complexity of the distributed services, to the client tier.• Tight coupling, which leads to direct

dependence between clients and business objects; • Too many method invocations between

client and server, leading to network performance problems; • Lack of a uniform client access strategy,

exposing business objects to misuse.

28

Page 29: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Session Façade / 2Session Façade / 2Client: the object which needs access to the business service. This client can be another session bean (Session Facade) in the same business tier or a business delegate in another tier.SessionFacade: a session bean which manages the relationships between numerous BusinessObjects and provides a higher level abstraction to the client.BusinessObject: a role object that facilitates applying different strategies, such as session beans, entity beans and a DAO. A BusinessObject provides data and/or some services.

29

Page 30: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

OUTRODUCTIONOUTRODUCTION

30

Page 31: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

RevisionRevision• Simple patterns• Singleton• Template Method• Factory Method• Adapter• Proxy• Iterator• Complex patterns• Abstract Factory• Strategy• Mediator• Façade• J2EE patterns• Data Access Objects• Model-View-Controller• Session Façade• Front Controller

31

Page 32: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

Questions?Questions?

32

Page 33: Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable

SourcesSourceshttp://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612

http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)

http://java.sun.com/blueprints/patterns/catalog.html◦ http://java.sun.com/blueprints/patterns/DAO.html

◦ http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

◦ http://java.sun.com/blueprints/patterns/MVC.html

◦ http://java.sun.com/blueprints/patterns/SessionFacade.html

◦ http://java.sun.com/blueprints/corej2eepatterns/Patterns/SessionFacade.html

◦ http://java.sun.com/blueprints/patterns/FrontController.html

◦ http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html

33