Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming/5/c/solution

The decorator pattern participants are:
  • Component: CableTV declares the common cost and packages queries, so clients can use both basic and decorated accounts through one object interface.
  • Concrete component: BasicPlan supplies the initial price and package description.
  • Decorator: the abstract Packages class stores a component reference and forwards its operations, enabling recursive object composition.
  • Concrete decorators: Movies and HDChannels override the queries to add one responsibility while preserving the wrapped component's behavior.
  • Client: an application chooses the nesting of components and observes the resulting account through CableTV.
Each wrapper contributes its price exactly once along the chain. The same structural arrangement handles a new optional package without changing the existing component interface.

New to topics? Read the docs here!