Title: ECE 355: Software Engineering
1ECE 355 Software Engineering
- CHAPTER 8
- Instructor
- Kostas Kontogiannis
2Course outline
- Unit 1 Software Engineering Basics
- Unit 2 Process Models and Software Life Cycles
- Unit 3 Software Requirements
- Unit 4 Unified Modeling Language (UML)
- Unit 5 Design Basics and Software Architecture
- Unit 6 OO Analysis and Design
- ? Unit 7 Design Patterns
- Unit 8 Testing and Reliability
- Unit 9 Software Engineering Management and
Economics
3References
- The material for this lecture was obtained from
- http//www.dofactory.com/Patterns/Patterns.aspxli
st - http//www.developer.com/design/article.php/150269
1
4Overview
- Some more patterns
- Adapter
- Bridge
- State
- Factory Method
- Command
- Proxy
- Chain of Responsibility
- Observations and Conclusions
- Further reading
5Adapter Design Pattern
- The Adapter is intended to provide a way for a
client to use an object whose interface is
different from the one expected by the client,
without having to modify either - This pattern is suitable for solving issues that
arise, for example, when - 1) you want to replace one class with another and
the interfaces do not match, and - 2) you want to create a class that can interact
with other classes without knowing their
interfaces at design time
6Adapter Design Pattern - Operation
- First, you create a custom Target class that
defines methods using an interface as expected by
the client - Second, you create a custom Adapter that
implements the interface expected by the Adaptee.
The Adapter class subclasses the Target, and
provides an alternate implementation of the
client requests in terms that the Adaptee
expects. Adapter overrides the Target method and
provides the correct interface for Adaptee. - This approach has the advantage that it does not
lead to modifications in the client. Adapter is a
structural pattern and you can use it to react to
changes in class interfaces as your system
evolves, or you can proactively use the Adapter
pattern to build systems that anticipate changing
structural details
7Adapter Design Pattern
8Adapter Design Pattern
- Participants The classes and/or objects
participating in this pattern are - Target Â
- defines the domain-specific interface that Client
uses. - Adapter Â
- adapts the interface Adaptee to the Target
interface. - Adaptee Â
- defines an existing interface that needs
adapting. - Client Â
- collaborates with objects conforming to the
Target interface.
9Adapter Design Pattern
   // "Adaptee"   class Adaptee      public
void SpecificRequest()Â Â Â Â Â Â Â Â Â Â Console.WriteLi
ne("Called SpecificRequest()")Â Â Â Â Â Â //
Client   class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â // Create adapter and place a
request       Target target new
Adapter()Â Â Â Â Â Â target.Request()Â Â Â Â Â Â // Wait
for user       Console.Read()     Â
-   // "Target"   class Target      public
virtual void Request()Â Â Â Â Â Â Â Â Â Â Console.WriteLi
ne("Called Target Request()")      - class Adapter Target      private
Adaptee adaptee new Adaptee()Â Â Â Â public
override void Request()Â Â Â Â Â Â Â Â Â Â // Possibly
do some other work       // and then call
SpecificRequest       adaptee.SpecificRequest()
       Â
Output Called SpecificRequest()
10Bridge Design Pattern - Operation
- Decouple an abstraction from its implementation
so that the two can vary independently - The Bridge pattern is useful when there is a
hierarchy of abstractions and a corresponding
hierarchy of implementations. Rather than
combining the abstractions and implementations
into many distinct classes, the Bridge pattern
implements the abstractions and implementations
as independent classes that can be combined
dynamically. Related patterns are - Layered Architecture The Bridge design pattern
is a way of organizing the entities identified
using the Layered Architecture pattern into
classes. Â - Abstract Factory The Abstract Factory pattern
can be used by the Bridge pattern to decide which
implementation class to instantiate for an
abstraction object
11Bridge Design Pattern
- Abstraction Â
- defines the abstraction's interface.
- maintains a reference to an object of type
Implementor. - RefinedAbstraction  Â
- extends the interface defined by Abstraction.
- Implementor Â
- defines the interface for implementation classes.
This interface doesn't have to correspond exactly
to Abstraction's interface in fact the two
interfaces can be quite different. Typically the
Implementation interface provides only primitive
operations, and Abstraction defines higher-level
operations based on these primitives. - ConcreteImplementor Â
- implements the Implementor interface and defines
its concrete implementation.
12Bridge Design Pattern
13Bridge Design Pattern
// "Implementor" Â Â abstract class
Implementor      public abstract void
Operation()Â Â Â Â // "RefinedAbstraction"
  class RefinedAbstraction Abstraction    Â
 public override void Operation()          impl
