MOD251 Modern Software Development Methods - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

MOD251 Modern Software Development Methods

Description:

The Product interface represents the ConcreteProducts which are created by a ... Copy-On-Write [J.Coplien92 - The 'Handle Class' Idiom] ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 35
Provided by: geirj
Category:

less

Transcript and Presenter's Notes

Title: MOD251 Modern Software Development Methods


1
MOD251 Modern Software Development Methods
  • Factory Composite Observer Proxy

2
Content / Agenda
  • Factory Pattern
  • Composite Pattern
  • Observer Pattern
  • Proxy Pattern

3
Factory Pattern
  • A creational pattern
  • Also known as the Factory Method pattern.
  • We have discussed this earlier, so I will just
    give a brief summary.

4
Factory Pattern - Solution
  • The Product interface represents the
    ConcreteProducts which are created by a Factory
    defined by the FactoryIF interface on request
    from the CreationRequestor.

5
Factory Pattern - Consequences
  • The creation requester class is independent of
    the class of concrete product objects actually
    created.
  • The set of product classes that can be
    instantiated may change dynamically.

6
Factory Pattern - Implementation
  • As discussed earlier
  • The factory can choose which type of object to
    instantiate based on method name, parameter (DI),
    config setting or system property.
  • The object can be instantiated dynamically using
    reflection.

7
Factory Pattern Java API Usage
  • The Java Calendar class has a factory method for
    instantiating concrete Calendars. This is a
    variant of the factory pattern where the abstract
    Calendar class represents both the products and
    the factory
  • Calendar rightNow Calendar.getInstance()

8
Composite Pattern
  • A partitioning pattern
  • Also known as the Recursive Composition pattern.

9
Composite Pattern - Synopsis
  • The Composite pattern allows you to build
    complex objects by recursively composing similar
    objects in a treelike manner. The Composite
    pattern also allows the objects in the tree to be
    manipulated in a consistent manner by requiring
    all of the objects in the tree to have a common
    superclass or interface.

10
Composite Pattern - Context
  • An application for drawing shapes. Shapes can be
    simple shapes (circles, squares, etc.) or
    composite shapes (combinations of circles,
    squares, etc.).

11
Composite Pattern - Solution
  • The solution shown for the graphics application
  • All shapes (both simple and composite) are drawn
    using the draw() method.
  • The composite shapes delegates the drawing to the
    shapes of which it is composed.
  • Composite shapes can be nested.

12
Composite Pattern Java API Usage
  • Used in Swing to build GUIs
  • Components like JLabel, JButton,
  • Containers like JFrame, JPanel,

13
Observer Pattern
  • A behavioral pattern
  • Also known as publish-subscribe.
  • Closely related to the more general MVC
    architecture/pattern.

14
Observer Pattern - Synopsis
  • Allows objects to dynamically register
    dependencies between objects, so that an object
    will notify those objects that are dependent on
    it when its state changes.

15
Observer Pattern - Context
  • A GUI application where it is important that the
    GUI is constantly updated, but you dont want
    your business logic to know about you GUI.
  • A multi-client application where clients must be
    notified of important events. E.g. a monitoring
    system.

16
Observer Pattern - Forces
  • You are implementing two otherwise independent
    classes. An instance of one will need to be able
    to notify other objects when its state changes.
    You dont want the classes to have direct
    knowledge of each other.
  • You have a one-to-many dependency that may
    require an object to notify multiple objects when
    its state changes.

17
Observer Pattern - Solution
  • The Observer implements the ObserverIF interface
    and can be notified of events via the notify
    method.
  • The Observable implements the ObservableIF
    interface and can register observers with the
    Multicaster.
  • An alternative design is to let the Observable
    extend the Multicaster directly and drop the
    ObservableIF interface.

18
Observer Pattern - Consequences
  • The Observer pattern allows an object to deliver
    notifications to other objects without the
    objects sending or the objects receiving the
    notifications being aware of each others class.

