24
CSC 480 Software Engineering Design With Patterns

CSC 480 Software Engineering Design With Patterns

Embed Size (px)

Citation preview

Page 1: CSC 480 Software Engineering Design With Patterns

CSC 480Software Engineering

Design With Patterns

Page 2: CSC 480 Software Engineering Design With Patterns

The Essence of Patterns

Each pattern describes a problem which occurs over and over again, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.

Christopher Alexander

Page 3: CSC 480 Software Engineering Design With Patterns

What Is a Design Pattern?

• In general, a pattern has four essential elements– The pattern name – abstracts a design problem, its

solutions, and consequences (vocabulary)– The problem – explains the context and describes how to

represent algorithm as objects – The solution – describes the elements that make up the

design, their relationships, responsibilities, and collaborations

– The consequences – the results and trade-offs of applying the pattern (evaluation)

Page 4: CSC 480 Software Engineering Design With Patterns

The Catalog of GoF Patterns

• Design patterns are first classified by their purpose– Creational – the process of object creation– Structural – composition of classes and objects– Behavioral – the ways in which classes and objects interact

and distribute responsibilities• The second criterion is scope– Class – static, fixed at compile-time– Object – more dynamic, changeable at run-time

Page 5: CSC 480 Software Engineering Design With Patterns

Design Pattern Space

Page 6: CSC 480 Software Engineering Design With Patterns

The COMMAND Pattern

The COMMAND pattern defines a method that all subclasses needs to do, while deferring details of the algorithms to each subclass to allow for additional information being provided.

behavioral

object

Page 7: CSC 480 Software Engineering Design With Patterns

Solution • Define a command interface with a method to

execute the command. • Supply methods in the command interface type to

manipulate the state of the command objects. • Each concrete command class implement the

command interface. • To invoke the command, call the execute method.

Page 8: CSC 480 Software Engineering Design With Patterns

Structure

Client

state

ConcreteCommand

execute()

<<interface>>Command

execute()

Page 9: CSC 480 Software Engineering Design With Patterns

Example

• The java.awt.Component class

Name in Command Pattern Actual Name

Command Component

ConcreteCommand MyTankPanel

execute() paintComponent()

state myTank, road, shell

Page 10: CSC 480 Software Engineering Design With Patterns

How to Memorize?

• The Hollywood PrincipleDon’t call us, we’ll call you.

• When we design with the Command pattern, we’re telling subclasses the same thing

• Other patterns (to be discussed shortly) that make use of the Hollywood Principle– Observer– Factory method

Page 11: CSC 480 Software Engineering Design With Patterns

The OBSERVER Pattern

The OBSERVER pattern provides a mechanism to monitor and notify changes in an source object to one or more objects (known as the “observers”) that need to update themselves with the latest state from the source.

behavioral

object

Page 12: CSC 480 Software Engineering Design With Patterns

Solution • Define an observer interface type. All concrete

observer classes must implement this interface type.

• The subject (source) maintains a collection of observer objects.

• The subject class supplies the method(s) for attaching observers.

• Whenever an event (change) occurs, the subject notifies all observers, which in turn update themselves.

Page 13: CSC 480 Software Engineering Design With Patterns

Structure

Subject

ConcreteObserver

notify()

<<interface>>Observer

notify()

attach()

Page 14: CSC 480 Software Engineering Design With Patterns

Example• The java.awt.event.ActionListener

interface (as well as other XxxListener interfaces)

Name in Command Pattern Actual Name

Subject JButton

Observer ActionListener

ConcreteObserver Specific classes implementing ActionListener

attach() addActionListener()

notify() actionPerformed()

Page 15: CSC 480 Software Engineering Design With Patterns

The FACTORY METHOD Pattern

The Factory Method pattern defines an interfacefor creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

creational

class

Page 16: CSC 480 Software Engineering Design With Patterns

Context

• A type (the creator) creates objects of another type (the product).

• Subclasses of the creator type need to create different kinds of product objects.

• Clients do not need to know the exact type of product objects.

Page 17: CSC 480 Software Engineering Design With Patterns

Solution

• Define a creator type that expresses the commonality of all creators.

• Define a product type that expresses the commonality of all products.

• Define a method, called the factory method, in the creator type. The factory method yields a product object.

• Each concrete creator class implements the factory method so that it returns an object of a concrete product class.

Page 18: CSC 480 Software Engineering Design With Patterns

Structure

Page 19: CSC 480 Software Engineering Design With Patterns

Example• The java.util.Collection interface (or

the java.util.Iterable interface as available since Java5)

Name in Command Pattern Actual Name

Creator Collection

Product Iterator

factorMethod() iterator()

ConcreteCreator Any specific Collection class

ConcreteProduct Any specific Iterator class (which is often anonymous)

Page 20: CSC 480 Software Engineering Design With Patterns

The ABSTRACT FACTORY Pattern

Similar to the FACTORY METHOD, the ABSTRACT FACTORY pattern defines methods that construct a family of related products. A concrete factory class is needed for each family of related products.

creational

object

Page 21: CSC 480 Software Engineering Design With Patterns

Solution • Define an AbstractFactory interface type. All

concrete Factory classes must implement this interface type.

• Define two or more product types that expresses the commonality of family of related products.

• Define a create method for each kind of related products in the family in the AbstractFactory type. Each create method yields a product object in the desired type.

• Each concrete factory class implements all set of the factory methods so that it returns an set of related product objects for the specific style.

Page 22: CSC 480 Software Engineering Design With Patterns

Structure

Page 23: CSC 480 Software Engineering Design With Patterns

Example• The java.awt.event.ActionListener interface (as

well as other XxxListener interfaces)

Name in Command Pattern Actual Name

AbstractFactory AbstractTankFactory

AbstractProduct AbstractTank (may be broken down to tank body, canon, and shell, etc)

factorMethod() createTank()

ConcreteFactory EET1TankFactory, MyGameTankFactory

ConcreteProduct EET1Tank, MyGameTank

Page 24: CSC 480 Software Engineering Design With Patterns

Composite Design Pattern