避免重复代码的好策略 [英] Good strategy to avoid duplicate code

查看:82
本文介绍了避免重复代码的好策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有以下情况:

  public class A {
public String createString(final String value ){
if(value == null){
throw new NullPointerException(value must not be null。);
}
final StringBuffer sb = new StringBuffer();
sb.append(A);
sb.append(B);
sb.append(C);
if(value.length()> 3){
sb.append(D);
sb.append(E);
}
sb.append(Z);
return sb.toString();
}
}

另一个类应该做类似的任务: / p>

  public class B {
public String createString(final String value){
if(value == null ){
throw new NullPointerException(value must not be null。);
}
final StringBuffer sb = new StringBuffer();
sb.append(A);
sb.append(B);
sb.append(C);
sb.append(Z);
return sb.toString();
}
}

什么是避免重复代码的好策略?到目前为止我想到的是具有A子集功能的类B,因此应该从A类扩展,并且相同的任务应该被重构为受保护的方法(假设它们在同一个包中)。这是它的样子:

  public class A {
public String createString(final String value){
final StringBuffer sb = createTheFirstPart(value);
if(value.length()> 3){
sb.append(D);
sb.append(E);
}
createTheLastPart(sb);
return sb.toString();
}

protected void createTheLastPart(final StringBuffer sb){
sb.append(Z);


protected StringBuffer createTheFirstPart(final String value){
if(value == null){
throw new NullPointerException(value must not be null) );
}
final StringBuffer sb = new StringBuffer();
sb.append(A);
sb.append(B);
sb.append(C);
return sb;
}
}

而B类:

  public class B extends A {
public String createString(final String value){
final StringBuffer sb = createTheFirstPart(value);
createTheLastPart(sb);
return sb.toString();
}
}

另一种可能的解决方案是这样的: p>

  public class A {
public String createString(final String value){
if(value == null) {
throw new NullPointerException(value must not be null。);
}
final StringBuffer sb = new StringBuffer();
sb.append(A);
sb.append(B);
sb.append(C);
addSomeSpecialThings(value,sb);
sb.append(Z);
return sb.toString();
}

protected void addSomeSpecialThings(final String value,final StringBuffer sb){
if(value.length()> 3){
sb.append D);
sb.append(E);
}
}
}

和B类: p>

  public class B extends A {
public String createString(final String value){
return super.createString(值);
}

protected void addSomeSpecialThings(final String value,final StringBuffer sb){
// do nothing
}
}

显然这不是很好,因为B有一个空的含义。的addSomeSpecialThings。
另外这个例子是一个非常简单的例子。例如,在方法中可能会有更多的区别,因此提取相同的功能并不容易。



我的解决方案都是关于继承,也许也会更好用组合做这个。我也认为这可能是策略模式的一个canidate。



那么这种问题最好的方法是什么?感谢您提前获得任何帮助。



kuku。

解决方案

将共享代码放在A和B的超类中:

  public abstract class SomeName {
public final String createString (final String value){
if(value == null){
throw new NullPointerException(value must not be null。);
}
final StringBuffer sb = new StringBuffer();
sb.append(A);
sb.append(B);
sb.append(C);
addSomeSpecialThings(value,sb);
sb.append(Z);
return sb.toString();
}

protected abstract void addSomeSpecialThings(final String value,
final StringBuffer sb);
}

然后B将如下所示:

  public class B extends SomeName {
protected void addSomeSpecialThings(final String value,
final StringBuffer sb){}
}

这将是A:

  public class A extends SomeName {
protected void addSomeSpecialThings(final String value,final StringBuffer sb){
if(value.length()> 3){
sb.append( D);
sb.append(E);
}
}
}


Lets say i have the following scenario:

public class A {
    public String createString(final String value){
        if (value ==  null){
            throw new NullPointerException("value must NOT be null.");
        }
        final StringBuffer sb = new StringBuffer();
        sb.append("A");
        sb.append("B");
        sb.append("C");
        if (value.length() > 3){
            sb.append("D");
            sb.append("E");
        }
        sb.append("Z");
        return sb.toString();   
    }
}

And another class which should do a similar Task:

public class B {
    public String createString(final String value){
        if (value ==  null){
            throw new NullPointerException("value must NOT be null.");
        }
        final StringBuffer sb = new StringBuffer();
        sb.append("A");
        sb.append("B");
        sb.append("C");
        sb.append("Z");
        return sb.toString();   
    }
}

What would be a good strategy to avoid the duplicate code ? What i came up with so far is that the class B which has a subset functionality of A and therefore should extend from the class A and the same tasks should be refactored into protected methods (assuming they are in the same package). This is what it would look like:

public class A {
    public String createString(final String value){
        final StringBuffer sb = createTheFirstPart(value);
        if (value.length() > 3){
            sb.append("D");
            sb.append("E");
        }
        createTheLastPart(sb);
        return sb.toString();   
    }

    protected void createTheLastPart(final StringBuffer sb) {
        sb.append("Z");
    }

    protected StringBuffer createTheFirstPart(final String value) {
        if (value ==  null){
            throw new NullPointerException("value must NOT be null.");
        }
        final StringBuffer sb = new StringBuffer();
        sb.append("A");
        sb.append("B");
        sb.append("C");
        return sb;
    }
}

And the class B:

public class B extends A {
    public String createString(final String value){
        final StringBuffer sb = createTheFirstPart(value);
        createTheLastPart(sb);
        return sb.toString();   
    }
}

Another possible solution would be something like this:

public class A {
    public String createString(final String value){
        if (value ==  null){
            throw new NullPointerException("value must NOT be null.");
        }
        final StringBuffer sb = new StringBuffer();
        sb.append("A");
        sb.append("B");
        sb.append("C");
        addSomeSpecialThings(value, sb);
        sb.append("Z");
        return sb.toString();   
    }

    protected void addSomeSpecialThings(final String value, final StringBuffer sb) {
        if (value.length() > 3){
            sb.append("D");
            sb.append("E");
        }
    }
}

and class B:

public class B extends A {
    public String createString(final String value){
        return super.createString(value);
    }

    protected void addSomeSpecialThings(final String value, final StringBuffer sb) {
        // do nothing
    }
}

Obviously this is not that good because B has an empty impl. of addSomeSpecialThings. Also this excample is a very simple one. For Example there could be more difference within the method so it would not be that easy to extract the same functionality.

My solutions are all about inheritance maybe it also would be better to do this with composition. I also thought that this is probably a canidate for the strategy pattern.

Well whats the best approach to such kind of Problem ? Thanks in advance for any help.

kuku.

解决方案

I would put the shared code in a superclass of A and B:

public abstract class SomeName {
    public final String createString(final String value){
        if (value ==  null){
            throw new NullPointerException("value must NOT be null.");
        }
        final StringBuffer sb = new StringBuffer();
        sb.append("A");
        sb.append("B");
        sb.append("C");
        addSomeSpecialThings(value, sb);
        sb.append("Z");
        return sb.toString();   
    }

    protected abstract void addSomeSpecialThings(final String value,
            final StringBuffer sb);
}

Then B would look like this:

public class B extends SomeName {
    protected void addSomeSpecialThings(final String value,
            final StringBuffer sb) {}
}

And this would be A:

public class A extends SomeName {
    protected void addSomeSpecialThings(final String value, final StringBuffer sb) {
        if (value.length() > 3){
            sb.append("D");
            sb.append("E");
        }
    }
}

这篇关于避免重复代码的好策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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