设计模式-策略模式
我是设计模式的初学者。
假设我正在开发一个 C# 应用程序来跟踪开发团队中各个成员执行的开发工作(即项目跟踪器)。
我试图从策略模式中获得启发。
所以我设计我的类和接口如下:
interface IEmployee
{
void Retires();
void TakesLeave();
}
interface IResponsible
{
void AcknowledgeJobAccomplish();
void CompletesJob();
}
interface ILeader
{
void FormsTeam();
void RecruitsNewMember();
void KicksOutMemberFromTheTeam();
void AssignsJob();
void UnassignsJob();
void QueriesTheJobStatus();
void ChangesTheJobStatus();
}
interface IPersistent
{
void Save();
void Update();
void Delete();
}
abstract class TeamMember : IEmployee, IResponsible, IPersistent
{
string Name;
}
class Programmer : TeamMember
{
}
class LeadProgrammer : Programmer, ILeader
{
ProgrammerCollection associateProgrammers;
}
class ProjectManager : TeamMember, ILeader
{
TeamMemberCollection teamMembers;
}
abstract class Tester : TeamMember
{
}
class UnitTester : Tester
{
}
class QC : Tester
{
}
class SupportStaff : TeamMember
{
}
我应该做些什么来改进这个设计?
I am a beginner in Design Patterns.
Suppose I am developing a C# application to track the development works performed by various members in development team (i.e. a Project Tracker).
I am trying to be inspired by Strategy Pattern.
So I am designing my classes and interfaces as follows:
interface IEmployee
{
void Retires();
void TakesLeave();
}
interface IResponsible
{
void AcknowledgeJobAccomplish();
void CompletesJob();
}
interface ILeader
{
void FormsTeam();
void RecruitsNewMember();
void KicksOutMemberFromTheTeam();
void AssignsJob();
void UnassignsJob();
void QueriesTheJobStatus();
void ChangesTheJobStatus();
}
interface IPersistent
{
void Save();
void Update();
void Delete();
}
abstract class TeamMember : IEmployee, IResponsible, IPersistent
{
string Name;
}
class Programmer : TeamMember
{
}
class LeadProgrammer : Programmer, ILeader
{
ProgrammerCollection associateProgrammers;
}
class ProjectManager : TeamMember, ILeader
{
TeamMemberCollection teamMembers;
}
abstract class Tester : TeamMember
{
}
class UnitTester : Tester
{
}
class QC : Tester
{
}
class SupportStaff : TeamMember
{
}
What things should I do to improve this design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,首先,您所拥有的不是策略模式的实例。 策略模式允许动态指定完成任务的方法。 这里实际上更多的是标准接口设计,通过接口继承来分配职责和能力。
编辑:让我们举个例子。 假设你有一群工人; 您还有一组任务。 每个 Worker 可以执行一项任务。 这些任务可以包含多个内容,例如 DoFoo() 和 DoBar()。 每个工人都不知道他们将执行什么任务; 他们只知道当他们出现时他们将执行一项任务。
因此,我们希望将 Workers 建模为具有他们将执行的任务。 由于任务差异很大,我们将把任务实现为一个接口。
所以我们会得到:
那么,当我们实例化一个
Job
时,该Job
创建两个Worker
。 第一个Worker
有一个Task1
任务; 第二个Worker
有一个Task2
任务。 为了让 Worker 执行其任务,我们调用 Job 类的 DoJob() 方法,它只调用每个Worker
上的DoWork()
方法,而 Worker 又调用每个Worker
上的DoTask()
方法设置Worker
的code>Task。如果我们想要将
Worker
更改为所有执行Task1
的操作,我们调用ChangeJobsToTask1()
方法,该方法设置Task<对于
Job
包含的所有Worker
对象,/code> 到Task1
; 如果此时我们对Job
对象调用DoJob()
,则所有Worker
都将执行Task1< /代码> 任务。 同样,如果我们想将
Task
更改为Task2
,只需调用ChangeJobsToTask2()
方法即可; 当调用其DoWork()
方法时,所有Worker
都将执行Task2.DoTask()
。这里抽象的重要一点是,
Worker
暴露了一个DoWork()
方法,但它们不一定知道正在完成什么工作。 也就是说,Worker
的Task
是可以互换的;Worker
只是知道他们将要执行一个Task
,但具体内容对于Worker
来说并不重要。Well, first off, what you have there is not an instance of a Strategy pattern. The Strategy Pattern allows for the dynamic specification of a method for getting things done. What you have here is really more of a standard interface design, where you allocate responsibilities and abilities by interface inheritance.
Edit: Let's use an example. Let's say that you have a group of Workers; you also have a set of Tasks. Each Worker can perform a Task. These Tasks can consist if several things, such as DoFoo() and DoBar(). Each Worker does not know what Task they will perform; they just know when they show up that they will do a Task.
So we'll want to model Workers as having a Task that they will perform. Since the Tasks vary widely, we'll implement the Task as an interface.
So we'll have:
So what happens is that when we instantiate a
Job
, theJob
creates twoWorker
s. The firstWorker
has aTask1
task; the secondWorker
has aTask2
task. To make theWorker
s do theirTask
s, we call theDoJob()
method on theJob
class, which just calls theDoWork()
method on each of theWorker
s, which in turn calls theDoTask()
method on each of theTask
s that theWorker
s were set with.If we want to change the
Worker
s to all doTask1
, we call theChangeJobsToTask1()
method, which sets theTask
toTask1
for all of theWorker
objects contained by theJob
; if, at that point, we call theDoJob()
on theJob
object, all theWorker
s will perform theTask1
task. Similarly, if we want to change theTask
s toTask2
, just call theChangeJobsToTask2()
method; all theWorker
s will then execute theTask2.DoTask()
when theirDoWork()
method is called.The important point of abstraction here is that the
Worker
s expose aDoWork()
method, but they do not necessarily know what work it is that is being done. That is, theTask
s for theWorker
s are interchangeable; theWorker
s just know that they're going to do aTask
, but the specifics of what it is are unimportant to theWorker
s.我在你的例子中没有看到策略模式。 策略模式在参数中采用“策略”类(通常继承自具有逻辑方法的接口,例如“DoJob()”),当调用方法时,它将通过应用之前传递的策略来执行操作,而不知道它是什么会具体做。
在您的示例中,您可以拥有一个所有人员都继承的类,该类具有 SetJob(IJobStrategy) 和 DoJob() 方法,该方法将调用接口 DoJob() (来自 IJobStrategy)。 然后,您可以有多个继承 IJobStrategy 的具体作业。 这样,您的员工不知道该工作,您可以更改工作而无需修改人员类别。
您可以在此处查看示例和更多信息。
I do not see the strategy pattern in your example. Strategy pattern take the "strategy" class (usually inherit from an Interface with a logic method, example "DoJob()") in parameter and when a method is called, it will do operation on by applying the strategy passing earlier without knowing what it will concretly do.
In your example you could have a class that all your people inherit that have a SetJob(IJobStrategy) and have a DoJob() method that will call the interface DoJob() (from IJobStrategy). Than, you can have multiple concrete job that inherit IJobStrategy. This way, your people do not know the job and you can change the job without having to modify the person class.
You can see example and more information here.
这看起来更像是接口隔离原则。 现在,这确实与策略配合得很好,但这就是我要做的不同之处。
Tester 不会是一个具体类,它会是一个 TeamMember,并配置了 TesterCompletesJobStrategy,因为它是 CompletesJobStrategy。 毕竟,唯一阻止我进行测试的是我当前在团队中分配的任务。
作为一个谈话要点,如果我的目标是一个策略,我会从类似这样的事情开始。
This seems much more like the Interface Segregation Principle. Now, that does play well with strategy, but here's what I'd make different.
Tester wouldn't be a Concrete class, it would be a TeamMember with a TesterCompletesJobStrategy configured as it's CompletesJobStrategy. After all, the only thing keeping me from testing is my currently assigned task on the team.
Just as a talking point, I'd start with something more like this if I was targeting a Strategy.
你可以在c#中使用“dynamic”来代替
像这样:
方法(动态输入)
方法(DTO1输入)
方式(DTO2输入)
方法(DTO3输入)
无编译时间错误的
u can use "dynamic" in c# instead
like this:
Method(dynamic input)
Method(DTO1 input)
Method(DTO2 input)
Method(DTO3 input)
without complie time error