Scenario

[Previous] [Next]

An adapter class abstracts classes, objects, or functions. Adapters are either transparent or opaque, depending on whether the class, object, or function (the adaptee) is accessible to the client.

With transparent adapters, the adaptee remains accessible to the client. Transparent adapters are mostly used for the following:

  • To enhance the functionality of a class or object by attaching various adapter classes
  • To attach to different implementations of an adaptee that is derived from an abstract interface

Opaque adapters encapsulate the access to the adaptee by making that interaction private to the adapter. The following sections show two of many possible scenarios for using an Adapter design pattern.

Scenario A: Opaque Object Adapter

You're building a Visual Basic toolkit class library, in which you would like to define a sorted map class (SMap) that derives from an abstract map interface (Map). (A map is a class that maps keys to items, or values.) The key is of type String, and the item could be of any type (Variant). Instead of building your map classes totally from scratch, you decide to delegate most of the work to the existing Visual Basic Collection class. You embed a private object reference to an instance of a Collection in SMap—SMap is the adapter class that abstracts the Collection object (the adaptee). Because this relationship is unknown to the clients of SMap, and because it involves abstracting an object instance, this is an Opaque Object Adapter design pattern. (See Figure 4-1.)

click to view at full size.

Figure 4-1. The SMap collection class implemented as an Opaque Object Adapter.

Scenario B: Transparent Class Adapter

You would like to define a sorted map class similar to the one described above. However, instead of delegating the core tasks to an object instance, you decide to inherit the SMap class from the user-defined collection class, CCollection. Using this approach, you will have to be aware of certain constraints of the Visual Basic language. Your intent is to inherit the implementation of CCollection, but with classes in Visual Basic, only interface inheritance is possible, not implementation inheritance. Because implementation inheritance is not possible, an opaque adapter makes no sense—therefore, all class adapters are transparent. Hence, your intent should be to create a transparent SMap adapter where a client can reference it from both its Map interface and its CCollection interface. Figure 4-2 shows that, in order to give the illusion of implementation inheritance in Visual Basic, a CCollection object reference is maintained in SMap. All invocations through the CCollection interface of SMap are passed on one-for-one to the CCollection object instance. Invocations through the Map interface of SMap are processed by the CCollection object instance to the extent deemed necessary by you, the programmer of the SMap adapter. Let me reiterate the fact that although the CCollection object instance might be private to SMap, this is still a Transparent Class Adapter design pattern by definition, because SMap publicly inherited the CCollection interface.

click to view at full size.

Figure 4-2. The SMap collection class implemented as a Transparent Class Adapter.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net