当类具有实现接口的属性时,如何在UML中建模

时间:2017-07-06 22:08:23

标签: c# uml

我有一个界面(MyController)。另外两个类实现了该接口(ControllerTypeAControllerTypeB)。另一个类(MyFinal)的字段为MyController,因此它可以包含ControllerTypeAControllerTypeB。如何在UML中对MyControllerControllerTypeAControllerTypeBMyFinal之间的关系进行建模?这是一个有效的C#程序来展示我的意思:

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

namespace ScratchApp
{
    public interface MyController
    {
        void method1(String str);
        void method2(int num);
    }

    public class ControllerTypeA : MyController
    {
        public void method1(String str) 
        {
            Console.WriteLine("This is controller type A and the string is: " + str);
        }

        public void method2(int num)
        {
            Console.WriteLine("This is controller type A and the number is: " + num);
        }
    }

    public class ControllerTypeB : MyController
    {
        public void method1(String str)
        {
            Console.WriteLine("This is controller type B and the string is: " + str);
        }

        public void method2(int num)
        {
            Console.WriteLine("This is controller type B and the number is: " + num);
        }
    }

    public class MyFinal
    {
        public MyController myController;

        public MyFinal(MyController mc)
        {
            myController = mc;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            MyFinal mf1 = new MyFinal(new ControllerTypeA());
            MyFinal mf2 = new MyFinal(new ControllerTypeB());

            mf1.myController.method1("From mf1");
            mf1.myController.method2(1);
            mf2.myController.method1("From mf2");
            mf2.myController.method2(2);
            Console.ReadKey();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您的属性属于MyController类型,因此您在此处拥有关联。两个ControllerType都意识到这一点MyController。从图形上看,它看起来像这样:

enter image description here

作为旁注:属性myController在图表上出现两次。 Mea culpa。它应该只显示一次:作为关联结束时的角色名称或作为MyFinal内的属性(然后没有角色名称)。这种方式并没有错。但作为一种风格问题,角色名称应优先于隔离专区中的属性。这种方式更明显,它是一个杰出的类型属性。