我有一个界面(MyController
)。另外两个类实现了该接口(ControllerTypeA
和ControllerTypeB
)。另一个类(MyFinal
)的字段为MyController
,因此它可以包含ControllerTypeA
或ControllerTypeB
。如何在UML中对MyController
,ControllerTypeA
,ControllerTypeB
和MyFinal
之间的关系进行建模?这是一个有效的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();
}
}
}