ementor.Operation()Â Â Â Â Â Â Â Â
-   // "Abstraction"   class Abstraction      p
rotected Implementor implementor    //
Property -     public Implementor Implementor     Â
    set implementor value         public
virtual void Operation()Â Â Â Â Â Â Â Â Â Â implementor.O
peration()Â Â Â Â Â Â Â Â
14Bridge Design Pattern
// "ConcreteImplementorA" Â Â class
ConcreteImplementorA Implementor      public
override void Operation()Â Â Â Â Â Â Â Â Â Â Console.Writ
eLine("ConcreteImplementorA Operation")Â Â Â Â Â Â
  // "ConcreteImplementorB"   class
ConcreteImplementorB Implementor      public
override void Operation()Â Â Â Â Â Â Â Â Â Â Console.Writ
eLine("ConcreteImplementorB Operation")Â Â Â Â Â Â
15Bridge Design Pattern - Client
- class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â Abstraction ab new
RefinedAbstraction()Â Â Â Â Â Â // Set
implementation and call       ab.Implementor
new ConcreteImplementorA()Â Â Â Â Â Â ab.Operation()
      // Change implemention and call
      ab.Implementor new ConcreteImplementorB(
)Â Â Â Â Â Â ab.Operation()Â Â Â Â Â Â // Wait for user
      Console.Read()     Â
Output ConcreteImplementorA OperationConcreteImp
lementorB Operation
16State Design Pattern - Operation
- Allow an object to alter its behavior when its
internal state changes. The object will appear to
change its class. - The State pattern is useful when you want to have
an object represent the state of an application,
and you want to change the state by changing that
object. - The State pattern is intended to provide a
mechanism to allow an object to alter its
behavior in response to internal state changes.
To the client, it appears as though the object
has changed its class. - The benefit of the State pattern is that
state-specific logic is localized in classes that
represent that state.
17State Design Pattern
- Participants The classes and/or objects
participating in this pattern are - Context
- defines the interface of interest to clients
- maintains an instance of a ConcreteState subclass
that defines the current state. - State
- defines an interface for encapsulating the
behavior associated with a particular state of
the Context. - Concrete State
- each subclass implements a behavior associated
with a state of Context
18State Design Pattern
19State Design Pattern
class Context      private State
state    public Context(State
Astate)Â Â Â Â Â Â Â Â Â Â this.state
Astate        // Property     public State
State          get return state
      set              state value
        Console.WriteLine("State "
state.GetType().Name)Â Â Â Â Â Â Â Â Â Â Â Â Â Â public
void Request()Â Â Â Â Â Â Â Â Â Â state.Handle(this)Â Â Â
  Â
- // "State"   abstract class State Â
- Â Â Â Â public abstract void Handle(Context
context)Â Â Â Â // "ConcreteStateA" Â Â class
ConcreteStateA State      public override
void Handle(Context context)Â Â Â Â Â Â Â Â Â Â context.S
tate new ConcreteStateB()Â Â Â Â Â Â Â Â //
"ConcreteStateB" Â Â class ConcreteStateB
State      public override void Handle(Context
context)Â Â Â Â Â Â Â Â Â Â context.State new
ConcreteStateA()Â Â Â Â Â Â
20State Design Pattern - Client
- static void Main()Â Â Â Â Â Â Â Â Â Â // Setup context
in a state       Context c new Context(new
ConcreteStateA())Â Â Â Â Â Â // Issue requests,
which toggles state       c.Request()      c.Re
quest()Â Â Â Â Â Â c.Request()Â Â Â Â Â Â c.Request()Â Â
    // Wait for user       Console.Read()   Â
 Â
Output State ConcreteStateAState
ConcreteStateBState ConcreteStateAState
ConcreteStateBState ConcreteStateA
21Factory Method - Operation
- When developing classes, you always provide
constructors for your clients' use. There are
certain circumstances in which you do not want
your clients to know which class out of several
to instantiate. - The Factory Method is intended to define an
interface for clients to use to create an object,
but lets subclasses decide which class to
instantiate. Factory Methods let a class defer
instantiation to subclasses.
22Factory Method
- The classes and/or objects participating in this
pattern are - Product
- defines the interface of objects the factory
method creates - ConcreteProduct
- implements the Product interface
- CreatorÂ
- declares the factory method, which returns an
object of type Product. Creator may also define a
default implementation of the factory method that
returns a default ConcreteProduct object. - may call the factory method to create a Product
object. - ConcreteCreatorÂ
- overrides the factory method to return an
instance of a ConcreteProduct.
23Factory Method
24Factory Method
// "ConcreteCreator" Â Â class ConcreteCreatorA
Creator      public override Product
FactoryMethod()Â Â Â Â Â Â Â Â Â Â return new
ConcreteProductA()Â Â Â Â Â Â Â Â //
"ConcreteCreator" Â Â class ConcreteCreatorB
Creator      public override Product
FactoryMethod()Â Â Â Â Â Â Â Â Â Â return new
ConcreteProductB()Â Â Â Â Â Â
-   abstract class Product      //
"ConcreteProductA" Â Â class ConcreteProductA
Product      // "ConcreteProductB"
  class ConcreteProductB Product      //
"Creator"   abstract class Creator      publ
ic abstract Product FactoryMethod()Â Â
25Factory Method - Client
-   class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â // An array of creators
      Creator creators new
Creator2Â Â Â Â Â Â creators0 new
ConcreteCreatorA()Â Â Â Â Â Â creators1 new
ConcreteCreatorB()Â Â Â Â Â Â // Iterate over
creators and create products - Â Â Â Â Â Â foreach(Creator creator in
creators)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Product product
creator.FactoryMethod()Â Â Â Â Â Â Â Â Console.WriteLi
ne("Created 0", Â Â Â Â Â Â Â Â Â Â product.GetType().Nam
e)            // Wait for user      Â
Console.Read()Â Â Â Â Â Â
Output Created ConcreteProductACreated
ConcreteProductB
26Command Design Pattern - Operation
- The Command pattern is intended to encapsulate a
request as an object. For example, consider a
window application that needs to make requests of
objects responsible for the user interface. - The client responds to input from the user by
creating a command object and the receiver. It
then passes this object to an Invoker object,
which then takes the appropriate action. This
allows the client to make requests without having
to know anything about what action will take
place. - In addition, you can change that action without
affecting the client. Practical uses for Command
are for creating queues and stacks for supporting
"undo" operations, configuration changes, and so
on.
27Command Design Pattern
- Participants The classes and/or objects
participating in this pattern are - Command  (e.g. Command)
- declares an interface for executing an operation
- ConcreteCommand  (e.g. CalculatorCommand)
- defines a binding between a Receiver object and
an action - implements Execute by invoking the corresponding
operation(s) on Receiver - Client  (e.g. CommandApp)
- creates a ConcreteCommand object and sets its
receiver - Invoker  (e.g. User)
- asks the command to carry out the request
- Receiver  (e.g. Calculator)
- knows how to perform the operations associated
with carrying out the request
28Command Design Pattern
29Command Design Pattern
// "Receiver" class Receiver       public
void Action()Â Â Â Â Â Â Â Â Â Â Console.WriteLine("Calle
d Receiver.Action()")Â Â Â Â Â Â Â Â // "Invoker"
  class Invoker       private Command
command    public void SetCommand(Command
command)Â Â Â Â Â Â Â Â Â Â this.command
command        public void
ExecuteCommand()Â Â Â Â Â Â Â Â Â Â command.Execute()Â Â
       Â
- abstract class Command       protected
Receiver receiver    // Constructor
    public Command(Receiver receiver)        Â
 this.receiver receiver        public
abstract void Execute()Â Â Â Â //
"ConcreteCommand" Â Â class ConcreteCommand
Command      // Constructor     public
ConcreteCommand(Receiver receiver)
      base(receiver)               public
override void Execute()Â Â Â Â Â Â Â Â Â Â receiver.Actio
n()Â Â Â Â Â Â Â Â
30Command Design Pattern - Client
-   class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â // Create receiver, command,
and invoker - Â Â Â Â Â Â Receiver receiver new
Receiver()Â Â Â Â Â Â Command command new
ConcreteCommand(receiver)Â Â Â Â Â Â Invoker invoker
new Invoker()Â Â Â Â Â Â // Set and execute
command - Â Â Â Â Â Â invoker.SetCommand(command)Â Â Â Â Â Â in
voker.ExecuteCommand()Â Â Â Â Â Â // Wait for user - Â Â Â Â Â Â Console.Read()Â Â Â Â Â Â
Output Called Receiver.Action()
31Proxy Design Pattern - Operation
- Provide a surrogate or placeholder for another
object to control access to it. - This pattern is useful for situations where
object creation is a time consuming process and
can make an application appear sluggish. A proxy
object can provide the client with feedback while
the object is being created. - Proxy can also be used to provide a more
sophisticated reference to an object. For
example, the proxy object can implement
additional logic on the objects behalf (security,
remote procedure calls, an so forth).
32Proxy Design Pattern
- Participants The classes and/or objects
participating in this pattern are - Proxy   (MathProxy)
- maintains a reference that lets the proxy access
the real subject. Proxy may refer to a Subject if
the RealSubject and Subject interfaces are the
same. - provides an interface identical to Subject's so
that a proxy can be substituted for for the real
subject. - controls access to the real subject and may be
responsible for creating and deleting it. - other responsibilites depend on the kind of
proxy - remote proxies are responsible for encoding a
request and its arguments and for sending the
encoded request to the real subject in a
different address space. - virtual proxies may cache additional information
about the real subject so that they can postpone
accessing it. For example, the ImageProxy from
the Motivation caches the real images's extent. - protection proxies check that the caller has the
access permissions required to perform a request.
- Subject   (IMath)
- defines the common interface for RealSubject and
Proxy so that a Proxy can be used anywhere a
RealSubject is expected. - RealSubject   (Math)
- defines the real object that the proxy represents
33Proxy Design Pattern
34Proxy Design Pattern
  // "Proxy"   class Proxy Subject      Rea
lSubject realSubject    public override void
Request()Â Â Â Â Â Â Â Â Â Â // Use 'lazy
initialization' Â Â Â Â Â Â if (realSubject
null)Â Â Â Â Â Â Â Â Â Â Â Â Â Â realSubject new
RealSubject()Â Â Â Â Â Â Â Â Â Â Â Â realSubject.Request(
)Â Â Â Â Â Â Â Â
- // "Subject" Â Â abstract class Subject
      public abstract void Request()     Â
  // "RealSubject"   class RealSubject
Subject      public override void
Request()Â Â Â Â Â Â Â Â Â Â Console.WriteLine("Called - RealSubject.Request()")Â Â Â Â Â Â
35Proxy Design Pattern - Client
-   class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â // Create proxy and request a
service - Â Â Â Â Â Â Proxy proxy new Proxy()Â Â Â Â Â Â proxy.Req
uest()Â Â Â Â Â Â // Wait for user
      Console.Read()     Â
