如何解决这个code复制+添加其他方法 [英] how to solve this code duplication + add another method

查看:137
本文介绍了如何解决这个code复制+添加其他方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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(){ ... }
}

正如你所看到的,我重复code在相同的方法。
昨天,我意识到,有时我想补充另一个表彰每个方法里面。
所以我在寻找一种方式来解决这两个问题。一个解决方案,我想的是:

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(){ ... }
    }
    

  • 使用Decorator模式,以添加额外的命令创建另一个实施:

  • 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();
            }
            ..
            ..
            ..
    }
    

  • 使用内部类的第一,第二和第三的愿望实现。例如:

  • 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个类型的传输:TransmissionA,TransmissionB,TransmissionC 每个传输有很多参数(成员或属性)。例如,TransmissionA具有WorkerId,客户ID,MessageName等。 TransmissionB有WorkerId和MessageName,但没有客户ID。 TransmissionC有WorkerId,客户ID,但没有MessageName。这些例子只是 - 在我的情况,我为每个传输更多的特性。每个属性都有设置方法。
    现在有一种新的需求。某处在系统上有一个名为更新任务的选项。如果该选项为ON,然后我需要在每套方法更新相关的任务。这就是为什么我认为在装饰图案。

    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.

    这篇关于如何解决这个code复制+添加其他方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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