How can be controled / coordinated algorithm

穿精又带淫゛_ 提交于 2019-12-11 04:46:38

问题


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?


回答1:


Creational patterns:

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

Behavioural patterns:

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

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

Real World Example of the Strategy Pattern provides good example.

Structural patterns:

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

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



来源:https://stackoverflow.com/questions/39162794/how-can-be-controled-coordinated-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!