重构具有许多子类的抽象Java类 [英] Refactoring abstract Java class with many child classes

查看:157
本文介绍了重构具有许多子类的抽象Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找想法来重构这种情况(更好的设计,最小的努力)。
从以下示例抽象类开始(实际有更多的字段,方法和抽象方法):

I'm looking for ideas on the best way to refactor this scenario (better design, minimal effort). Starting from the following example abstract class (actual has many more fields, methods and abstract methods) :

abstract class Car
{
    private int manufactureYear;
    // ... many more fields that are hard to clone

    public Car(int manYear)
    {
      this.manufactureYear = manYear;
    }

    abstract public Color getColor();
    abstract public int getNumCylinders();
}

有这么多的孩子类(说100)扩展这个类。这些小孩班被视为汽车的规格。这里有两个例子:

There are so many child classes (say 100) that extend this class. These child classes are considered like 'specs' for the cars. Here are two examples :

class CarOne extends Car
{
    private static Color COLOR = Color.Red;
    private static int CYLINDERS = 4;

    public CarOne(int manYear)
    {
      super(manYear);
    }

    public final Color getColor();
    {
    return COLOR;
    }

    public final int getNumCylinders() 
    {
    return CYLINDERS;
    }
}

class CarOneThousand extends Car
{
    private static Color COLOR = Color.Black;
    private static int CYLINDERS = 6;

    public CarOneThousand(int manYear)
    {
      super(manYear);
    }

    public final Color getColor();
    {
        return COLOR;
    }

    public final int getNumCylinders() 
    {
    return CYLINDERS;
    }
}

在运行时,车载对象得到实例化和使用: p $ $
$ b

During runtime car objects get instantiated and used:

CarOne carObject = new CarOne(2009);
carObject.getColor();
carObject.getNumCylinders();

然而,在获得一些外部数据后,我发现汽车被重新涂漆,引擎改变了。汽车的新规格变成:

However, after getting some external data, I discover that the car was repainted and the engine changed. The new specs for the car become:

class ModCar extends Car
{
    private static Color COLOR = Color.Blue; 
    private static int numCylinders = 8;

    public ModCar (int manYear)
    {
      super(manYear);
    }

    public final Color getColor();
    {
    return COLOR;
    }

    public final int getNumCylinders() 
    {
    return numCylinders;
    }
}

所以真的需要将这些规范应用到新的 carObject 而不修改现有的字段,例如 manufacturingDate 。问题是如何最大程度地减少这些100+小孩类的更改代码(最好让它们保持不变),同时可以在运行时更新 carObject

So really need to "apply" these specs to the new carObject without modifying existing fields such as manufactureDate. The problem is how to minimize the code of changes to those 100+ child classes (preferably leave them untouched) while being able to update the carObject during runtime.

NB我被赋予了这个代码的工作,所以我没有写这个条件开始。

N.B. I was given to work on this code so I didn't write it in this condition to begin with.

推荐答案

根据描述和示例,您正在不适当地使用继承。看起来您正在创建许多类,您应该使用单个类和许多对象实例。如果这是真的,您也不需要设计模式来解决问题。没有进一步澄清问题,这应该足够了:

Based on the description and example, you are using inheritance inappropriately. It looks like you are creating many classes where you should be using a single class and many object instances. If this is true, you also don't need a design pattern to solve the problem. Without further clarification of the problem, this should suffice:

class Car
{
    private int manufactureYear;
    private Color color;
    private int numCylinders;

    public int getManufactureYear() { return manufactureYear; }
    public void setManufactureYear(int manufactureYear) { this.manufactureYear = manufactureYear; }

    public Color getColor() { return color; }
    public void setColor(Color color) { this.color = color; }

    public int getNumCylinders() { return numCylinders; }
    public void setNumCylinders(int numCylinders) { this.numCylinders = numCylinders; }
}

示例用法:

// make a blue 6-cylinder:
Car blue6 = new Car();
blue6.setColor(BLUE);
blue6.setCylinders(6);

// make a red 4-cylinder:
Car red4 = new Car();
red4.setColor(RED);
red4.setCylinders(4);

// Uh-oh, they painted my red car!
red4.setColor(YELLOW);

如果要最小化更改,可以从上面使用我重构的Car类,然后清理孩子班,所以他们利用它。一些东西:

If you want to minimize changes, you could use my refactored Car class from above, and then clean up the child classes so they leverage it. Something like:

class CarOne extends Car { // extends my version of Car...

    private static Color COLOR = Color.Red;
    private static int CYLINDERS = 4;

    public CarOne() {
      setColor(COLOR);
      setNumCylinders(CYLINDERS );
    }

    // getters deleted, base class has them now
}

由于实际上是一个基类,我的猜测是,99%的代码没有引用具体的车类(只有基类),所以你应该能够很容易地改变。当然,很难说没有看到真正的代码。

Since there is in fact a base class, my guess is that 99% of the code does not reference the concrete car classes (only the base class), so you should be able to change things fairly easily. Of course, hard to say without seeing the real code.

这篇关于重构具有许多子类的抽象Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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