简体   繁体   English

Java 8:寻找减少代码重复的设计模式

[英]Java 8: looking for a design pattern to reduce code duplication

I'm having the following test classes.我有以下测试课程。 I'm wondering if there's a design pattern that can reduce the code duplication in the following scenario.我想知道是否有一种设计模式可以减少以下场景中的代码重复。

public abstract class BaseTestClass {

    protected String color;

    protected String type;

    protected Product product;

    abstract void setColor();

    abstract void setClothType();

    abstract Product createTheProduct();

    @BeforeClass
    public void setUp(){
      // there're partial overlaps of "setup" for pants and shirts
    }

    @Test
    public void doATest(){
        testSomethingWithProduct(product);
    }

    @AfterClass
    public void tearDown(){
      // there're partial overlaps of "tearDown" for pants and shirts
    }
}


public class TestBlueShirt extends BaseTestClass {

    @Override
    void setColor() {
        this.color = "blue";
    }

    @Override
    void setClothType() {
        this.type = "shirt";
    }

    @Override
    Product createTheProduct() {
       setColor();
       setClothType();
       // create this.product based on color and type...
    }
}

public class TestRedShirt extends BaseTestClass {}
public class TestBluePants extends BaseTestClass {}
public class TestRedPants extends BaseTestClass {}
...

You will find there's duplicated code when setting colors for the same type of cloth or when setting the type for the same color.同种布料设置colors或同色设置类型时,会出现重复码。 I'm wondering how I can have a concrete class that can produce something like Class<T> (RedShirt, BlueShirt, RedPants, etc.), and based on Class<T> , I can directly implement @Test in the base class.我想知道如何拥有一个具体的 class 可以产生类似Class<T> (RedShirt、BlueShirt、RedPants 等)的东西,并且基于Class<T> ,我可以直接在基础 class 中实现@Test So I can avoid code duplication as much as I can.所以我可以尽可能地避免代码重复。

something like:就像是:

public abstract class BaseTestClass {

    protected Product product;

    abstract Product createTheProduct(Class<T> ...);

    @BeforeClass
    public void setUp(){
      setUpBasedOnProduct(Class<T> ...);

    }

    @Test
    public void doATest(){
        testSomethingWithProduct(product);
    }

    @AfterClass
    public void tearDown(){
        tearDownBasedOnProduct(Class<T> ...);
    }
}

import ...ClassGenerator

public class TestBlueShirt extends BaseTestClass {
    
    @Override
    createTheProduct(ClassGenerator.createClass("blue", "shirt"));
}

Thanks in advance!提前致谢!

You want to create an object, so what you're looking for falls under the umbrella of creational design patterns.您想要创建一个 object,因此您正在寻找的内容属于创建设计模式的范畴。 I'm not sure if there's a perfect fit for your needs, but the Factory pattern matches some of your needs.我不确定是否有适合您的需求,但工厂模式符合您的一些需求。

In a typical Factory pattern use case, you would supply a type (as a String or enumeration) to a method, and receive a matching object.在典型的工厂模式用例中,您将向方法提供类型(作为String或枚举),并接收匹配的 object。 Your logic would be a little more complex in that there will be several inputs and some branching logic to locate the correct type.您的逻辑会稍微复杂一点,因为会有多个输入和一些分支逻辑来定位正确的类型。 For example, you can't just use a String "shirt" to get your object since the color is built into the types (you have RedShirt , BlueShirt , etc...).例如,您不能只使用String “衬衫”来获取 object,因为颜色内置于类型中(您有RedShirtBlueShirt等...)。

As a final note, I'd consider asking yourself why RedShirt and BlueShirt have to be of different types.作为最后一点,我会考虑问自己为什么RedShirtBlueShirt必须是不同的类型。 Rather than using a design pattern to get around the issue, I'd reconsider the original design.我不会使用设计模式来解决这个问题,而是重新考虑原始设计。 For example, you could use an Apparel extends Product class containing a color and type member, such that you can query that information regardless of the type of Apparel .例如,您可以使用Apparel extends Product class 包含一个colortype成员,这样您就可以查询该信息而不管Apparel的类型。 Of course, use your best judgement depending on your situation.当然,根据您的情况使用您的最佳判断。

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

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