如何在一个类中封装不同的类以维护其独特的方法? (在delphi中有多个继承?) [英] How to encapsulate different classes within one class mantaining their unique methods? (multiple inheritance in delphi?)

查看:210
本文介绍了如何在一个类中封装不同的类以维护其独特的方法? (在delphi中有多个继承?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重写一个免费的教育性数字电路模拟器,以增加其功能的惯性.我的问题是如何将事件分派到原始类,并向它们添加预先处理.我有这样的东西:

I'm currently rewriting a free educational digital circuit simulator to add inertiality to its features. My problem is how to dispatch events to original classes adding a pre-elaboration to them. I have something like this:

TC1 = class
  ID: integer;
  Connections : array [integer] of Pin;
  function Func1; virtual;
  function FuncN;
end;

TC2-1 = class (TC1)
  function Func1; override;
  function My1Func();
end;

TC2-n = class (TC1)
  function Func1; override;
  function MyNFunc();
end;


TContainer = class
  C1 : TC1;
  function ContFunc;
end;

function Container.ContFunc;
begin
    c1.Func1;
end;

现在,这意味着ContFunc可以按我希望的那样调用C2.Func1,它专门化了300多个继承自TC1形式的组件的行为.

Now this means that ContFunc call C2.Func1 as I wish, specializing behaviour of more than 300 components inheriting form TC1.

但是现在我必须添加一些特殊的操作(对于所有组件后代都相等 每次调用Func1时都从TC1中调用,如果需要,请在该操作期间进行选择 是否调用TC2-n.Func1(在更改了祖先TC1的某些属性之后. 有没有办法在不更改TC1的所有后代的情况下做到这一点? 我可以像这样使用辅助类(不赞成使用吗?):

But now I have to add some special operations (equal for all component descendants from TC1 every time Func1 is called, and chosing during that operations if I have to call TC2-n.Func1 or not (after changing some property of ancestor TC1. Is there a way to do that cleanly, without changing all descendants of TC1? Can I use a helper class (deprecated?) like this:

TH = class helper of TC1
  function Func1 virtual; override;
end;

function TH.Func1;
begin
  if x then TC2.Func1 else SaveActionData; 
end

如果我添加TH,则当TContainer调用Func1时,将调用谁? 如我所愿,它调用TC2.Func1而不是TH.Func1. 有没有一种方法可以覆盖后裔方法Func1 无需为任何一个编写帮助程序类(他们将完成所有工作) 相同的操作,意味着代码完全相等)? 可以从TH调用TC2-n的300个后代函数Func1吗?

If I add TH, when TContainer call Func1, who is called? It call TC2.Func1 and not TH.Func1 as I wished?. Is there a way to override descenants method Func1 without writing an helper class for any single one (they will do all the same operations, meaning exactly equal code)? It is possible to call from TH the 300 descendant functions Func1 of TC2-n ?

换句话说,我正在尝试找到一种方法,通过Tcontainer调用c1.Func1;来获得这样的调用:

In other words, I'm trying to find a way to obtain a call like this by Tcontainer call to c1.Func1;:

调用TC2.Func1(与TC1的任何后代不同)的NewFunc1(对于所有TC1的后代均相等).

NewFunc1 (equal for all TC1 descendants) who call TC2.Func1 (different for any descendant of TC1).

任何人都可以建议这样做的方法吗?

Anyone can suggest a way to do that?

推荐答案

每当有人调用Func1时,您都有一些任务需要执行,无论后代在其重写的方法中选择做什么.这是模板方法模式的工作.

You have some tasks that need to be performed whenever someone calls Func1, regardless of what descendants have chosen to do in their overridden methods. This is a job for the template method pattern.

为基类提供一个公共的非虚拟方法Func1,该方法执行所需的操作,然后调用一个受保护的虚拟方法.后代可以重写该虚拟方法,但是使用该类的任何人都只能调用公共非虚拟方法.

Give the base class a public non-virtual method Func1 that performs the operations you need and then invokes a protected virtual method. The descendants can override that virtual method, but anyone using the class can only call the public non-virtual method.

type
  TC1 = class
  protected
    function InternalFunc1: Integer; virtual; // abstract?
  public
    function Func1: Integer;
  end;

function TC1.Func1;
begin
  if x then
    Result := InternalFunc1
  else
    Result := SaveActionData; 
end;

现在,后代可以覆盖InternalFunc1,基类将确保仅在适当的时候调用它.

Now descendants can override InternalFunc1, and the base class will make sure it gets called only when appropriate.

type
  TC2 = class(TC1)
  protected
    function InternalFunc1: Integer; override;
  end;

您需要在所有300个后代类中重命名当前的Func1函数. IDE的重构工具可能会帮助您解决问题.

You'll need to rename your current Func1 function in all your 300 descendant classes. The IDE's refactoring tools might be able to help with that.

这篇关于如何在一个类中封装不同的类以维护其独特的方法? (在delphi中有多个继承?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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