Proxy Observer Symbolic Links Rebecca Chernoff Proxy Pattern
Proxy, Observer, Symbolic Links Rebecca Chernoff
Proxy Pattern l Intent – l Provide a surrogate or placeholder for another object to control access to it Other Names – Surrogate
Proxy: Motivations & Applicability l l l l Remote Proxy Local representative of object in a different address space. Defer cost of creation and initialization. Virtual Proxy via access control. Protection Proxy Reference counting Load a. Reference persistent object into memory when Smart first referenced Object locking
Proxy: Participants l Real. Subject – l Defines the real object that the proxy represents. Subject – – Defines the common interface for Real. Subject and Proxy. Allows for a Proxy to be used wherever a Real. Subject is expected.
Proxy: Participants l Proxy – – Maintains a reference to the real subject. May refer to a Subject if the Real. Subject 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.
Proxy: Participants l Additional Responsibilities of Proxy – Remote Proxy l – Virtual Proxy l – Encode and send the request to the real subject in a different address space. Cache additional information in order to postpone accessing the real subject. Protection Proxy l Check access permissions.
Proxy: Structure
Proxy: Consequences l Introduces a level of indirection – Remote Proxy l – Virtual Proxy l – Perform optimizations such as creation on demand. Protection Proxy and Smart References l l Hides fact that object resides in a different address space. Allow additional housekeeping tasks when object is accessed. Copy-On-Write
Proxy: Related Patterns l Adapter – l 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.
Observer Pattern l Intent – l 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
Observer: Motivation l Maintain consistency between related objects. – l Application Data vs. Presentation Avoid tightly coupled classes that reduce reusability.
Observer: Applicability l l l 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.
Observer: Participants l Observer – l 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.
Observer: Participants l Concrete. Observer – – – l Implements the Observer interface to keep its state consistent with the subject’s. Maintains a reference to the Concrete. Subject Stores state that should stay consistent with the subject’s. Concrete. Subject – – – Implements the Subject interface to update Observers. Stores state of interest to Concrete. Observer objects. Sends a notification to its observers when its state changes.
Observer: Structure
Observer: Collaborations
Observer: Consequences l l 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.
Observer: Implementation l Mapping subjects to their observers. – l Observing more than one subject. – l Observer needs to know which Subject is notifying. Who triggers the update? – – l Tradeoff between time and space. 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.
Observer: Implementation l Making sure the Subject state is self-consistent before notification. – l Avoiding observer-specific update protocols: the push and pull models. – – l Use Template. Method pattern with Notify as last operation. 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.
Observer: Implementation l Encapsulating complex update semantics. – Change-Manager l l 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.
Observer l Related Patterns – Mediator l – The Change. Manager encapsulates complex update semantics, thus acting as a mediator between the Subject and its Observers. Singleton l Change. Manager may be unique and globally accessible.
But Where Do Surrogates Fit into This? l l 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.
But Where Do Surrogates Fit into This? l 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.
But Where Do Surrogates Fit into This? l Proxy Pattern – – – Subject => Node Proxy => Link Real Subject => ?
But Where Do Surrogates Fit Into This?
- Slides: 25