T B H P N

C# Compound Objects

Synopsis:

A compound object is an instance of a type that inherits from, and/or aggregates, instances of other types. When a class aggregates an instance of another class, it is responsible for constructing the aggregated instance by a call to the new operator.
When a class inherites from a base, it inherits the base's methods, except for constructors and destructor. The derived class may choose to override virtual methods of the base, and must define any abstract methods of the base. It is appropriate to use inherited non-virtual methods of the base as appropriate.
Compound Object Class Diagram
Here is a demonstration class that inherits from an abstract base class, and an interface. The abstract base class aggregates an instance of another class.

Code in File CompoundObjects.cs

  /////////////////////////////////////////////////////////////////////
  // CompoundObjects.cs - demonstrate compound object relationships  //
  //                                                                 //
  // Jim Fawcett, CSE681 - Software Modeling and Analysis, Fall 2017 //
  /////////////////////////////////////////////////////////////////////

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  namespace CompoundCSharpObjects
  {
    ///////////////////////////////////////////////////////////////////
    // ISpeaker interface

    interface ISpeaker
    {
      void say();
      string name { get; set; }
    }
    ///////////////////////////////////////////////////////////////////
    // Aggregated class
    //   - owned by AbstractBase

    public class Aggregated : ISpeaker
    {
      public Aggregated()
      {
        Console.Write("\n  construction of Aggregated instance");
      }
      ~Aggregated()
      {
        Console.Write("\n  Aggregated instance dying");
      }
      public string name { get; set; } = "aggregated instance";
      public void say()
      {
        Console.Write("\n  Hi, I'm {0}, of type {1}", name, this.GetType().Name);
      }
    }
    ///////////////////////////////////////////////////////////////////
    // AbstractBase
    //   - base class for CompoundObject

    public abstract class AbstractBase : ISpeaker
    {
      public AbstractBase()
      {
        Console.Write("\n  construction of AbstractBase instance");
      }
      ~AbstractBase()
      {
        Console.Write("\n  AbstractBase instance dying");
      }
      public string name { get; set; } = "abstract base instance";
      void speak()
      {
        Console.Write("\n  Hi there, ");
      }
      protected virtual void phrase()
      {
        Console.Write("\n  AbstractBase phrase");
      }
      public void say()
      {
        speak();
        Console.Write("I'm {0}, of type {1}", name, typeof(AbstractBase).Name);
        ((AbstractBase)this).phrase();  // this dispatches to the derived class method
        agg.say();
      }
      private Aggregated agg = new Aggregated();
    }
    ///////////////////////////////////////////////////////////////////
    // CompoundObject class
    //   - implements AbstractBase and ISpeaker

    public class CompoundObject : AbstractBase
    {
      public CompoundObject()
      {
        Console.Write("\n  construction of CompoundObject instance");
      }
      ~CompoundObject()
      {
        Console.Write("\n  CompoundObject instance dying");
      }
      public string name { get; set; } = "compound object instance";
      void speak()
      {
        Console.Write("\n  Well, hello there, ");
      }
      protected override void phrase()
      {
        Console.Write("\n  CompoundObject phrase");
      }
      public void say()
      {
        speak();
        Console.Write("I'm {0}, of type {1}", name, this.GetType().Name);
        phrase();
        base.say();
      }
    }
    ///////////////////////////////////////////////////////////////////
    // TestCompoundObjects class

    class TestCompoundObjects
    {
      static void Main(string[] args)
      {
        Console.Write("\n  Demonstrate Compound Object Relationships");
        Console.Write("\n ===========================================");

        CompoundObject co = new CompoundObject();
        co.say();
        Console.Write("\n\n");
      }
    }
  }

Output

  Demonstrate Compound Object Relationships
 ===========================================
  construction of Aggregated instance
  construction of AbstractBase instance
  construction of CompoundObject instance
  Well, hello there, I'm compound object instance, of type CompoundObject
  CompoundObject phrase
  Hi there, I'm abstract base instance, of type AbstractBase
  CompoundObject phrase
  Hi, I'm aggregated instance, of type Aggregated


  Aggregated instance dying
  CompoundObject instance dying
  AbstractBase instance dyingPress any key to continue . . .

Example Code:

CSharpBasicDemos Code Folder - includes CompoundObjects
C# inheritance

    CST strip