Top Banner

of 25

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Observer or Publisher-Subscriber

Observer or Publisher-SubscriberContextA component uses data or information provided by another componentProblemChanging the internal state of a component may introduce inconsistencies between cooperating components.To restore inconsistency, we need a mechanism for exchanging data or state information between such components.Forces associated with problemComponents must be loosely coupledComponents that depends upon information provider are not known priori.

SolutionImplement change-propagation mechanism between info provider (subject) & components that depend on it (observer).Observers ca dynamically register or unregister with this mechanism .Whenever the subject changes its state, it starts the change-propagation mechanism to restore consistencies with all registered observers.Changes are propagates via special update function common to all observers.To implement change propagation- passing data and state info from subject to observer use pull-model.IdiomsIdioms deal with implementation of particular design issues. Specific to programming lang.Ex: C++ uses reference-counting idioms to manages dynamically allocated resources where as Smalltalk used garbage collection mechanism.

Subject

applicationData

propagatechangeattach(observer)detach(observer)ObserverUpdateservicePropagates changesCounted BodyManage logical sharing and proper resource deallocation of objects that use dynamically allocated resources.

Context: The interface of a class is separated from its implementation.A handle class presents the class interface to the user.The other class embodies the implementation and its called body.The handle forwards member function invocation to the body.ProblemAssignment in C++ is defined recursively as member by-member assignment with copying as the termination of the recursion.Copying of bodies is expensive.Copying can be avoided by using pointers and references, but these leave the problem of who is responsible for cleaning up the object.Sharing bodies on assignment is usually semantically incorrect if the shared body is modified through one of the handles.SolutionA reference count is added to the body class to facilitate memory management.Memory management is added to the handle class, particularly to its implementation of initialization, assignment, copying, and destruction.It is incumbent on any operation that modifies the state of the body to break the sharing of the body by making its own copy. It must decrement the reference count of the original body.

SolutionReference count is added to the body class to facilitate memory management.Handle class implements initialization, assignment, copying and destruction.It is the responsibility of any operation that modifies the state of body to break the sharing of the body by making its own copy, decrementing the reference count of the original body.Relationship between PatternsEx: Refinement of MVC patternMVC separates core functionalities from view.Applying pattern introduces new problems,Some times views and controllers depend on model state.Whenever state of model is changed the dependent views & controllers must be updated.But, change of UI must still given to user.Here Observer pattern helps, Model is subject and views & controllers are observers.

Patterns and its variantsEx: Document-View variant of MVC patternExample is interactive text editorIn this, its harder to separate controller functionality from view.Suppose, select a text with mouse and change it from regular to bold.Text selection is controller action that does not change to model.Its harder to separate view from controllerTransparent peer-peer inter process communication Developing a distributed application with high performance peer-to-peer inter-process communication.Forces to be balancedInter-process communication must be efficient. (searching the location of remote server is undesirable.)Independence from particular inter-process communication mechanism is desirable. (change without affecting clients or server) Client should not aware or dependent on name and location of their servers. (behave as if they were in same process.)

SolutionThis problem is solved by combining two patternsForward-Receiver Pattern (Solves first two forces) Proxy server Pattern (Solves third one)

Forwarder-receiver patternForwarder-receiver pattern offers a general interface for sending and receiving messages and data across process boundaries.Hides mechanism of concrete inter-process communication.Pattern provider name-to-address mapping server

Proxy serverClient is communicating with the representative of server that is located in same process.Proxy server knows the name of server & other details and it forwards the request to it.

Three kinds of relationshipsRefinement: implementation of patternVariant: helps selecting right patter in given design situationCombination: helps composing complex design structure.Command ProcessorSeparates the request for a service from its execution.Ex: Text editor providing undo option for multiple changes.

ContextApplication that needs flexible and extensible user interface, or application that provides service related to the execution of the user function, such as scheduling or undo.ProblemNeed to implement services that go beyond the core functionality of system for execution of user requests.Ex: undo, redoForces shape the solutionDifferent user work with an application in different wayEnhancement of application should not break existing codeAdditional services such as undo implemented consistently for all requests.SolutionEncapsulate requests into objectsWhenever a user calls a specific function of the application, the request is turned into a command object.Whenever a user calls a specific function of the application, the request is turned into a command object.The command processor schedules the execution of commands, may store them for later undo.Each command object delegates the execution of its task to supplier components within the functional core of the application.StructureThe abstract command component defines the interface of all command objects. (As a minimum this interface consists of a procedure to execute a command. )The additional services implemented by the command processor require further interface procedures for all command objects.Command ProcessorFor each user function derive a command component from the abstract command.Command component implements the interface of the abstract command by using zero or more supplier components.Ex: Save state of associated supplier components prior to execution, and restore it in case of undo.Ex: The delete command is responsible for storing the text deleted and its position in the document.Controller represents the interface of application. It accepts requests, such as paste text, and creates the corresponding command objects.Command objects are then delivered to the command processor for execution.Command Processor manages command objects, schedules them and starts their execution.It also implements additional services related to the execution of commands.Supplier components provide most of the functionality required to execute concrete commands.Ex: when an undo mechanism is required, supplier provides means to save and restore its internal stated. ClassCollaboratorsController- Command Processor - CommandResponsibilitiesAccepts service requestsTranslates requests into commandsTransfers commands to command processorClassCollaboratorsCommand Processor - Abstract CommandResponsibilitiesActivates command execution.Maintains command objectsProvides additional services related to command command executionClass CollaboratorsSupplierResponsibilityProvide application specific functionality