如何控制/坐标算法

How can be controled / coordinated algorithm

下图是复杂算法的简单部分。 我试着按照算法准备了一些classes。

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.
    }
}

我主要关心的是管理很多class成功algorithm.I尝试设计关卡工厂方法将决定class(在同一关卡)必须是在该级别实例化 在级别工厂方法的 constructor.Do 中管理下一级工厂方法 instantiated.Flow 之后,您认为这是可管理的,可维护的吗?您提供更好的解决方案吗?

创作模式:

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

行为模式:

如果您要在 运行 时间动态地寻找算法交换,您应该使用 Strategy_patternContext

上下文 知道和returns 特定算法,因此客户端不知道所有 100 或 1000 类,它们实现了一个算法。

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

结构模式:

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

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