角色、抽象模式、松耦合

发布于 2024-10-18 07:48:49 字数 3148 浏览 1 评论 0原文

假设我们有以下内容:

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

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

我读到在这种情况下使用基于角色的架构更合适。您能否提供一个示例(公司、员工实体)?


谢谢!

Let's imagine we got the following:

A) Factory interface such as

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

B) Concrete factory such as

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) Employees family: Manager and Sales inherited from an abstract Employee.

More on my simple) architecture

Some client code

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

I wanted to ask about some business logic architecture details.
Using the architecture I can't design what the Company class should be, don't know how do I integrate it.

I have read that it more appropriate to use Role-based architecture in such cases. Could you provide an example (Company, Employees entities)?


Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-10-25 07:48:49

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

Are you sure you included the override keyword in the ToString() method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文