如何控制/协调算法

时间:2016-08-26 09:20:37

标签: design-patterns business-logic business-rules business-logic-layer

下图是复杂算法的一个简单部分。enter image description here 我尝试按照算法准备一些类。

abstract class Person
{
    public string HasXRecords { get; set; }
    public int PersonAnotherFeature { get; set; }
    public List<X> Xs { get; set; } = new List<X>();
}
abstract class X
{
    //There will more than 1000 type subX classes
}

interface IAdder
{
    void AddXToList();
}

interface IRemover
{
    void RemoveXFromList();
}

class XAdderFactory
{
    private Person _person;
    public bool PersonHasNoRecords
    {
        get
        {
            return string.IsNullOrEmpty(_person.HasXRecords);
        }
    }
    public XAdderFactory(Person person)
    {
        this._person = person;
        if (PersonHasNoRecords)
        {
            new XListMakerAFactory(person);
        }
        else
        {
            new XListMakerB(person);
        }
    }
}

class XListMakerB: IAdder
{
    private Person _person;
    public XListMakerB(Person person)
    {
        this._person = person;
        AddXToList();
        new PersonXListEvaluator(person);
    }
    public void AddXToList()
    {
        //Dynamic instance of X will be added in to person Xlist.
    }
}

class XListMakerAFactory
{
    public XListMakerAFactory(Person person)
    {
        switch (person.PersonAnotherFeature)
        {
            case 1:new XListMakerA1(person);
                break;
                //there will be XListMakerA2,XListMakerA3 etc.
        }
        new XRemoverFactory(person);
    }
}
class XListMakerA1: IAdder
{
    private Person _person;
    public XListMakerA1(Person person)
    {
        this._person = person;
        AddXToList();
        new PersonXListEvaluator(person);
    }
    public void AddXToList()
    {
        //_person.Xs.Add(new X1());
        // According to business logic,X2,X3 etc. will be added manually.
    }
}

class XRemoverFactory
{
    public XRemoverFactory(Person person)
    {
        new XRemoverFromList1(person);
        new XRemoverFromList2(person);
    }
}

class XRemoverFromList1 : IRemover
{
    private Person _person;
    public XRemoverFromList1(Person person)
    {
        this._person = person;
        RemoveXFromList();
    }
    public void RemoveXFromList()
    {
        //According some business logic some Xs will be removed.
    }
}

class XRemoverFromList2 : IRemover
{
    private Person _person;
    public XRemoverFromList2(Person person)
    {
        this._person = person;
        RemoveXFromList();
    }
    public void RemoveXFromList()
    {
        //According some business logic some Xs will be removed.
    }
}
 class PersonXListEvaluator
{
    public PersonXListEvaluator(Person person)
    {
        //According to business rules evaluation will be cordinated.
    }
}

我主要关心的是管理很多classess到successces算法。我尝试设计那个级别的工厂方法将决定哪个类(在同一级别)必须在该级别实例化之后实例化下一级工厂方法.Flow在级别工厂方法的构造函数中管理。您认为这是可管理的,可维护的吗?您提供更好的解决方案吗?

1 个答案:

答案 0 :(得分:1)

创作模式:

您需要Abstract_factory,根据您的要求返回两种类型的工厂-type_list_a和type_list_b。

行为模式:

如果您希望动态地在运行时交换算法,则应将Strategy_pattern Context 一起使用。

上下文知道并返回特定算法,以便客户端不知道实现算法的所有100或1000个类。

Real World Example of the Strategy Pattern提供了很好的例子。

结构模式:

如果要在不暴露所有子系统的情况下隐藏客户端的复杂性,请使用Facade模式。

查看sourcemaking教程,了解有关每种模式的用例的更多详细信息。