Design Patterns - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Design Patterns

Description:

{ sone = Singleton.getInstance(); stwo = Singleton.getInstance(); public void testUnique() { assertTrue('Should be equal', sone == stwo); Template Design Pattern ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 18
Provided by: usersC1
Category:
Tags: design | patterns | sone

less

Transcript and Presenter's Notes

Title: Design Patterns


1
Design Patterns
  • Section 7.1 (JIAs)
  • Section 7.2.1 (till page 259) (JIAs)
  • Section 7.2.2(JIAs)
  • Section 10.4.1 (JIAs)

2
Design Patterns
  • Project phase 6
  • Assignment 7
  • Speaker --- December 8
  • Pattern is a term used to describe architectural
    designs
  • Christopher Alexander et al.
  • came up with problems which occur over and over
    again in architectural designs
  • describe solutions for those problems
  • A pattern is a generic or reusable solution to a
    recurring problem
  • Can that be applied to software?

3
Design Patterns
  • Similarities between software and architecture
  • Both are based on creative processes
  • Design must satisfy customers needs
  • Design my be feasible to engineer
  • Designers must balance many competing constraints
    and requirements
  • As a result, software design patterns are
    schematic descriptions of solutions to recurring
    problems in software design
  • Code reuse in OO?

4
Design Patterns
  • Why use design patterns?
  • Capture and document the experience acquired in
    software design
  • Support reuse in design and boost confidence in
    software systems
  • those that use established design patterns that
    have been proven effective
  • Provide a common vocabulary for software
    designers to communicate about software design
  • Work started by Gamma et al.
  • Design Patterns Gamma et al., 1995

5
Design Patterns
  • 23 general-purpose design patterns Gamma et al.
  • application domain independent
  • Classified into
  • Creational patterns
  • Deal with the process of object creation
  • E.g. Singleton --- One instance of class can
    exist
  • Behavioral patterns
  • Deal with dynamic interaction among classes and
    objects
  • E.g. Template --- Separate variant and invariant
    behaviors among related classes
  • Structural patterns
  • Deal with the static composition and structure of
    classes and objects
  • E.g. Adapter --- Convert an interface of a class
    to a different one expected by other classes

6
Design Patterns
  • Style for describing patterns
  • Pattern name
  • Category
  • Intent
  • Problem addresses
  • Also known as
  • Applicability
  • Situations where pattern can be applied
  • Structure
  • Class diagram that depicts the participants of
    the pattern and the relationships among them
  • Participants
  • A list of classes participating in the pattern

7
Singleton Design Pattern
  • Pattern name Singleton
  • Category Creational design pattern
  • Intent Ensure that a class has only one instance
    and provide a global point of access to it
  • A singleton is an object that cannot be
    instantiated
  • Also known as N/A
  • Applicability Use the Singleton pattern when
    there must be exactly one instance of a class and
    it must be accessible to clients from a
    well-known access point
  • a GUI application must have a single mouse
  • an active modem needs one and only one telephone
    line
  • an operating system can only have one window
    manager
  • or, a PC is connected to a single keyboard
  • Such objects are accessed by disparate objects
    throughout a software system, and therefore
    require a global point of access
  • Structure NEXT SLIDE
  • Participants Itself

8
Singletons Structure
                                                
                                                  
                                 Singleton
class diagram
9
Singleton
  • Declares the unique instance of the class as a
    static variable
  • Defines a static method getInstance() for clients
    to access the unique instance --- global access
    point
  • Code
  • public class Singleton    private static
    Singleton instance null   private
    Singleton()        // no code required //
    Exists only to defeat instantiation?
  •       public static Singleton getInstance()
  • // global access point      if(instance
    null)          instance new
    Singleton()            return instance  

10
Singleton
  • public class SingletonInstantiator
  • public SingletonInstantiator()
  • Singleton instance Singleton.getInstance()
  • Singleton anotherInstance new Singleton() !!!
  • Test if working properly?

11
Testing Singletons
  • import junit.framework.
  • public class SingletonTest extends TestCase
  • private Singleton sone null, stwo null
  • public SingletonTest(String name)
  • super(name)
  • public void setUp()
  • sone Singleton.getInstance()
  • stwo Singleton.getInstance()
  • public void testUnique()
  • assertTrue(Should be equal, sone stwo)

12
Template Design Pattern
  • One of the most widely used design patterns
  • Useful for separating the variant and the
    invariant object behavior
  • Invariant means common to all subclasses
  • The invariant behavior is placed in the abstract
    class (template) and uses abstract methods for
    the variant behavior
  • Any subclasses that inherit it can override the
    abstract methods and implement the specifics
    needed in that context
  • In the body of TemplateMethod() --- invariant ---
    there are calls to operation1() and operation2()
  • operation1() and operation2() are abstract
  • defined by the subclasses which override them

13
Template Design Pattern
  • Why abstract classes for templates?
  • Contain behavior that is common to all its
    subclasses encapsulated in non-abstract methods
  • Might even be defined as final
  • The abstract methods in such a class require that
    context-specific behavior be implemented for each
    concrete subclass
  • Called Hook methods

14
Template Design Pattern
  • A program that sorts data using different sorting
    algorithms
  • The AbstractClass would have a method called
    Sort()
  • Analogous to TemplateMethod() -- the invariant
    behavior
  • When called, would use the abstract methods in
    the class, which are specified by any class that
    inherits from it
  • The hook methods in this case might be
  • compare() (compares two objects and returns the
    one that is "higher")
  • sortPass() (performs one iteration of a
    particular sorting algorithm)
  • The usual control structure of object calls and
    relations is reversed!
  • It is the parent class that calls the method in
    the subclass
  • "Hollywood Principle" -- "Don't call us, we'll
    call you."

15
Template Design Pattern
  • Pattern Name Template Method
  • Category Behavioral design pattern
  • Intent To define the skeleton of an algorithm
    deferring some steps to subclasses, thus allowing
    the subclasses to redefine certain steps of the
    algorithm
  • This way, subclasses can override parts of the
    algorithm without changing its overall structure
  • Applicability The Template Method Pattern should
    be used
  • to implement the invariant parts of an algorithm
    once and leave it to the subclasses to implement
    variant behavior to
  • localize the common behavior among subclasses to
    avoid code duplication
  • A.K.A Don't call us, we'll call you

16
Structure of the Template Design Pattern
17
Template Design Pattern
  • Participants
  • Generic (or Abstract) Class
  • Defines the abstract hook methods (variant
    behavior) that concrete classes override to
    implement the steps of an algorithm
  • Implements a template method (invariant behavior)
    that defines the skeleton of an algorithm by
    called the hook methods
  • Concrete Class
  • Implements the hook methods to carry-out
    subclass-specific steps of the algorithm defined
    in the template method
  • Example 7.3 page 267
  • Code in csci230bin/Plotter folder
Write a Comment
User Comments (0)
About PowerShow.com