Patterns - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Patterns

Description:

http://www.patterndepot.com/put/8/JavaPatterns.htm (a free download, and the ... There are some esoteric traps to avoid if the collection is simultaneously being ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 29
Provided by: harry66
Category:

less

Transcript and Presenter's Notes

Title: Patterns


1
Patterns
  • COM379
  • University of Sunderland
  • James Malone

2
Resources
  • Gamma, et al., 1995, Design Patterns,
    Addison-Wesley.
  • http//www.patterndepot.com/put/8/JavaPatterns.htm
    (a free download, and the code is also
    available!)

3
Introduction to Patterns
  • The recurring aspects of designs are called
    design patterns.
  • A pattern is the outline of a reusable solution
    to a general problem encountered in a particular
    context
  • Many of them have been systematically documented
    for all software developers to use
  • A good pattern should
  • Be as general as possible
  • Contain a solution that has been proven to
    effectively solve the problem in the indicated
    context.
  • Studying patterns is an effective way to learn
    from the experience of others

4
Pattern description
  • Context
  • The general situation in which the pattern
    applies
  • Problem
  • A short sentence or two raising the main
    difficulty.
  • Forces
  • The issues or concerns to consider when solving
    the problem
  • Solution
  • The recommended way to solve the problem in the
    given context.
  • to balance the forces
  • Antipatterns (Optional)
  • Solutions that are inferior or do not work in
    this context.
  • Related patterns (Optional)
  • Patterns that are similar to this pattern.
  • References
  • Who developed or inspired the pattern.

5
Creational Patterns
  • Factory Method
  • Singleton

6
Factory Method
  • You need an object, but which subtype of the
    object needs to be decided later. This can be a
    particularly problem during initialization, as it
    means the file/object loader has to know every
    possible subclass.
  • The solution to this is called the factory
    method or object factory.
  • Positives
  • Flexibility
  • Can connect parallel class hierarchies
  • Negatives
  • Forces subclassing of the base class

7
Example Factory Method
  • A database class wants an Employ object but does
    not need or want details of all possible
    subclasses of Employ

Employ
getClass
Factory
Database
Engineer
Director
8
Singleton (Creation or Non-Creation Pattern?)
  • Ensures a class has a globally accessible unique
    instance.
  • The use of a public constructor cannot guarantee
    that no more than one instance will be created.
  • The singleton instance must also be accessible to
    all classes that require it
  • Sometimes you need a class with only one
    instance, and sometimes you need global access to
    a class.
  • Positives
  • Avoids the need to link objects at system
    initialization.
  • Sometimes you need it badly.
  • Supports persistence. The local copy can be a
    proxy for a copy kept on disk.
  • Reduced name space pollution.
  • Can be modified to support multiple instances
    using a Map.
  • Negatives
  • Has to be carefully implemented.
  • Sometimes its just not useful.

9
Singleton
  • Solution

Singleton
theInstance
getInstance
getInstance method
Company
if (theCompanynull)
theCompany
theCompany new Company()

Company private
return theCompany
getInstance
10
Structural Patterns
  • Adapter
  • Flyweight
  • Proxy
  • MVC (discussed earlier)

11
Adapter (or Wrapper)
  • This pattern is basically how glue code is
    written to convert one interface to another,
    allowing unrelated classes to work together.
  • Two approaches
  • Inheritance (class adapter)the adapter inherits
    from the nonconforming one and adds the methods
    we need to make the new derived class match the
    desired interface.
  • Object composition by reference to implementation
    (object adapter)the adapter contains an instance
    of the class providing the function. The
    reference can be to a common superclass, allowing
    the adapter to adapt multiple subclasses (like a
    wrapper around the old class).
  • Positives
  • Already used in Java
  • Negatives
  • A class adapter creates a concrete subclass and
    cannot handle subclasses of the original class.
  • An object adapter makes it hard to override the
    object subclass behavior.

12
The Adapter Pattern
  • Context
  • You are building an inheritance hierarchy and
    want to incorporate it into an existing class.
  • The reused class is also often already part of
    its own inheritance hierarchy.
  • Problem
  • How to obtain the power of polymorphism when
    reusing a class whose methods
  • have the same function
  • but not the same signature
  • as the other methods in the hierarchy?
  • Forces
  • You do not have access to multiple inheritance or
    you do not want to use it.

13
Adapter
  • Solution

14
Adapter
  • Example

