SMA Repository Contents

Synopsis:

Repository code links are presented here, with annotations about their purpose and function.
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