19
Observer Pattern - Implementation
  • An Observable object will normally pass a
    self-reference as a parameter to an Observer
    objects notify/update method. In most cases, the
    Observer object needs access to the Observable
    object in order to act on the notification.
  • If you implement the pattern by extending the
    multicaster, you may have problems with multiple
    inheritance. This can be solved by delegating and
    wrapping an inherited class.

20
Observer Pattern Java API Usage
  • Javas delegation event model (in Swing
    components) is a specialized form of the Observer
    pattern.
  • The Java API has a class called Observable that
    implements the multicaster and an interface
    called Observer containing the update-method.
    When implementing the Observer pattern in Java
    you usually use (subclass, implement) these.

21
Observer Pattern Code example
A simple example demonstrating how to use the
Java API
22
Proxy Patternby Radu Marinescu
23
Loading "Heavy" Objects
  • Document Editor that can embed multimedia objects
  • MM objects are expensive to create ? opening of
    document slow
  • avoid creating expensive objects
  • they are not all necessary as they are not all
    visible at the same time
  • Creating each expensive object on demand !
  • i.e. when image has to be displayed
  • What should we put instead?
  • hide the fact that we are "lazy"!
  • don't complicate the document editor!

24
Idea Use a Placeholder!
  • create only when needed for drawing
  • keeps information about the dimensions (extent)

25
Basic Aspects
proxy (n. pl prox-ies) The agency for a person
who acts as a substitute for another person,
authority to act for another
  • Intent
  • provide a surrogate or placeholder for another
    object to control access to it
  • Applicability
  • whenever there is a need for a more flexible or
    sophisticated reference to an object than a
    simple pointer
  • remote proxy if real object is far away
  • virtual proxy if real object is expensive
  • protection proxy if real object is vulnerable
  • enhancement proxies (smart pointers)
  • prevent accidental delete of objects (counts
    references)

26
Structure
27
Participants
  • Proxy
  • maintains a reference that lets the proxy access
    the real subject.
  • provides an interface identical to Subject's
  • so that proxy can be substituted for the real
    subject
  • controls access to the real subject
  • may be responsible for creating or deleting it
  • Subject
  • defines the common interface for RealSubject and
    Proxy
  • RealSubject
  • defines the real object that the proxy holds
    place for

28
Collaborations
29
Remote Proxy
  • Hide real details of accessing an object
  • actual object is on a remote machine (remote
    address space)
  • used in RMI and CORBA

30
Further Reasons to Use Proxies
  • Virtual Proxy
  • Creates/accesses expensive objects on demand
  • may wish to delay creating an expensive object
    until it is really accessed
  • It may be too expensive to keep entire state of
    the object in memory at one time
  • Protection Proxy
  • Provides different levels of access to original
    object
  • Cache Proxy (Server Proxy)
  • Multiple local clients can share results from
    expensive operations
  • remote accesses or long computations

31
Consequences
  • Proxies introduce a level of indirection
  • used differently depending on the kind of proxy
  • hide different address space (remote p.)
  • creation on demand (virtual p.)
  • allow additional housekeeping activities
    (protection, smart pointers)
  • Copy-On-Write J.Coplien92 - The "Handle Class"
    Idiom
  • copying large and complicated objects is
    expensive
  • use proxy to pay the price of copying only when
    the new object is modified
  • Subject must be reference counted
  • proxy increments counter on copy
  • when modified, do copy and decrement counter
  • if (counter 0) delete Subject

32
(No Transcript)
33
References
  • Robert Martin Agile Software Development
  • Mark Grand Patterns in Java, Volume 1
  • Radu Marinescu A presentation on Patterns in
    the course Ingineria programarii 2 (Software
    Engineering 2), The "Politehnica" University of
    Timisoara, Romania

34
Next
  • Package design
  • Software testing
Write a Comment
User Comments (0)
About PowerShow.com