并行继承层次结构重构 [英] Parallel Inheritance Hierarchy Refactoring

查看:44
本文介绍了并行继承层次结构重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理并行继承层次结构,我有一个逐步重构的想法,并希望就它是否可行或是否有更好的方法来提出意见.

I'm dealing with a Parallel Inheritance Hierarchy and I have an idea for a step by step refactoring and would like opinions on whether it will work or whether there is a better way to do it.

  1. 创建一个 Handler 类,其中包含实现所需特定行为的子项,例如 CarHandler 和子项 vwHandlerfordHandlerbmwHandler

添加对所有组件的调用并对其进行重构,以便子项返回必要的对象.例如调用vwHandler.getDrive会返回vwDrive.drive的结果,调用vwHandler.getSeat会返回vwSeat.seat)

Add calls to all the components and restructure it so that the children return the objects neccessary. e.g. Call vwHandler.getDrive will return the result of vwDrive.drive, call vwHandler.getSeat returns vwSeat.seat)

重构其他类,以便它们提供通用功能而不是特定功能,例如调用 vwHandler.getDrive 将返回 drive(vwSpec1, vwSpec2)

Refactor the other classes so that they provide general functionality rather than specific e.g. Call vwHandler.getDrive will return drive(vwSpec1, vwSpec2)

处理程序示例

public abstract class CarHandler
{
   private Car car;

   public Car getCar()
   {
       return car;
   }
   public Car setCar()
   {
       car = car;
   }

   public CarHandler(Car car)
   {
       this.car = car;
   }

   public Car getCar()
   {
       return car;
   }

   public void setCar(Car car)
   {
       this.car = car;
   }

   public void updateCar()
   {
       Globalizer.updateOnServer(car);
   }

   public abstract Drive getNewDrive();
   public abstract Seat getNewSeat();    
}

推荐答案

这种方法实际上对我来说效果很好,我已经实现了前两个步骤,最终删除了数百行重复并获得了良好的效果看看未来如何进一步重构.我强烈推荐这种方法.

This approach is working quite well for me actually, I've implemented the first two steps and ended up removing several hundred lines of duplication as well as getting a good look at how to further refactor in future. I would highly recommend this approach.

这篇关于并行继承层次结构重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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