UML help C# Design Principles

2020-07-14 06:27发布

问题:

Could anyone please point out the meaning of the graph below:

  1. What is the relationship between PolicyLayer and PolicyServiceInterface
  2. What is the relationship between PolicyServiceInterface and MachanismLayer.

C# code would be also appreciated!

Please note that the UML is from Agile Principles, Patterns, and Practices in C# By Martin C. Robert, Martin Micah 2006.

Added on 15 2011/6/2

Do the following have the same meaning: 1) A solid line with a triangle at one end 2) A dashed line with a triangle at one end

Added on 2011/6/3 1st

What is the difference between: 1) A solid line with an arrow at one end 2) A dashed line with an arrow at one end

Example in thie sample and PersistentObject and ThirdPartyPersistentSet in the link below:

UML help C# Design Principles

Added on 2011/6/3 2nd

Can the relationship between PolicyLayer and PolicyServiceInterface as below:

public class PolicyLayer

{
    private PolicyServiceInterface policyServiceInterface = new PolicyServiceInterfaceImplementation();
}

class PolicyServiceInterfaceImplementation:PolicyServiceInterface {}

Regarding

回答1:

1) What is the relationship between PolicyLayer and PolicyServiceInterface

The -----> is Association ("knows a")


(source: sedris.org)

C# code:

public interface PolicyServiceInterface { }

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

2) What is the relationship between PolicyServiceInterface and MachanismLayer.

The - - -|> is Realization ("implements")

C# Code:

public interface PolicyServiceInterface { }

public class MachanismLayer : PolicyServiceInterface 

3) Do the following have the same meaning: 1) A solid line with a triangle at one end 2) A dashed line with a triangle at one end?

No they have different meanings:

The -----|> is Generalization ("inherits")

C# Code:

public class PolicyServiceInterface { } // could also be abstract

public class MachanismLayer : PolicyServiceInterface 

What is the difference between: 1) A solid line with an arrow at one end 2) A dashed line with an arrow at one end

The - - -> is Dependency ("uses a") There are various forms of dependency including local variables, parameter values, static function calls or return values.

C# Code:

// 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();
   } 
}

See my answer here for Composition and Aggregation.



回答2:

PolicyLayer uses Policy service interface(probabily it holds a reference) MachanismLayer implements 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();
        }
    }