What is the name of this bad practice / anti-pattern?

前端 未结 14 2142
独厮守ぢ
独厮守ぢ 2021-02-03 18:02

I\'m trying to explain to my team why this is bad practice, and am looking for an anti-pattern reference to help in my explanation. This is a very large enterprise app, so here

14条回答
  •  遇见更好的自我
    2021-02-03 18:38

    Since you don't show what the code really is for it's hard to give you a robust sulotion. Here is one that doesn't use any if clauses at all.

    // invoked to map different kinds of items to different features
    public void BootStrap
    {
        featureService.Register(typeof(MyItem), new CustomFeature());
    }
    
    // your code without any ifs.
    public void ControlStuff()
    {
        var listOfThings = LoadThings();
        foreach (var thing in listOfThings)
        {
            thing.InvokeFeatures();
        }
    }
    
    // your object
    interface IItem
    {
        public ICollection Features {get;set;}
    
        public void InvokeFeatues()
        {
            foreach (var feature in Features)
                feature.Invoke(this);
        }
    }
    
    // a feature that can be invoked on an item
    interface IFeature
    {
        void Invoke(IItem container);
    }
    
    // the "glue"
    public class FeatureService
    {
    
        void Register(Type itemType, IFeature feature)
        {
            _features.Add(itemType, feature);
        }
    
        void ApplyFeatures(T item) where T : IItem
        {
            item.Features = _features.FindFor(typof(T));
        }
    }
    

提交回复
热议问题