角色,抽象图案,松散耦合

时间:2011-02-20 23:47:59

标签: c# interface factory-pattern

让我们假设我们得到以下内容:

A)工厂界面,如

public interface IEmployeeFactory
{        
    IEmployee CreateEmployee(Person person, Constants.EmployeeType type, DateTime hiredate);
}

B)混凝土工厂,如

public sealed class EmployeeFactory : Interfaces.IEmployeeFactory
{        
    public Interfaces.IEmployee CreateEmployee(Person person, Constants.EmployeeType type, DateTime hiredate)
    {
        switch(type)
        {
        case BusinessObjects.Common.Constants.EmployeeType.MANAGER:
            {
                return new Concrete.Manager(person, hiredate);
            }
        case BusinessObjects.Common.Constants.EmployeeType.SALES:
            {
                return new Concrete.Sales(person, hiredate);
            }
        default:
            {
                throw new ArgumentException("Invalid employee type");
            }
        }
    }
}

C)员工家庭:ManagerSales继承自抽象员工。

更多关于我的简单)架构

一些客户端代码

public sealed class EmployeeFactoryClient
{
    private Interfaces.IEmployeeFactory factory;
    private IDictionary<String, Interfaces.IEmployee> employees;

    public Interfaces.IEmployee this[String key]
    {
        get { return this.employees[key]; }
        set { this.employees[key] = value; }
    }
    public EmployeeFactoryClient(Interfaces.IEmployeeFactory factory)
    {
        this.factory   = factory;
        this.employees = new Dictionary<String, Interfaces.IEmployee>();
    }
    public void AddEmployee(Person person, Constants.EmployeeType type, DateTime hiredate, String key)
    {            
        this.employees.Add(
            new KeyValuePair<String, Interfaces.IEmployee>(
                key,
                this.factory.CreateEmployee(
                    person,
                    type,
                    hiredate
                )
            )
        );            
    }
    public void RemoveEmployee(String key)
    {
        this.employees.Remove(key);
    }
    public void ListAll()
    {
        foreach(var item in this.employees)
        {
            Console.WriteLine(
                "Employee ID: " + item.Key.ToString() + 
                "; Details: "   + item.Value.ToString()
            );
        }
    }
}
class ApplicationShell
{
    public static void Main()
    {
        var client = new EmployeeFactoryClient(new EmployeeFactory());
        client.AddEmployee(
            new Person {
                FirstName = "Walter",
                LastName  = "Bishop",
                Gender    = BusinessObjects.Common.Constants.SexType.MALE,
                Birthday  = new DateTime()
            },
            BusinessObjects.Common.Constants.EmployeeType.MANAGER,
            new DateTime(),
            "A0001M"
        );
        Console.WriteLine(client["A0001M"].ToString());
        Console.ReadLine();
    }
}

我想询问一些业务逻辑架构的细节。 使用该架构我无法设计公司类应该是什么,不知道如何集成它。

我已经读过在这种情况下使用基于角色的架构更合适。你能提供一个例子(公司,员工实体)吗?


谢谢!

1 个答案:

答案 0 :(得分:3)

您确定在ToString()方法中包含了 override 关键字吗?