【问题标题】:How to model in UML when class has an attribute that realizes an interface当类具有实现接口的属性时如何在 UML 中建模
【发布时间】:2017-07-07 16:43:59
【问题描述】:

我有一个接口 (MyController)。另外两个类实现了该接口(@98​​7654322@ 和ControllerTypeB)。另一个类 (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();
        }
    }

}

【问题讨论】:

    标签: c# uml


    【解决方案1】:

    您的属性是MyController 类型,因此您在这里有一个关联。 ControllerTypes 都意识到了这一点 MyController。从图形上看是这样的:

    附带说明:属性myController 在图表上出现了两次。过失。它应该只显示一次:作为关联末尾的角色名称或作为MyFinal 中的属性(然后不显示角色名称)。这种方式真的没有错。但作为风格问题,角色名称应该优先于隔间中的属性。这样就更明显地表明它是一个可区分的类型化属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多