PLoPD 3 - Part 1

August 15, 2013 | Miscellaneous

This post is part of the notes I kept while reading the Pattern Languages of Program Design 3 book.

The notes I kept in this post come from the General Purpose Design Patterns section of the book.

Null Object, by Bobby Woolf

p. 1

“avoid cluttering your code with conditionals that check for a null value”

“implements an interface with entirely null operations”

“is another example of how polymorphism can simplify and regularize you code”

p. 5

In the Model-View-Controller, a view uses its controller to gather input from the user. This is a Strategy since the controller is the view's strategy for how it will gather input [Gamma+95, p. 315].

Product Trader, by Dirk Bäumer and Dirk Riehle

p. 29

“create objects by naming an interface and by providing a specification”

p. 31

Applicability

DO NOT user a Product Trader as a replacement for Factory Methods or direct object creation.

See also http://libra.msra.cn/Publication/12728810/product-trader.

Sponsor-Selector, by Eugene Wallingford

p. 2

The Strategy pattern [Gamma+95] lets you switch algorithms easily, even at runtime. But it says little about the ramifications of switching or the criteria on which to base the switch. That's where the Sponsor-Selector comes in.

p. 67

“a mechanism for selecting the best resource for a task from a set of resources that change dynamically”

p. 69

Solution

Each resource has a sponsor to recommend when the resource can be used. The selector takes these recommendations as input and decides which resource should be used.

The selector broadcasts the request to all of the sponsors. Each sponsor evaluates the applicability of its recourse and sends a rating back. The selector then uses these ratings, along with context information, to select the best resource in the current context.

The key to the Sponsor-Selector pattern is the separation of three fundamentally different responsibilities: recommending a resource, selecting among resources, and using a resource.

p. 70

Structure

A resource is any one of a set of objects that has specific functionality in some larger context.

Each sponsor contains knowledge of when its resource is appropriate for use. If possible, a sponsor should make no reference to any resource other than its own.

The selector embodies knowledge of which resource to prefer in particular kinds of situations. The selector sends requests to the set of sponsors and receives a set of ratings in return.

p. 72

Implementation

Sponsors may be implemented using the Proxy pattern [Buschmann+96]. The selector may be implemented using the Broker pattern [Buschmann+96]. If the resources themselves are methods to be invoked, they can be implemented using the Strategy pattern [Gamma+95].

See also http://www.cs.uni.edu/~wallingf/patterns/sponsor-selector.html.