Receptionist Pattern

Tram Ho

The Receptionist design pattern solves the general problem of redirecting an event that occurs in an application’s execution context to another execution context for processing. It is a hybrid model. Although it does not appear in the Gang of Four book, it incorporates elements of the Command, Memo, and Proxy design patterns described in that book. It is also a variation of the Trampoline model (nor appears in the book). In this form, an event is initially received by a trampoline object, which is immediately returned or redirected to a target object for processing.

The Receptionist Design Pattern in Practice

A KVO message calls observeValueForKeyPath: ofObject: change: context: method implemented by an observer. If the change to the property occurs on a secondary thread, observeValueForKeyPath: ofObject: change: context: code executes on the same thread. There is a central object in this form, the receptionist, that acts as a threaded mediator. As shown in the illustration, a receptionist object is specified as the observer property property model property. The receptionist implements observeValueForKeyPath: ofObject: change: context: to redirect notifications received on a sub thread to another execution context in the main activity queue, in this case. When the property changes, the receptionist receives a KVO notification. The receptionist immediately adds a block operation to the main activity queue, the block containing the code specified by the client that updates the user interface appropriately.

Bouncing updates KVO into the main activity queue

You define a receptionist class so that it has the necessary elements to add itself as an observer of an attribute and then convert a KVO message into an update task. Therefore, it must know the object it is observing, the properties of the object it is observing, what update task to perform and the queue to execute it. The code below shows the original declaration of the RCRecellectist class and its instance variables.

Declaring the receptionist class

The RCTaskBlock object variable is a block object of the following type of declaration

These parameters are similar to the parameters of the observeValueForKeyPath: ofObject: change: context method, Next the parameter class declares a single factory class method in which an RCTaskBlock object is a parameter:

Factory class method to create a receptionist object

Note that the code copies the block object instead of retaining it. Because the block can be created on the stack, it must be copied to the heap so it exists in memory when the KVO message is sent.

Finally, the parameter class implements the observeValueForKeyPath: ofObject: change: context:

This code simply lists the task in the given activity queue, passes the task block to the observed object, the main path for the changed attribute and the dictionary containing the new value. The task is encapsulated in an NSBlockOperation object that executes the task on the queue.

The client object provides the block code that updates the user interface when it creates a receptionist object, Note that when it creates the receptionist object, the client switches into the active queue that the block will be executed, in the field. This merge is the main manipulation queue.

Creating a receptionist object

When to use the Receptionist Pattern

You can apply the Front Desk design template whenever you need to move your work to another execution context for processing. When you observe the message or deploy the block handler or respond to the action message and you want to ensure that your code executes in the appropriate execution context, you can implement the Receptionist form to redirect the public. The work must be done for the execution of that context. With the Receptionist template, you can even perform some filters or combine incoming data before you exit a task to process the data. For example, you can collect data into batches and then, within that period of time, send those batches elsewhere for processing.

A common situation in which a helpful Receptionist model is key-value observing. In key-value observing, changes to the value of a descriptive attribute model object are communicated to the observer through KVO messages. However, model object changes may occur on a background thread. This leads to thread mismatches, because changes to the model object model state often result in updates to the user interface and these must occur on the main thread. In this case, you want to redirect KVO messages to the main stream. where updates to the app user interface may occur.

Share the news now

Source : Viblo