Proxy, Observer, Symbolic Links Rebecca Chernoff.

25
Proxy, Observer, Symbolic Links Rebecca Chernoff

Transcript of Proxy, Observer, Symbolic Links Rebecca Chernoff.

Page 1: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy, Observer, Symbolic Links

Rebecca Chernoff

Page 2: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy Pattern

Intent– Provide a surrogate or placeholder for another

object to control access to it

Other Names– Surrogate

Page 3: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Motivations & Applicability

Local representative of object in a different address space.

Defer cost of creation and initialization. Protection via access control. Reference counting Load a persistent object into memory when

first referenced Object locking

Remote Proxy

Virtual Proxy Protection Proxy

Smart Reference

Page 4: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Participants

RealSubject– Defines the real object that the proxy represents.

Subject– Defines the common interface for RealSubject

and Proxy.– Allows for a Proxy to be used wherever a

RealSubject is expected.

Page 5: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Participants

Proxy– Maintains a reference to the real subject. May

refer to a Subject if the RealSubject and Subject interfaces are the same.

– Identical interface to Subject so the Proxy can be substituted.

– Controls access to the real subject.– May be responsible for creating and deleting the

real subject.

Page 6: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Participants

Additional Responsibilities of Proxy– Remote Proxy

Encode and send the request to the real subject in a different address space.

– Virtual Proxy Cache additional information in order to postpone

accessing the real subject.

– Protection Proxy Check access permissions.

Page 7: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Structure

Page 8: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Consequences

Introduces a level of indirection– Remote Proxy

Hides fact that object resides in a different address space.

– Virtual Proxy Perform optimizations such as creation on demand.

– Protection Proxy and Smart References Allow additional housekeeping tasks when object is

accessed.

Copy-On-Write

Page 9: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Proxy: Related Patterns

Adapter– Provides a different interface to an object, but

since Proxy can refuse to perform a request, the interface is effectively a subset.

Decorator– Similar implementation to proxy, but different

purpose. Decorator adds responsibilities whereas proxy controls access.

Page 10: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer Pattern

Intent– Define a one-to-many dependency between

objects so that when one object changes state, all its dependents are notified and updated automatically

Also Known As– Dependents– Publish-Subscribe

Page 11: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Motivation

Maintain consistency between related objects.– Application Data vs. Presentation

Avoid tightly coupled classes that reduce reusability.

Page 12: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Applicability

An abstraction has two aspects, one dependent on the other.

A change to one object requires changing others.

An object should be able to notify other objects without making assumptions about the objects.

Page 13: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Participants

Observer– Defines an interface for objects to be notified

when a subject changes.

Subject– Defines an interface for attaching and detaching

Observer objects.– Unknown number of Observer objects.

Page 14: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Participants

ConcreteObserver– Implements the Observer interface to keep its state

consistent with the subject’s.– Maintains a reference to the ConcreteSubject– Stores state that should stay consistent with the subject’s.

ConcreteSubject– Implements the Subject interface to update Observers.– Stores state of interest to ConcreteObserver objects.– Sends a notification to its observers when its state changes.

Page 15: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Structure

Page 16: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Collaborations

Page 17: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Consequences

Vary subjects and observers independently. Abstract coupling between Subject and

Observer. Support for broadcast communication. Simple update to Server may cause a

cascade of updates to Observer and its dependent objects.

Page 18: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Implementation

Mapping subjects to their observers.– Tradeoff between time and space.

Observing more than one subject.– Observer needs to know which Subject is notifying.

Who triggers the update?– Subject: client doesn’t have to remember to update, but

consecutive operations cause multiple updates.– Observer: client can wait to trigger the update, but client must

remember to call update. Dangling references to deleted subjects.

– Notify observers right before deletion. Observers of multiple Subjects can’t be deleted when just one Subject is deleted.

Page 19: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Implementation

Making sure the Subject state is self-consistent before notification.

– Use TemplateMethod pattern with Notify as last operation.

Avoiding observer-specific update protocols: the push and pull models.

– Push Model: Detailed information sent regardless.– Pull Model: Observers ask for details explicitly.

Specifying modifications of interest explicitly.– Observers register for a specific aspect of interest.

Page 20: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer: Implementation

Encapsulating complex update semantics.– Change-Manager

Takes responsibility of maintaining references to observers away from the Subject.

Defines a particular update strategy. Updates all dependent observers at the request of a subject.

Combining the Subject and Observer classes.– When multiple inheritance not supported by language, both

interfaces may reside in one class.

Page 21: Proxy, Observer, Symbolic Links Rebecca Chernoff.

Observer

Related Patterns– Mediator

The ChangeManager encapsulates complex update semantics, thus acting as a mediator between the Subject and its Observers.

– Singleton ChangeManager may be unique and globally

accessible.

Page 22: Proxy, Observer, Symbolic Links Rebecca Chernoff.

But Where Do Surrogates Fit into This?

File System Example Symbolic Links / Aliases / Shortcuts

– Reference to another node in the file system.– Surrogate for a node, not the node itself.– Own access rights.– Edit and save a file.– Add and remove nodes to a directory.

Page 23: Proxy, Observer, Symbolic Links Rebecca Chernoff.

But Where Do Surrogates Fit into This?

How do I find the right design pattern for the task?– Consider how design patterns solve design problems.– Scan the Intent sections for something that sounds right.– Study how patterns interrelate.– Look at patterns whose purpose corresponds to what you’re

trying to do.– Examine a relevant cause of redesign and apply the

patterns that help you avoid it.– Consider what should be variable in your design.

Page 24: Proxy, Observer, Symbolic Links Rebecca Chernoff.

But Where Do Surrogates Fit into This?

Proxy Pattern– Subject => Node– Proxy => Link– Real Subject => ?

Page 25: Proxy, Observer, Symbolic Links Rebecca Chernoff.

But Where Do Surrogates Fit Into This?