如何解决此代码重复+添加另一种方法 [英] how to solve this code duplication + add another method

查看:26
本文介绍了如何解决此代码重复+添加另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个班级:

class First {
    public void SetA(){ ... }
    public void SetB(){ ... }
    public void SetC(){ ... }
    public void SetD(){ ... }
    public void SetZ(){ ... }
}

class Second {
    public void SetC(){ ... }
    public void SetD(){ ... }
    public void SetE(){ ... }
    public void SetF(){ ... }
    public void SetX(){ ... }
}

class Third{
    public void SetA(){ ... }
    public void SetB(){ ... }
    public void SetE(){ ... }
    public void SetF(){ ... }
    public void SetY(){ ... }
}

如您所见,我在相同的方法中重复了代码.
昨天我意识到有时我想在每个方法中添加另一个推荐.
所以我正在寻找一种方法来解决这两个问题.我想到的一种解决方案是:

As you can see, I duplicate code in the same methods.
Yesterday I realized that sometimes I would like to add another commend inside each method.
So I am looking for a way to solve both those problems. One solution I thought about is:

  1. 创建接口:

  1. Create Interface:

interface IAllMethods {
        void SetA();
        void SetB();
        void SetC();
        void SetD();
        void SetE();
        void SetF();
        void SetX();
        void SetY();
        void SetZ();
}

  • 创建默认实现:

  • Create default implementation:

    class DefaultAllMethods {
            public void SetA(){ ... }
            public void SetB(){ ... }
            public void SetC(){ ... }
            public void SetD(){ ... }
            public void SetE(){ ... }
            public void SetF(){ ... }
            public void SetX(){ ... }
            public void SetY(){ ... }
            public void SetZ(){ ... }
    }
    

  • 使用装饰器模式创建另一个实现以添加额外的命令:

  • Create another implementation using decorator pattern in order to add the extra command:

    class ExtraAllMethods {
            private IAllMethods _allMethods;
            public ExtraAllMethods (IAllMethods allMethods) {
                _allMethods=allMethods;
            }
            public void SetA(){ 
                _allMethods.SetA();
                extraMethod();
            }
            public void SetB(){ 
                _allMethods.SetB();
                extraMethod();
            }
            public void SetC(){ 
                _allMethods.SetC();
                extraMethod();
            }
            ..
            ..
            ..
    }
    

  • 在 First、Second 和 Third 类中使用欲望实现.例如:

  • Use the desire implementation inside classes First, Second and Third. For example:

    class Third{
        private IAllMethods  _allMethods;
        public Third(IAllMethods allMethods) {
            _allMethods=allMethods;
        }
        public void SetA(){ _allMethods.SetA(); }
        public void SetB(){ _allMethods.SetB(); }
        ..
        ..
        ..
    }
    

  • 您如何看待这个解决方案?有没有更好的设计来满足这种需求?

    What do you think about this solution? Is there any better design to this need?

    更新
    人们要求真正的业务,所以这里是:我有3种传输类型:传输A,传输B,传输C每个传输都有许多参数(成员或属性).例如,TransmissionA 有 WorkerId、CustomerId、MessageName 等.TransmissionB 有 WorkerId 和 MessageName,但没有 CustomerId.TransmissionC 有 WorkerId、CustomerId 但没有 MessageName.这些只是示例 - 在我的情况下,每次传输都有更多的属性.每个属性都有 Set 方法.
    现在有了新的需求.在系统的某个地方有一个名为更新任务"的选项.如果该选项为 ON,那么我需要在每个 Set 方法中更新相关任务.这就是我想到装饰者模式的原因.

    UPDATE
    People ask for the real business, so here is it: I have 3 types of transmission: TransmissionA, TransmissionB, TransmissionC Each transmissions has many parameters (members or properties). For example, TransmissionA has WorkerId, CustomerId, MessageName and so on. TransmissionB has WorkerId and MessageName but no CustomerId. TransmissionC has WorkerId, CustomerId but no MessageName. These are example only - in my situation I have many more properties for each transmission. Each property has Set method.
    Now there is a new need. Somewhere on the system there is an option called "Update Task". If the option is ON then I need to update relevant task in each Set method. This is why I thought on decorator pattern.

    推荐答案

    这将起作用,但不是引入包含所有方法的无所不知的接口和基本实现类(从而了解您的整个方法集合)您也可以采用更细粒度的方法,只为每个方法制作一个单一的接口和基本实现.这将更具可扩展性,如果设置正确,您可以以类似插件的方式轻松附加这些内容.

    this will work but instead of introducing all-knowing interface and base implementation classes containing all methods (and thus having knowledge of the entirety of your collection of methods) you could also go for a more granular approach and just make a single interface and base implementation for each method. That would be more extensible and you could attach those just as easy in a plugin-like fashion if you set it up properly.

    这篇关于如何解决此代码重复+添加另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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