csc 480 software engineering design with patterns
Post on 14-Dec-2015
220 Views
Preview:
TRANSCRIPT
CSC 480Software 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
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)
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
Design Pattern Space
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
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.
Structure
Client
state
ConcreteCommand
execute()
<<interface>>Command
execute()
Example
• The java.awt.Component class
Name in Command Pattern Actual Name
Command Component
ConcreteCommand MyTankPanel
execute() paintComponent()
state myTank, road, shell
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
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
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.
Structure
Subject
ConcreteObserver
notify()
<<interface>>Observer
notify()
attach()
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()
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
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.
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.
Structure
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)
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
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.
Structure
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
Composite Design Pattern
top related