首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi类和方法。需要为多个类实现一个方法

Delphi类和方法。需要为多个类实现一个方法
EN

Stack Overflow用户
提问于 2019-03-06 07:48:21
回答 2查看 165关注 0票数 0

举个例子,我给一个农场安排了蔬菜班。

代码语言:javascript
复制
TVegetable = class

TCarrot = class(TVegetable)
TTomato = class(TVegetable)

我需要两种不同种类的蔬菜,一种用于超市,另一种用于工厂。

代码语言:javascript
复制
TCarrotSupermarket = class(TCarrot)
TCarrotFactory = class(TCarrot)

除了一个方法的代码之外,这些类都是相同的:

代码语言:javascript
复制
procedure Utilization;

TCarrotSupermarket.Utilization与超市合作,TCarrotFactory.Utilization与工厂合作。

我需要一个相同的Utilization代码用于TCarrotSupermarket.UtilizationTTomatoSupermarket.UtilizationTPotatoSupermarket.Utilization,另一个代码用于TCarrotFactory.UtilizationTTomatoFactory.UtilizationTPotatoFactory.Utilization

只为Utilization编写两次代码(对于超市和工厂)并在适当的类中使用它的最佳方式是什么?

EN

回答 2

Stack Overflow用户

发布于 2019-03-07 03:00:30

欢迎使用Pattern Design。你的案例是Strategy patternn

代码语言:javascript
复制
class TStrategyVegetable = class(TVegetable)
  FUtil: TUtilization
public
  procedure Create(util: TUtilization);
  procedure Utilization();
end

procedure TStrategyVegetable.Create(util: TUtilization)
begin
  FUtil := util
end
procedure TStrategyVegetable.Utilization;
begin
  FUtil.Utilization;
end

然后在代码中:

代码语言:javascript
复制
carrotSupermarket = TCarrotSupermarket.Create(TCarrotSupermarketUtil.Create);
carrotFactory = TCarrotFactory.Create(TCarrotFactoryUtil.Create);
票数 1
EN

Stack Overflow用户

发布于 2019-03-07 16:37:45

下面是使用接口方法解析解决方案的一些伪代码。(未经测试,甚至没有编译,但它应该会为您指明正确的方向)

代码语言:javascript
复制
IFactoryInterface=interface(IUnknown) ['{someGUID}']
  procedure Utilization;
end;

ISuperMarketInterface=interface(IUnknown) ['{AnotherGUID}']
  procedure Utilization;
end;

TVegetable = class (TSomeObject,IFactoryInterface,ISupermarketInterface)
protected
  // the routines tha do the actual implementation
  // can be regular virtual, dynamic, or whatever
  procedure FactoryUtilization; 
  procedure SuperMarketUtilization; 
  // link up the interfaces using method resolution
  procedure IFactoryInterface.Utilization=FactoryUtilization;
  procedure ISupermarketInterface.Utilization=SuperMarketUtilization;

{ in case not derived from TInterfacedObject, 
  You'll have to add _AddRef,_Release and 
  QueryInterface methods too }

end;

// the other implementations can be as before
TCarrot = class(TVegetable)
TTomato = class(TVegetable)

然后,当使用代码时,它应该看起来像这样:

代码语言:javascript
复制
VAR lSOmeVegetable,lAnotherVegetable:TVegetable;
... 
lSomeVegetable:=TCarrot.Create
lanotherVegetable:=Tomato.Create
...
// call using buolt-in type checking
(lSOmeVegetable as IFactoryInterface).Utilization;
(lAnotherVegetable as ISuperMarketInterface).Utilization;

或以其他方式使用support例程

代码语言:javascript
复制
VAR lFactory:IFactoryInterface;
...
if supports(lSomeVegetable,IFactoryInterface,lFactory) then
  lFactory.Utilization
...

希望这能对你有所帮助。这里有一个指向一些匹配的documentation的指针

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55013388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档