【问题标题】:Best practice for consolidating multiple classes合并多个类的最佳实践
【发布时间】:2013-06-09 03:00:38
【问题描述】:

在下面的 sn-p 中,我有两个具有完全相同的过程名称的类。这些过程中的代码大不相同。是否可以合并这些类以便于维护和未来的增强,或者我是否会以可接受的方式解决这个问题?

type
  TMFRCore = class
    private
      FGridMain: TNextGrid;
      FGridSummary: TNextGrid;
    public
      constructor Create(Grid: TNextGrid; SummaryGrid: TNextGrid);
      destructor Destroy; override;
      procedure SetRowColour(Grid: TNextGrid; Row: Integer; Color: TColor; Start: Integer = 0);
      procedure OnSummaryDblClick(Sender: TObject);
      function GetSectionId(Section: String): Integer;

      property GridMain: TNextGrid read FGridMain write FGridMain;
      property GridSummary: TNextGrid read FGridSummary write FGridSummary;
  end;

type
  TMFRDates = class(TMFRCore)
    public
      procedure UpdateSummary;
      procedure UpdateMain(const sAircraft: string);
  end;

type
  TMFRHours = class(TMFRCore)
    public
      procedure UpdateSummary;
      procedure UpdateMain(const sAircraft: string);
  end;

我如下初始化这些类;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  MFRHours := TMFRHours.Create(gridHours, gridHoursSummary);
  MFRDates := TMFRDates.Create(gridDates, gridDatesSummary);
end;

【问题讨论】:

    标签: delphi delphi-xe4


    【解决方案1】:

    您可以在基类中将方法声明为虚拟(也可能是抽象),这样您就可以利用动态分派和多态性,但除此之外,实际上没有其他东西可以整合。这些类共有的所有东西都已经在一个基类中了。添加这些声明实际上意味着您有更多代码,因为您仍然需要保留现有的方法声明和实现。

    【讨论】:

    • 感谢罗布的回复。我想我会继续我要走的路。很高兴终于在德尔福取得进展,我的道路可以被大众接受:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 2010-09-18
    相关资源
    最近更新 更多