[ Pobierz całość w formacie PDF ]
.In all cases, this requires an instance of CreateObject to be applied as well.ApplicabilityUse Retrieve New when:" A remote object provides an object that is required for localcomputation and is provided by an exposed field object or returnvalue from an exposed method." The object returned needs to be a fresh copy without otherreferences to it.Structuretarget Sink SourceRetrieve NewSinkHolder SourceAccessRetrievesink Retrieved sourceSinkSourceSource srcRetrieved targetRetrieved selected()f()Backgroundcreate new obj;target = src.selected();return copy-of-objCreationSite objectCreatedCreateObjectTypeToCreateselected RetrievedFrom the Library of Santiago Itzcoatl Salinas Reyna 237Chapter 6: Intermediate Pattern CompositionsPINbox-only version (identical to Retrieve Shared):target Sink SourceRetrieve NewSinkHolder SourceAccessRetrievesink source RetrievedBackgroundCreationSite objectCreatedCreateObjectTypeToCreateselected RetrievedParticipantsSourceThe type of the object tasked with handing off the retrieved object.Itserves as the initial source, creating the object.SinkThe object (or class) type that requests the retrieved object andincludes the item,target, to be given a new value.RetrievedThe type of the value to be updated and the value that is returned.targetThe field that is updated to the retrieved object for local use.selectedThe method or field that produces and returns the new value.In thisscenario, the returned object is guaranteed to not have any other ref-erences to it.CollaborationsThere are only three objects in this collaboration, and they simply play theparts of the request originator, the request fulfiller, and the passed object.From the Library of Santiago Itzcoatl Salinas Reyna 238Chapter 6: Intermediate Pattern CompositionsThe critical portions of Retrieve New occur within the implementation ofthe retrieval methodselected.This method is tasked with ensuring thatthe object being returned is newly formed and has no other referencesto it.ConsequencesSeparation between object ownership and object creation has some advan-tages.For example, the type of the created object can be determined in aflexible manner, possibly using polymorphism, such as in an Abstract Fac-tory.It means, however, that the recipient object is solely responsible forthe lifetime of the created object.If references to the created object arehanded out, a good reference management policy must be in place unlessgarbage collection is allowed.ImplementationIn C++:class Retrieved {};2class Source {4 public:Retrieved giveMeAValue() {6 Retrieved ret;return ret;8 };};10class Sink {12 Retrieved target;Source srcobj;14 public:void operation() {16 target = srcobj.giveMeAValue();;}18 };In Python:1 class Source:def giveMeAValue(self):3 ret = Retrieved()return ret5class Sink:7 def __init__(self):srcobj = Source()9 return selfFrom the Library of Santiago Itzcoatl Salinas Reyna 239Chapter 6: Intermediate Pattern Compositions11 def operation(self):target = srcobj.giveMeAValue()Related PatternsRetrieved New is closely related to Retrieve Shared, and if you look at thethe PINbox-only diagrams, they are equivalent.The distinctive portion is abehavior-level aspect because of how the requested object is returned to thecaller, either with clean ownership characteristics, as here, or with possiblereferences being retained, as in Retrieve Shared.Many of the creational patterns from the original Design Patterns [21]use Retrieve New in some manner.Prototype uses Retrieve New explicitly toensure that fresh copies of an object are always returned, and Builder pro-vides an excellent example of Retrieve New s ability to encapsulate com-plex creation logic.Abstract Factory and Factory Method also end up usingRetrieve New in many cases.From the Library of Santiago Itzcoatl Salinas Reyna 240Chapter 6: Intermediate Pattern CompositionsRetrieve Shared Object ManagementIntentTo obtain a reference to a shared object without holding explicit ownershipof that object.Ubiquitous in object-oriented programming.MotivationObjects are conceptual entities, and any particular instance may encapsu-late state or provide functionality that may be of interest to many otherobjects simultaneously.Consider a printer queue, for example.Many appli-cations want to have access to the queue, but there is no reason foreach application to have its own queue.In fact, this would certainly leadto eventual resource collisions as differing queues competed.Instead, aqueue can be shared among many applications simultaneously.Requestsfrom disparate objects can be handled in the order best suited by thequeue.The queue is a shared resource, and must be available for whoeverneeds it.Retrieve Shared uses both Create Object and Retrieve, as does RetrieveNew, but differs in that it is free to cache or otherwise hand off referencesto the newly created object for other purposes.Not only is there no guar-antee that the newly created object has no other references, it is explicitlyassumed that it does have other references to it.Ownership is ill-definedamong those entities holding references.Retrieve Shared is so common that many languages support it directlyon some level.C++, for example, has thesharedptrconstruct, whichis included in Section 20.7.2.2 of the recently adopted 2011 C++ stan-dard [18].Other languages and environments have idioms and assumed librarysupport for quasi-manual management of referenced objects, such asObjective-C sautoreleasefeature.Many others use garbage collec-tion to automatically detect and remove  dead objects from the systemduring runtime.Java, C#, Python, and most scripting languages are exam-ples of this last case.Automated memory management is becoming morepopular and common among programming languages, but it is still farFrom the Library of Santiago Itzcoatl Salinas Reyna 241Chapter 6: Intermediate Pattern Compositionsfrom universal.All developers should have the basics of manual memorymanagement under their belt.ApplicabilityUse Retrieve Shared when:" A remote object provides a value object that is required for localcomputation and is either: provided by a method call s return value or provided by an exposed field object." The object should be shared with other objects and not considered aprivate resource.Structuretarget Sink SourceRetrieve SharedSinkHolder SourceAccessRetrievesink Retrieved sourceSinkSourceSource srcRetrieved targetRetrieved selected()f()Backgroundcreate new obj;target = src [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wpserwis.htw.pl