lex*_*eme 5 c# interface factory-pattern
让我们假设我们得到以下内容:
A)工厂界面如
public interface IEmployeeFactory
{
IEmployee CreateEmployee(Person person, Constants.EmployeeType type, DateTime hiredate);
}
Run Code Online (Sandbox Code Playgroud)
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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
C)员工家庭:Manager与Sales从一个抽象的员工继承.
更多关于我的简单建筑

一些客户端代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我想问一些业务逻辑架构的细节.使用该架构我无法设计公司类应该是什么,不知道如何集成它.
我已经读过在这种情况下使用基于角色的架构更合适.你能提供一个例子(公司,员工实体)吗?
谢谢!