Client
Shape
area
Square
Circle
ImportedCircle
calcArea
15
Flyweight
  • Use sharing to support large numbers of
    fine-grained objects efficiently. E.g., the
    characters and other images on a screen need not
    individually contain all the data needed to draw
    them. Similarly, in C, where there are no
    primitive types, the overhead to generate
    individual numbers as objects, can be
    overwhelming.
  • Approach
  • Create sharable instances of a class that are
    allocated when needed. Either create a basic pool
    at initialization or as needed. Note that this
    approach can be used for complex objects as well.
  • Positives
  • Avoids overhead.
  • Saves storage.
  • Useful with databases.
  • Often combined with the composite pattern.
  • Negatives
  • Hard to do in Java.

16
Flyweight Example
  • Suppose we want to draw a small folder icon with
    a name under it for
  • each person in a an organization. If this is a
    large organization, there could be a large number
    of such icons, but they are actually all the same
    graphical
  • image. Even if we have two icons, one for is
    Selected and one for not
  • Selected the number of different icons is
    small. In such a system, having an
  • icon object for each person, with its own
    coordinates, name and selected state is a waste
    of resources.
  • Instead, well create a FolderFactory that
    returns either the selected or
  • the unselected folder drawing class, but does
    not create additional instances
  • once one of each has been created. Since this is
    such a simple case, we just
  • create them both at the outset and then return
    one or the other

17
Proxy
  • Allows you to represent a complex object with a
    simpler or more accessible one. May support copy
    on write for large objects that may or may not
    change. There are a number of useful
    applications.
  • Approach
  • Define a proxy class with the same method
    signatures as the complex class. Those methods
    provide the proxy access to the complex class
    object.
  • Positives
  • Provides remote access, virtual access, protected
    access, or smart access.
  • Negatives
  • Non-trivial to implement.
  • Overhead and delayed access.

18
The Proxy Pattern
  • Context
  • Often, it is time-consuming and complicated to
    create instances of a class (heavyweight
    classes).
  • There is a time delay and a complex mechanism
    involved in creating the object in memory
  • Problem
  • How to reduce the need to create instances of a
    heavyweight class?
  • Forces
  • We want all the objects in a domain model to be
    available for programs to use when they execute a
    systems various responsibilities.
  • It is also important for many objects to persist
    from run to run of the same program

19
Proxy
  • Solution

interface
ClassIF







HeavyWeight
Proxy
Client
20
Proxy
  • Example

21
Model-View-Controller(MVC)
  • In the MVC pattern, the model and theview are
    strictly kept apart from each other
  • Their interaction is mediated by thecontroller
    component the glue that bindsthem together
  • MVC consists of three kinds of objects
  • Model the application object
  • View UI (screen presentation)
  • Controller defines the way the UI reacts to
    user inputs

22
MVC Class Diagram
controller
view
model
23
Behavioral Patterns
  • Command
  • Iterator
  • Observer
  • Template Method

24
Command
  • Encapsulate a request as an object. Supports
    queueing, logging, and undo operations. The Java
    1.1 event model implements this.
  • Approach
  • Encapsulate the request as an object.
  • Positives
  • Hides the private details of the request.
  • Commands are first class objects.
  • Allows priority queueing.
  • Supports a macro or scripting language approach.
  • Negatives
  • Overhead
  • Lots of little classes

25
Iterator
  • An iterator is an object that supports the
    traversal of a collection. The interface between
    an algorithm and a collection should be an
    iterator. you have seen this already in banking
    example
  • Approach
  • See the Iterator interface. Consider implementing
    by using inner classes.
  • Positives
  • Raises the level of abstraction without a major
    performance penalty.
  • Negatives
  • There are some esoteric traps to avoid if the
    collection is simultaneously being modified.
  • Protected access
  • Typecasting is necessary in Java

26
Observer
  • Supports separation between model and view.
  • Approach see Swing you will learn about
    observable classes
  • Positives
  • Decouples observers from models
  • Negatives
  • Lack of coordination between observers. Who has
    responsibility?
  • What sort of messages should be sent? Adapter
    classes may be required.

27
Observer
  • Solution

28
Template Method
  • Define the skeleton of an algorithm, but defer
    details to subclasses. Fundamental to Java.
  • In other words, if your baseclass is an abstract
    class you are using the template pattern
  • Approach
  • Abstract classes provide this capability.
  • Positives
  • Normal part of OO programming.
  • Negatives
  • Not as powerful as in C.
Write a Comment
User Comments (0)
About PowerShow.com