Summary

This chapter explored techniques that may be used to structure object-oriented programs. These techniques may be regarded as idioms or guidelines augmenting the mechanisms provided by object-oriented languages.

Composition by inheritance

1


slide: Section 7.1: Composition by inheritance

In section 1, we looked at the problems that may occur when employing virtual functions and explicit scoping, in particular in the presence of recursion, how to maintain invariance when using inheritance, and we discussed the diamond structure for the realization of compound objects.

Interaction through delegation

2

  • hiding -- functional behavior, interface
  • smart pointers -- object management
  • abstraction -- interface, realization
  • roles -- dynamic switching

slide: Section 7.2: Interaction through delegation

In section 2, an example was given demonstrating implicit delegation in C++. Also, we looked at the notion of smart pointers. Delegation underlies the envelope/letter idiom which allows for separating the interface of a data type from its realization. We also discussed an example of dynamic role switching, employing explicit delegation.

Indirect cooperation

3

  • gadgets -- require, damage and draw
  • events -- interception, callbacks - MV(C) -- model, view, control

slide: Section 7.3: Indirect cooperation

In section 3, we looked at some patterns of cooperation between objects. The examples were inspired by solutions employed in the Interviews user interface development library. Also, a schematic implementation of the Smalltalk MV(C) paradigm was given.

Event-driven control

4

  • abstract system -- functional behavior
  • user interface -- displayer, prompter
  • events -- to define interactions
  • dependencies -- interconnections between events

slide: Section 7.4: Event-driven control

Finally, in section 4, we looked at an example demonstrating event-driven control. Events provide a powerful abstraction to define the interaction of objects in response to a user action.

Questions

  1. Discuss the interplay between scoping and recursion for virtual functions.
  2. How would you express the invariant behavior of an employee class? Explain what problems may occur when adding a bonus to the salary of a derived manager class.
  3. Explain how compound objects may be constructed by employing the diamond structure.
  4. Explain how the functionality of a class may be made accessible through an arbitrary interface class by employing implicit delegation.
  5. What are smart pointers? Why are they useful? How would you implement them?
  6. Sketch how you would implement dynamic role switching in C++.
  7. What is a gadget? Give an example showing the interaction between a gadget and a window.
  8. Characterize the notion of event in a window environment. Characterize also the interaction between events and the gadget to which the event is directed.
  9. Sketch how the Smalltalk MV(C) paradigm may be implemented in C++. Give a simple example of its use.
  10. Explain how events may be used to regulate the effects of user actions. Why are events needed?

Further reading

An invaluable source with respect to idioms and advanced usage of C++ is  [Coplien92]. Further, you may consult  [KP88] for examples of applying the MVC paradigm, and  [Henderson93] for examples of employing events.  [Henderson93] also proposes a canonical mapping from a formal specification language to an implementation in C++.