这适用于员工呼叫系统。
到目前为止,我有3个班:人事经理和经理团队。
作为基类的人,Manager是能够使用类ManagerTeam访问创建新员工的权限的类。
为了简单起见,我将跳过一些人类课程。
class Person
{
private string firstName;
private string lastName;
private string phone;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
}
class Manager : Person
{
private string Position;
public Manager() : base()
{
Position = "Manager";
}
/*
* later on maybe some sort of regulation for creating new manager
* or new person with acabailities here
*/
}
class ManagerTeam : Manager
{
private string Shifts;
private string On_Call;
string[] Employee_list = new string[5];
/*
* maybe this should be turned into a funciton to insert all employees from
* "CreateNewEmployees" into the array.
*/
public ManagerTeam() : base()
{
/*
* this calls for another object being person to be assigned into
* an array for person.
*/
}
static MangerTeam CreateNewEmployee() /* change this is constructor */
{
/*
* this assigns shift times and checks if on call and any data
* to an employee
*/
}
}
我感到很困惑,因为我想到哪个应该是构造函数,哪个应该只是将新员工的对象加载到员工列表的数组中,这应该由管理员访问,后面有几个原因要实现。后来的目标是添加员工类,并且该员工类将具有接受和拒绝请求的能力,该信息也将在开发时进入管理团队。
答案 0 :(得分:2)
OOP中的继承通常意味着" is-a"关系。在你的情况下,"是" ManagerTeam
一个Manager
?在不知道ManagerTeam
的目的的情况下很难说,但仅凭名称,我猜不会。因此,继承可能不是您设计中的正确选择。
就构造函数与方法而言,请考虑有效对象的必需是什么。如果管理员需要让员工正常运作,那么应该在构造函数中,否则单独的方法是合适的。
答案 1 :(得分:2)
任何系统成功的关键是简化。对于实体类来说尤其如此,这些实体类将构成构建其余应用程序的基础。在这里弄错了,项目很可能会失败或需要很长时间才能完成。
我与其他开发人员分享的大部分项目往往过度设计,比实际需要的方式更复杂,使维护和新改进变得困难。
关于你的示例恕我直言,你可以尝试更简单的事情,并在需要时增加复杂性:
为普通员工和经理重复使用Employee类,并使用position属性指定Employee的类型。
public class Employee
{
private string firstName;
private string lastName;
private string phone;
//This could be turned into a class or a enum, e.g. Roles for more complex scenarios
private string position;
private string team;
private string shift;
//You can add a constructor if you want to make sure a position is always provided, but it would be better to handle this at the database level by setting the column as NOT NULL
public Employee(string position)
{
this.position = position;
}
...
}
ManagerTeam类将是您将用于管理数据的核心或帮助程序类,包括业务规则,它不应包含自己的数据,只需调用其他方法,例如,数据库上下文或工作单元检索数据:
public static class ManagerTeam
{
//You need to set this when the user signs in
internal static Employee CurrentEmployee;
//This method will return the full list of employees from your data source, you could use an IQueryable instead of the list so you can filter and make queries
public static IList<Employee> GetEmployees()
{
return db.Employees;
}
public static IList<Employee> GetEmployeeByPosition(string position)
{
return db.Employees.Where(x => x.Position == position);
}
public static IList<Employee> GetEmployeesByShift(string shift)
{
return db.Employees.Where(x => x.Shift == shift);
}
public static IList<Employee> GetEmployeesByTeam(string team)
{
return db.Employees.Where(x => x.Team == team);
}
public static void CreateEmployee(Employee employee)
{
// Only managers can add employees
if (CurrentEmployee.Position == "Manager") {
db.Employees.Add(employee);
}
}
...
}
如果您使用的是ASP.NET MVC或WPF等框架,那么您可以完全避免使用该类,只需处理控制器,操作,视图模型等中的所有内容......
答案 2 :(得分:0)
我建议使用mixin模式来解决这个问题。创建员工是员工的功能,员工是经理,经理团队是员工的经理。 所以我会把它分解成以下内容:
class Employee
{
private string firstName;
private string lastName;
private string phone;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
}
interface IManager
{
Employee CreateNewEmployee();
}
class Manager : IManager
{
private readonly Employee _employee;
public Manager(Employee employee)
{
_employee = employee;
}
public Employee CreateNewEmployee()
{
throw new NotImplementedException();
}
}
class ManagerTeam : IManager
{
private readonly IEnumerable<IManager> _employees;
public ManagerTeam(IEnumerable<IManager> employees)
{
_employees = employees;
}
public Employee CreateNewEmployee()
{
throw new NotImplementedException();
}
}