[ Pobierz caÅ‚ość w formacie PDF ] ." Behavior should be decomposed to the various member objects." The structure of the aggregate objects is not known ahead of time." Polymorphic behavior is expected but not enforced.StructureFamilyHead targetTrusted RedirectionFamilyHeadoperation()targetBackgroundRedirectoroperation() target.operation();Redirector operationParticipantsFamilyHeadDefines interface and contains a method to be possibly overridden.RedirectorUses interface ofFamilyHeadand redirects internal behavior backto an instance ofFamilyHeadto gain polymorphic behavior overan amorphous object structure.targetThe object that is being requested to perform the redirected work.From the Library of Santiago Itzcoatl Salinas Reyna213Chapter 5: EDP CatalogoperationIndicates the intended task to be completed in both the calling andcalled objects.CollaborationsRedirectorrelies on the classFamilyHeadfor an interface and aninstance of the same for an object-recursive implementation.The Redirec-tion relationship is a critical part of this pattern because it drives the con-cept of do the same as I was asked to do. The trusted aspect comes infrom redirecting the work to a supertype of the current object for polymor-phic handling.ConsequencesRedirectoris reliant onFamilyHeadfor its interface, but it falls to theentirety of the class hierarchy to provide the various implementations.Forthis reason, other classes in the hierarchy may exhibit unexpected behavior.If unexpected behavior is occurring, consider instead using the DeputizedRedirection pattern to restrict the possibilities to a manageable set.ImplementationIn C++:class FamilyHead {2 public:virtual void operation();4 };6 class Redirector : public FamilyHead {public:8 void operation();FamilyHead* target;10 };12 voidRedirector::operation() {14 // Optional work before.target->operation();16 // Optional work after.}Related PatternsTrusted Redirection is a specialization of Redirection such that the possibletargets of the redirected task are from a trusted cluster of classes defined asmutual subclasses of a the target type.As such, relaxing that trust lets yougo back to that simpler, more general EDP.Further restricting that trust toFrom the Library of Santiago Itzcoatl Salinas Reyna214Chapter 5: EDP Cataloga subset of the sibling subclasses of the superclass results in Deputized Redi-rection.If the exact same type of target object is required with no variability,then look to Redirected Recursion for how to work with object type similar-ity set to the same type.Retaining the subtyping relationship but coalescingthe calling and target objects into one object gives rise to Extend Method.Simply breaking the method similarity moves us to Trusted Delegation.Method Call ClassificationObject: Similar Object Type: Subtype Method: SimilarFrom the Library of Santiago Itzcoatl Salinas ReynaObjectSimilaritySimilarTrustedRedirectionDissimilarTypeSimilarityMethodSimilarityFrom the Library of Santiago Itzcoatl Salinas ReynaChapter 5: EDP Catalog215SiblingSubtypeissimilarDSimilarSimilarDissimilar216Chapter 5: EDP CatalogDeputized Delegation Object BehavioralIntentWhen we need the trusted delegation behavior of Trusted Delegation butfind it too generalized, it is necessary to preselect a subtree of the classhierarchy for a restricted polymorphism.MotivationStatic typing is a way to preselect types from a well-defined pool and formmore concrete notions of an object s type before the invocation of a sys-tem at runtime.This preselection is a way of adding type safety we knowprecisely what capabilities an object will have when used during execution.Polymorphism, on the other hand, is a technique for abstracting out typinginformation until runtime.We purposefully obfuscate many details of theunderlying type of the object, providing flexibility at the cost of type safety.Sometimes we need a balance of the two.Consider the driving example from Trusted Delegation.A commondata-input control is missing, one to enter text.The way things are set up,aTextInputclass that subclassed fromInputControlwould be ableto alter the value of aBarGraphshowing numeric values, and this maynot be what the designer wants.7 The design from Trusted Delegation canbe fine-tuned by adding the classes and changes shown in Listing 5.31.Listing 5.31 UI widgets demonstrating Deputized Delegation in C++.1 class UIWidget {public:3 virtual void updateValue( int newValue );};5class InputControl : UIWidget{7 UIWidget* target;public:9 void userHasSetNewValue(int myNewValue) {target->updateValue(myNewValue);11 }7.A good argument can be made that having a text input widget control a numeric display does makerational design sense, assuming a numeric input control that was restricted to numerics only, but thereare UI toolkits that went the other way.From the Library of Santiago Itzcoatl Salinas Reyna217Chapter 5: EDP Catalog};13class SliderBar : InputControl {15 public:// Moves the slider bar accordingly17 void updateValue( int newValue );void acceptUserClick() {19 // Determine new value, set itint newVal;21 // Update the slider bar graphicthis->userHasSetNewValue(newVal);23 };};25class RotaryKnob : InputControl {27 public:// Rotates the knob image accordingly29 void updateValue( int newValue );void acceptUserClick() {31 // Determine new value, set itint newVal;33 // Update the knob imagethis->userHasSetNewValue(newVal);35 };};37class GraphicsContext {39 public:virtual void render(int);41 };43 class DisplayWidget : public UIWidget {protected:45 // Each subclass should set the// GraphicsContext to something47 // meaningful for its needsGraphicsContext* gc;49 public:void updateValue( int newValue ) {51 gc->render( newValue );};53 };55 class TextWidget : public DisplayWidget {};57 class BarGraph : public DisplayWidget {};59 // New class to support Deputized Delegationclass TextInputControl : public InputControl {61 TextWidget* textTarget;public:63 void userHasSetNewValue(int myNewValue) {textTarget->updateValue(myNewValue);65 // Æ--- Deputized Delegation};67 };Now theTextInputControlis available for altering instances oftheTextFieldclass, but it will not be able to alter instances ofBar-Graphor other nontext display items.SliderBarandRotaryKnobcould similarly be limited to a class hierarchy based on aNumericWid-get, for example.This may be easier to see in Figure 5.5
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|