如何可以控制/协调算法 [英] How can be controled / coordinated algorithm

查看:248
本文介绍了如何可以控制/协调算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图是复杂算法的简单部分。
我尝试根据算法准备一些类。

Below picture is a simple part of complex algorithm. I try to prepare some classes in accordance with algorithm.

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

我主要关心的是管理很多类成功的算法。我尝试设计这个级别的工厂方法将决定哪个类(在同一级别)必须在那个级别实例化下一级工厂方法实例化。流程在级别工厂方法的构造函数中管理。你认为这是可管理的主表格?您是否提供更好的解决方案?

My main concern is to manage a lot of classess to succes algorithm.I try to design that level factory methods will decide which class (at the same level) must be instantiated at that level After that next level factory method instantiated.Flow is managed in the level factory methods's constructor.Do you think this is manageable,maintable?Do you offer better solution?

推荐答案

创作模式:

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

You need as Abstract_factory, which returns two types of Factories as per your requirements -type_list_a and type_list_b.

行为模式:

如果您正在运行时寻找算法交换动态,您应该使用 Strategy_pattern 上下文

If you are looking for exchange of algorithm at run time dynamically, you should use Strategy_pattern with Context.

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

Context knows and returns specific algorithm so that client does not know all 100 or 1000 classes, which implement an algorithm.

strategy-pattern-example-in-java-with-context-class 提供了很好的例子。

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

If you want to hide the complexity to the client without exposing all sub-systems, use Facade pattern.

看看 sourcemaking 教程,了解每种模式的用例的更多细节。

Have a look at sourcemaking tutorials for more details on use cases of each pattern.

这篇关于如何可以控制/协调算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