UML帮助C#设计原则

Pin*_*ong 5 c# oop uml design-principles

任何人都可以请指出下图的含义:

在此输入图像描述

  1. PolicyLayer和PolicyServiceInterface之间的关系是什么
  2. PolicyServiceInterface和MachanismLayer之间有什么关系.

C#代码也将不胜感激!

请注意,UML来自C#中的敏捷原则,模式和实践.作者:Martin C. Robert,Martin Micah 2006.

添加于2011年6月15日

请执行以下相同的含义:1)一端带三角形的实线2)一端带三角形的虚线

添加于2011年6月3日1日

有什么区别:1)一端带箭头的实线2)一端带箭头的虚线

以下链接中的示例和PersistentObject和ThirdPartyPersistentSet中的示例:

UML帮助C#设计原则

添加于2011年6月3日2日

PolicyLayer和PolicyServiceInterface之间的关系如下:

public class PolicyLayer

{
    private PolicyServiceInterface policyServiceInterface = new PolicyServiceInterfaceImplementation();
}

class PolicyServiceInterfaceImplementation:PolicyServiceInterface {}
Run Code Online (Sandbox Code Playgroud)

关于

SwD*_*n81 7

1)PolicyLayer和PolicyServiceInterface之间的关系是什么

----->是Association("知道一个")

协会http://www.sedris.org/drawings/uml_8.gif

C#代码:

public interface PolicyServiceInterface { }

public class PolicyLayer
{
    private IPolicyServiceInterface _policyServiceInterface;
    // Constructor Assocation
    public PolicyLayer(IPolicyServiceInterface policyServiceInterface)
    {
        _policyServiceInterface = policyServiceInterface;
    }
}
Run Code Online (Sandbox Code Playgroud)

2)PolicyServiceInterface和MachanismLayer之间的关系是什么.

- - - |>是Realization("implements")

实现

C#代码:

public interface PolicyServiceInterface { }

public class MachanismLayer : PolicyServiceInterface 
Run Code Online (Sandbox Code Playgroud)

3)以下具有相同的含义:1)一端带有三角形的实线2)一端带三角形的虚线?

不,他们有不同的含义:

----- |>是Generalization("继承")

概括

C#代码:

public class PolicyServiceInterface { } // could also be abstract

public class MachanismLayer : PolicyServiceInterface 
Run Code Online (Sandbox Code Playgroud)

有什么区别:1)一端带箭头的实线2)一端带箭头的虚线

- - - >是Dependency("使用")有各种形式的依赖,包括局部变量,参数值,静态函数调用或返回值.

依赖

C#代码:

// Here Foo is dependent on Baz
// That is Foo - - -> Baz
public class Foo {
   public int DoSomething() { // A form of local variable dependency
       Baz x = new Baz();
       return x.GetInt();
   } 
}
Run Code Online (Sandbox Code Playgroud)

请参阅我在这里的答案,了解组合和聚合.


Mas*_*uso 2

PolicyLayer使用Policy服务接口(可能它持有一个引用) MachanismLayer实现PolicyServiceInterface

 public interface IPolicyServiceInterface
    {
        void DoSomething();
    }

    public class MachanismLayer : IPolicyServiceInterface
    {
        public void DoSomething()
        {
            Console.WriteLine("MachanismLayer Do Something");
        }
    }

    public class PolicyLayer
    {
        private IPolicyServiceInterface _policyServiceInterface;
        public PolicyLayer(IPolicyServiceInterface policyServiceInterface)
        {
            _policyServiceInterface = policyServiceInterface;
        }

        public void DoSomethig()
        {
            _policyServiceInterface.DoSomething();
        }
    }

    public class Program
    {
        public static void Main(string[] agrs)
        {
           MachanismLayer machanismLayer=new MachanismLayer();
           PolicyLayer policyLayer = new PolicyLayer(machanismLayer);
           policyLayer.DoSomethig();
        }
    }
Run Code Online (Sandbox Code Playgroud)