角色、抽象模式、松耦合
假设我们有以下内容:
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) 员工系列:从抽象员工继承的 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();
}
}
我想询问一些业务逻辑架构细节。 使用该架构,我无法设计 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定在 ToString() 方法中包含了 override 关键字吗?
Are you sure you included the override keyword in the ToString() method?