Output Called RealSubject.Request()
36Chain of Responsibility Design Pattern
- One of the tenets of software engineering is to
keep objects loosely coupled. The Chain of
Responsibility is intended to promote loose
coupling between the sender of a request and its
receiver by giving more than one object an
opportunity to handle the request. - The receiving objects are chained and pass the
request along the chain until one of the objects
handles it. - The result is to avoid coupling the sender of a
request to its receiver by giving more than one
object a chance to handle the request. Chain the
receiving objects and pass the request along the
chain until an object handles it.
37Chain of Responsibility Design Pattern
- Participants The classes and/or objects
participating in this pattern are -
- Handler
- defines an interface for handling the requests
- (optional) implements the successor link
- ConcreteHandler
- handles requests it is responsible for
- can access its successor
- if the ConcreteHandler can handle the request, it
does so otherwise it forwards the request to its
successor - Client
- initiates the request to a ConcreteHandler object
on the chain
38Chain of Responsibility Design Pattern
39Chain of Responsibility Design Pattern
- // "Handler"
- abstract class Handler
-
- protected Handler successor
- public void SetSuccessor(Handler
successor)    this.successor successor -  public abstract void HandleRequest(int
request)Â Â -
// "ConcreteHandler1" Â Â class ConcreteHandler1
Handler      public override void
HandleRequest(int request)Â Â Â Â Â Â Â Â Â Â if
(request gt 0 request lt 10)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Con
sole.WriteLine("0 handled request 1",
          this.GetType().Name,
request)Â Â Â Â Â Â Â Â Â Â Â Â else if (successor !
null)Â Â Â Â Â Â Â Â Â Â Â Â Â Â successor.HandleRequest(requ
est)Â Â Â Â Â Â Â Â Â Â Â Â
40Chain of Responsibility Design Pattern
// "ConcreteHandler3" Â Â class ConcreteHandler3
Handler      public override void
HandleRequest(int request)Â Â Â Â Â Â Â Â Â Â if
(request gt 20 request lt 30)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Co
nsole.WriteLine("0 handled request 1",
          this.GetType().Name,
request)Â Â Â Â Â Â Â Â Â Â Â Â else if (successor !
null)Â Â Â Â Â Â Â Â Â Â Â Â Â Â successor.HandleRequest(requ
est)Â Â Â Â Â Â Â Â Â Â Â Â
- // "ConcreteHandler2" Â Â class ConcreteHandler2
Handler      public override void
HandleRequest(int request)Â Â Â Â Â Â Â Â Â Â if
(request gt 10 request lt 20)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Co
nsole.WriteLine("0 handled request 1",
          this.GetType().Name,
request)Â Â Â Â Â Â Â Â Â Â Â Â else if (successor !
null)Â Â Â Â Â Â Â Â Â Â Â Â Â Â successor.HandleRequest(requ
est)Â Â Â Â Â Â Â Â Â Â Â Â
41Chain of Responsibility Design Pattern
- class MainApp      static void
Main()Â Â Â Â Â Â Â Â Â Â // Setup Chain of
Responsibility       Handler h1 new
ConcreteHandler1()Â Â Â Â Â Â Handler h2 new
ConcreteHandler2()Â Â Â Â Â Â Handler h3 new
ConcreteHandler3()Â Â Â Â Â Â h1.SetSuccessor(h2)Â Â Â
   h2.SetSuccessor(h3)      // Generate and
process request       int requests 2, 5,
14, 22, 18, 3, 27, 20Â Â Â Â Â Â foreach (int
request in requests)Â Â Â Â Â Â Â Â Â Â Â Â Â Â h1.HandleRequ
est(request)Â Â Â Â Â Â Â Â Â Â Â Â // Wait for user
      Console.Read()     Â
