There is a factory which has workers. The workers are allowed to carry out two kind of activity. Build and Maintain. The build activity is carried out by more than one worker whereas Maintain activity is carried out by one worker only.
Below are entity classes:
public class ActivityEntity
{
internal ActivityEntity(){}
public DateTime Start {get;set;}
public List<Worker> Workers {get;set;}
}
public class Activity
{
Public DateTime Start { get; set; }
}
public class BuildActivity: Activity
{
Public List<Worker> Workers { get; set; }
}
public class MaintainActivity: Activity
{
public Worker Worker { get; set; }
}
which creational design pattern should I use so that the caller (developer) can easily get access to respective class rather than writting if...else condition?
For example, here is what I did (but that does not work)
public interface IActivityBuilder
{
public void SetStartTime(DateTime startTime);
}
public interface IBuildActivityBuilder : IActivityBuilder
{
public void SetWorkers(List<Worker> workers);
}
public class BuildActivityBuilder: IBuildActivityBuilder
{
private readonly ActivityEntity activityEntity;
public BuildActivityBuilder()
{
activityEntity = new ActivityEntity();
}
public void SetStartTime(DateTime startTime)
{
activityEntity.Start = startTime;
}
public void SetWorkers(List<Worker> workers)
{
activityEntity.Workers= workers;
}
}
public interface IMaintainActivityBuilder : IActivityBuilder
{
public void SetWorker(Worker worker);
}
public class MaintainActivityBuilder: IMaintainActivityBuilder
{
private readonly ActivityEntity activityEntity;
public MaintainActivityBuilder()
{
activityEntity = new ActivityEntity();
}
public void SetStartTime(DateTime startTime)
{
activityEntity.Start = startTime;
}
public void SetWorkers(Worker worker)
{
activityEntity.Workers.Add(worker);
}
}
public interface IActivityModelBuilderResolver
{
public IActivityBuilder Resolve(ActivityType activityType);
}
public class ActivityModelBuilderResolver : IActivityModelBuilderResolver
{
public IActivityModelBuilder Resolve(ActivityType activityType)
{
if (activityType == ActivityType.Build)
{
return new BuildActivityBuilder();
}
else if (activityType == ActivityType.Maintain)
{
return new MaintainActivityBuilder ();
}
return null;
}
}
Now, when I use IActivityResolver, it correctly returns required builder but since the return type is IActivityModelBuilder which does not have "SetWorker(s)" method, I am not able to set the worker to ActivityEntity.
Aucun commentaire:
Enregistrer un commentaire