2014-09-19 66 views
2

我有一些非常相似的功能,但每一个功能都有一条线是不同的。这种情况的最佳解决方案(设计模式)是什么?

我该如何避免代码重复?

public class Example{ 

    public void f(){ 
     System.out.println("Start"); 
     OtherExample.start(); 
     AnotherExample.funct1(); //DIFFERENT CODE LINE 
     OtherExample.end(); 
     System.out.println("End"); 
    } 

    public void g(){ 
     System.out.println("Start"); 
     OtherExample.start(); 
     AnotherExample.funct2(); //DIFFERENT CODE LINE 
     OtherExample.end(); 
     System.out.println("End"); 
    } 

    public void h(){ 
     System.out.println("Start"); 
     OtherExample.start(); 
     AnotherExample.funct3(); //DIFFERENT CODE LINE 
     OtherExample.end(); 
     System.out.println("End"); 
    } 

    public void i(){ 
     System.out.println("Start"); 
     OtherExample.start(); 
     AnotherExample.funct4(); //DIFFERENT CODE LINE 
     OtherExample.end(); 
     System.out.println("End"); 
    } 
} 

你能告诉我一些合适的设计模式吗?

+4

我会看看到[模板方法模式(http://en.wikipedia.org/wiki/Template_method_pattern)。 – rgettman 2014-09-19 17:54:07

+2

有抽象父类吗? – user2693979 2014-09-19 17:57:33

+0

你能多给一点背景吗?这是更多的仪器,或方法装饰? – 2014-09-19 18:17:41

回答

3

这是之前JAVA 8不使用lambda expressions

你可以重构你这样的代码,使其看起来更整洁和可读性:

public abstact class ParentClass(){ 

    public void start() { 
     System.out.println("Start"); 
     OtherExample.start();   
     callMethodLetter(); 
     OtherExample.end(); 
     System.out.println("End"); 
    } 

    public abstract void callMethodLetter();  

} 

然后你可以扩展父类和落实callMethodLetter ()来调用正确的方法。

+0

除非装饰呼叫是父母和子女课程的责任,否则这将打破SRP ...换句话说,它的作品,但我不会推荐它。 – Kraal 2014-09-19 19:10:56

+0

请展开想了解你在说什么。如果孩子不得不说需要/调用哪种方法,它将如何打破SRP。 – StackFlowed 2014-09-19 19:21:01

+0

漂亮印花是一种责任,做事是另一回事,而这个“东西”可能是班上的主要责任。 – Kraal 2014-09-19 19:38:58

1

你的例子很简单,很难避免代码重复,同时不太复杂,但大概你有更复杂的情况。

你可以做这样的:

public class Example { 

    public void f() { 
     (new F()).execute(); 
    } 

    public void g() { 
     (new G()).execute(); 
    } 

    public void h() { 
     (new H()).execute(); 
    } 

    public void i() { 
     (new I()).execute(); 
    } 

    private class F extends AbstractX { 
     @Override 
     public void executeFunction() { 
      AnotherExample.funct1(); 
     } 
    } 

    private class G extends AbstractX { 
     @Override 
     public void executeFunction() { 
      AnotherExample.funct2(); 
     } 
    } 

    private class H extends AbstractX { 
     @Override 
     public void executeFunction() { 
      AnotherExample.funct3(); 
     } 
    } 

    private class I extends AbstractX { 
     @Override 
     public void executeFunction() { 
      AnotherExample.funct4(); 
     } 
    } 

    private abstract class AbstractX { 
     public void execute() { 
      System.out.println("Start"); 
      OtherExample.start(); 
      executeFunction(); 
      OtherExample.end(); 
      System.out.println("End"); 
     } 

     public abstract void executeFunction(); 
    } 

} 
+0

Aeshang击败我,但这是更详细的,所以我会留下来。 – Raskolnikov 2014-09-19 18:17:38

1

可以让两个独立的方法,在起点和终点像下面 -

public void start() { 
     System.out.println("Start"); 
     OtherExample.start();   
    } 

    public void end() { 
     OtherExample.end(); 
     System.out.println("End"); 
    } 

比你的方法调用调用 -

public void f(){ 
      start(); 
      AnotherExample.funct1(); //DIFFERENT CODE LINE 
      end(); 
     } 

也与其他方法一样。

0

您的四个“示例”方法都在同一个类中。是有原因的吗?如果这些是真正不同的“例子”,则更加面向对象。

首先创建一个实例类:

public abstract class Example { 
    public void execute(); 
} 

然后有4个不同的这个实例类的实现,如:

public class FirstExample { 
    public void execute() { 
    // ... 
    } 
} 

然后你可以有你的代码有地方:

public void wrap(Example example){ 
    start(); 
    example.execute(); 
    end(); 
} 

并用以下代码调用此代码片段:

wrap(new FirstExample()); 
... 
wrap(new FourthExample()); 
1

如果您能够使用Java 8,则这是method references的理想用例。

public class Example { 

    public void f() { 
     common(AnotherExample::funct1); 
    } 

    public void g() { 
     common(AnotherExample::funct2); 
    } 

    public void h() { 
     common(AnotherExample::funct3); 
    } 

    public void i() { 
     common(AnotherExample::funct4); 
    } 

    public void common(Runnable function){ 
     System.out.println("Start"); 
     OtherExample.start(); 
     function.run(); 
     OtherExample.end(); 
     System.out.println("End"); 
    } 
} 
5

这正是Lambda Expressions是:

public static void f(Runnable r) { 
    System.out.println("Start"); 
    OtherExample.start(); 
    r.run(); 
    OtherExample.end(); 
    System.out.println("End"); 
} 

public static void main(String[] args) { 
    f(AnotherExample::funct1); 
    f(AnotherExample::funct2); 
    f(AnotherExample::funct3); 
    f(AnotherExample::funct4); 
} 
相关问题