Output ConcreteHandler1 handled request
2ConcreteHandler1 handled request
5ConcreteHandler2 handled request
14ConcreteHandler3 handled request
22ConcreteHandler2 handled request
18ConcreteHandler1 handled request
3ConcreteHandler3 handled request
27ConcreteHandler3 handled request 20
42(Design) Pattern References
- The Timeless Way of Building, Alexander Oxford,
1979 ISBN 0-19-502402-8 - A Pattern Language, Alexander Oxford, 1977 ISBN
0-19-501-919-9 - Design Patterns, Gamma, et al. Addison-Wesley,
1995 ISBN 0-201-63361-2 CD version ISBN
0-201-63498-8 - Pattern-Oriented Software Architecture,
Buschmann, et al. Wiley, 1996 ISBN
0-471-95869-7 - Analysis Patterns, Fowler Addison-Wesley, 1996
ISBN 0-201-89542-0 - Smalltalk Best Practice Patterns, Beck Prentice
Hall, 1997 ISBN 0-13-476904-X - The Design Patterns Smalltalk Companion, Alpert,
et al. Addison-Wesley, 1998 ISBN 0-201-18462-1 - AntiPatterns, Brown, et al. Wiley, 1998 ISBN
0-471-19713-0
43More Books
- Pattern Languages of Program Design
(Addison-Wesley) - Vol. 1, Coplien, et al., eds. 1995 ISBN
0-201-60734-4 - Vol. 2, Vlissides, et al., eds. 1996 ISBN
0-201-89527-7 - Vol. 3, Martin, et al., eds. 1998 ISBN
0-201-31011-2 - Vol. 4, Harrison, et al., eds. 2000 ISBN
0-201-43304-4 -
- Concurrent Programming in Java, Lea
Addison-Wesley, 1997 ISBN 0-201-69581-2 - Applying UML and Patterns, Larman 2nd edition,
Prentice Hall, 2002 ISBN 0-13-092569-1 - Pattern Hatching Design Patterns Applied,
Vlissides Addison-Wesley, 1998 ISBN
0-201-43293-5 - The Pattern Almanac, Rising Addison-Wesley,
2000 ISBN 0-201-61567-3
44More Books
- Advanced C, Coplien, Addison-Wesley, 1992
- Effective C 50 Specific Ways to Improve Your
Programs and Design (2nd Edition), Scott Meyers,
Addison-Wesley, (September 1997) - More Effective C 35 New Ways to Improve Your
Programs and Designs, Scott Meyers,
Addison-Wesley (December 1995)
45Early Papers
- Object-Oriented Patterns, P. Coad Comm. of the
ACM, 9/92 - Documenting Frameworks using Patterns, R.
Johnson - OOPSLA '92
- Design Patterns Abstraction and Reuse of
Object-Oriented Design, Gamma, Helm, Johnson,
Vlissides, ECOOP '93. - Columns
- C Report, Dr. Dobbs Sourcebook, JOOP, ROAD
46Conferences
- PLoP Pattern Languages of Programs, Monticello,
Illinois, USA (September) - EuroPLoP, Kloster Irsee, Germany (July)
- ChiliPLoP 2000,
- March 2000, Wickenburg, Arizona, USA
- KoalaPLoP 2000, May 2000, Melbourne, Australia
- See http//hillside.net/patterns/conferences for
up-to-the-minute information.
47Mailing Lists
- patterns_at_cs.uiuc.edu present and refine patterns
- patterns-discussion_at_cs.uiuc.edu general
discussion on patterns - gang-of-4-patterns_at_cs.uiuc.edu discussion on
Design Patterns - siemens-patterns_at_cs.uiuc.edu discussion on
Pattern-Oriented Software Architecture - ui-patterns_at_cs.uiuc.edu discussion on user
interface design patterns - business-patterns_at_cs.uiuc.edu discussion on
patterns for business processes - ipc-patterns_at_cs.uiuc.edu discussion on patterns
for distributed systems - See http//hillside.net/patterns/Lists.html for
an up-to-date list.
48URLs
- General
- http//hillside.net/patterns
- http//www.research.ibm.com/designpatterns
- Conferences
- http//hillside.net/patterns/conferences/
- Books
- http//hillside.net/patterns/books/
- Mailing Lists
- http//hillside.net/patterns/Lists.html
- Portland Patterns Repository
- http//c2.com/ppr/index.html