简体   繁体   English

这种情况的最佳解决方案(设计模式)是什么?

[英]What is best solution (design pattern) for this situation?

I have some very similar functions, but one line is different in each function. 我有一些非常相似的功能,但每个功能中有一行不同。

How can I avoid code duplication? 如何避免代码重复?

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");
    }
}

Could you tell me some appropriate design patterns? 你能告诉我一些合适的设计模式吗?

This is exactly what Lambda Expressions are for: 这正是Lambda表达式的用途:

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);
}

This is for prior to JAVA 8 without using lambda expressions 这是针对JAVA 8之前的,而不使用lambda表达式

You can refactor your code like this to make it look neater and readable : 您可以像这样重构代码,使其看起来更整洁和可读:

public abstact class ParentClass(){

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

   public abstract void callMethodLetter();       

}

Then you can extend the ParentClass and implement the callMethodLetter() to call the correct method. 然后,您可以扩展ParentClass并实现callMethodLetter()以调用正确的方法。

Your example is so simple that it's hard to avoid code duplication whilst being less verbose, but presumably you have more complicated situations in mind. 您的示例非常简单,以至于很难避免代码重复,同时又不那么冗长,但可能会考虑到更复杂的情况。

You could do it like this: 你可以这样做:

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

}

you can make two separate method for start and end like below - 你可以制作两个单独的开始和结束方法,如下所示 -

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

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

than call in your method call - 比调用你的方法调用 -

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

same call with other methods too. 同样用其他方法调用。

If you're able to use Java 8, this is a perfect use case for method references . 如果您能够使用Java 8,这是方法引用的完美用例。

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");
    }
}

Your four "examples" methods are all inside the same class. 您的四个“示例”方法都在同一个类中。 is there a reason for it ? 这是有原因的吗? If these are real different "examples", make it more object oriented. 如果这些是真正不同的“示例”,请使其更加面向对象。

First create an Example class : 首先创建一个Example类:

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

Then have 4 different implementations of this Example class such as : 然后有这个Example类的4种不同的实现,例如:

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

You can then have your code have somewhere : 然后,您可以将代码放在某处:

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

And call this snippet with : 并将此代码段称为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM