类设计 - 避免冗余代码 [英] Class Design - Avoiding redundant code

查看:168
本文介绍了类设计 - 避免冗余代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展B类的A类。
我的类B有5个抽象方法。在所有这5种方法中,我需要对methodA()进行一个调用。这样做的问题是,由于我需要创建一个可以让40个扩展classB的类,所以我需要为methodA写入相同的调用,每个类和这40个类都有5次。
SO我最后写入方法A的电话约200次。



现在我如何设计,以便我不需要这些电话子类级别?



下面的示例



方法A是一种在数据库中记录用户操作的方法。
所以每次我创建一个扩展类B的类,在所有5个抽象方法中,我需要调用一个方法A,它将用户的操作记录在数据库中。



谢谢

解决方案

您可以使用这样的模式:

 抽象public class B {
/ **覆盖实现子类逻辑* /
抽象保护SomeClass reallyCalculateStuff();

/ **类的客户端调用的公共API方法* /
final public SomeClass calculateStuff(){
executeSharedCode();
return reallyCalculateStuff();
}

/ **所有其他方法需要调用的方法* /
private void executeSharedCode(){
// ...
}
}

public class A extends B {
@Override
protected SomeClass reallyCalculateStuff(){
// ...
}
}

你显然会想出更好的名字。但是这个想法是基类已经包含了公共API,并且负责调用共享方法。



如果 executeSharedCode 需要在每个子类中实现不同的方式。只要使它 abstract



然而,根据使用情况,可能会有更好的选择。例如拦截器。也许值得重新思考设计,因为可以通过不同的设计类来避免这种模式。但是,所有这些事情都无法判断没有上下文,所以我只是给你这种模式。


I have a class A extending class B . My class B has 5 abstract methods. In all these 5 methods , I need to make one call each to a methodA() . The problem with this is that since I need to create around lets say 40 classes which extend classB , I need to write same calls for methodA , 5 times per class and in all those 40 classes. SO I end up writing calls to methodA , around 200 times.

Now how can I design this such that I don't need to make these calls at child class level?

Example below

The methodA here is a method which logs User's action in the database. So each time I create a class extending class B, in all the 5 abstract methods I need to call a methodA which logs user's action in database.

Thanks

解决方案

You can use a pattern like this:

abstract public class B {
    /** Override to implement the subclass logic */
    abstract protected SomeClass reallyCalculateStuff();

    /** The public API method to be called by clients of the class */
    final public SomeClass calculateStuff() {
        executeSharedCode();
        return reallyCalculateStuff();
    }

    /** The method all other methods need to call */
    private void executeSharedCode() {
        // ...
    }
}

public class A extends B {
    @Override
    protected SomeClass reallyCalculateStuff() {
        // ...
    }
}   

You obviously would want to come up with better names. But the idea is that the base class already contains the public API and takes care of calling the shared method.

This will also work if executeSharedCode needs to be implemented differently in every subclass. Just make it abstract as well.

However, depending on the usecase, there might be better alternatives to this. For example, interceptors. It might also be worth to rethink the design as this pattern could be perhaps avoided by designing classes differently. But all those things are impossible to judge without context, so I'll just give you this pattern.

这篇关于类设计 - 避免冗余代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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