写个玩玩,不过可能不是很合题意type TCustomFuncArgs = class(TObject); TM1FuncArgs = class(TCustomFuncArgs); TM2FuncArgs = class(TCustomFuncArgs); TM3FuncArgs = class(TCustomFuncArgs); TM1FuncArgs = class(TCustomFuncArgs); TM4FuncArgs = class(TCustomFuncArgs); TM5FuncArgs = class(TCustomFuncArgs); TM7FuncArgs = class(TCustomFuncArgs); TM9FuncArgs = class(TCustomFuncArgs);type TCustomFunction = class(TObject) public function Execute(AArgs: TCustomFuncArgs): Variant; end; TM1Function = class(TCustomFunction); TM2Function = class(TCustomFunction); TM3Function = class(TCustomFunction); TM4Function = class(TCustomFunction); TM5Function = class(TCustomFunction); TM7Function = class(TCustomFunction); TM9Function = class(TCustomFunction); TFunctionFactory = class(TObject) public function GetFunction(AName: string): TCustomFunction; end;type TCustomClassInvoker = class(TObject) private FFunctionFactory: TFunctionFactory; protected function IsValidFuncName(AName: string): boolean; virtual; abstract; public constructor Create; destructor Destroy; override; procedure Call(AFuncName: string; AArgs: TCustomFuncArgs): Variant; end;constructor TCustomClassInvoker.Create; begin FFunctionFactory := TFunctionFactory.Create; end;destructor TCustomClassInvoker.Destroy; begin FFunctionFactory.Free; inherited; end;procedure TCustomClassInvoker.Call(AFuncName: string; AArgs: TCustomFuncArgs): Variant; var f: TCustomFunction; begin if not IsValidFuncName(AFuncName) then raise exception.create('xxxxxxxxx'); f := FFunctionFactory.GetFunction(AFuncName); if f = nil then raise Exception.Create('xxxxxxxxx'); result := f.Execute(AArgs); end;type TClassAInvoker = class(TCustomClassInvoker); protected function IsValidFuncName(AName: string): boolean; override; end; TClassBInvoker = class(TCustomClassInvoker); protected function IsValidFuncName(AName: string): boolean; override; end; TClassCInvoker = class(TCustomClassInvoker); protected function IsValidFuncName(AName: string): boolean; override; end;var AInvoker: TClassCInvoker; AArgs: TM3FuncArgs; begin AArgs := TM3FuncArgs.Create; AInvoker := TClassCInvoker.Create; showmessage(AInvoker.Call('M3', AArgs)); AArgs.Free; AInvoker.Free; end;
sanguomi的话是有道理的,从LZ题目上看,显然应该是在类A和类B已经定义并实现基础上,再来设计类C的,并非要完全重构A、B、C。所以使用接口或者从同一基类继承的方法是不符合题意的,因为我们不知道A和B是否实现了相同的接口,或者说A和B是否都派生于同一基类,题目说的仅仅是A和B的m1、m2、m3方法功能一样而已。 Delphi的类没有多重继承功能,但还是有很多方法来解决的: 我试着写了3种方法(不见得很好, 各有优缺点),为了简化,将不需要的函数(m2,m3,m5,m9)省略。方法1:type TA = class public procedure m1; procedure m7; end; TB = class public procedure m1; procedure m4; end; TC = class private FA: TA; FB: TB; public constructor Create; destructor Destroy; override; procedure m1; procedure m4; procedure m7; end; { TA }procedure TA.m1; begin ShowMessage('m1'); end;procedure TA.m7; begin ShowMessage('m7'); end;{ TB }procedure TB.m1; begin ShowMessage('m1'); end;procedure TB.m4; begin ShowMessage('m4'); end;{ TC }constructor TC.Create; begin FA := TA.Create; FB := TB.Create; end;destructor TC.Destroy; begin FA.Free; FB.Free; inherited; end;procedure TC.m1; begin FA.m1; end;procedure TC.m4; begin FB.m4; end;procedure TC.m7; begin FA.m7; end; 方法2:type TA = class public procedure m1; procedure m7; end; TB = class public procedure m1; procedure m4; end; TC = class(TA) private FB: TB; public constructor Create; destructor Destroy; override; procedure m4; end;{ TA }procedure TA.m1; begin ShowMessage('m1'); end;procedure TA.m7; begin ShowMessage('m7'); end;{ TB }procedure TB.m1; begin ShowMessage('m1'); end;procedure TB.m4; begin ShowMessage('m4'); end;{ TC }constructor TC.Create; begin FB := TB.Create; end;destructor TC.Destroy; begin FB.Free; inherited; end;procedure TC.m4; begin FB.m4; end;方法3:type TA = class public procedure m1; procedure m7; end; TB = class public procedure m1; procedure m4; end; TC = class(TA) public procedure m4; end;{ TA }procedure TA.m1; begin ShowMessage('m1'); end;procedure TA.m7; begin ShowMessage('m7'); end;{ TB }procedure TB.m1; begin ShowMessage('m1'); end;procedure TB.m4; begin ShowMessage('m4'); end;{ TC }procedure TC.m4; begin with TB.Create do begin m4; Free; end; end;
#include <iostream>
using namespace std;class A
{
public:
void M1(){std::cout<<"M1"<<endl;}
void M2(){std::cout<<"M2"<<endl;}
void M3(){std::cout<<"M3"<<endl;}
void M7(){std::cout<<"m7"<<endl;}protected:
private:
};
class B
{
public:
void M1(){std::cout<<"M1"<<endl;}
void M2(){std::cout<<"M2"<<endl;}
void M3(){std::cout<<"M3"<<endl;}
void M4(){std::cout<<"m4"<<endl;}protected:
private:
};class AB: public A, public B
{
public:
protected:
private:
};int main()
{
AB *test = new AB;
test->A::M1();
test->B::M1();
test->M4();
delete test;
return 0;
}
AB = class
private
Fa: A;
Fb: B;
public
procedure M1;
procedure M4
end; procedure AB.M1;
begin
Fa.M1;
end; procedure AB.M4;
begin
Fb.M4;
end;
但是看你代码A,B类还没成型
java 没有研究过这个问题
delphi下可以重名方法名 或 委托实现(这个应该类似java接口的implements)
procedure m1;
procedure m2;
procedure m3;
end;
IA = Interface
procedure m4;
procedure m5;
end;
IB = Interface
procedure m7;
procedure m9;
end; TExample=class(TInterfacedObject,IExample)
private
//子类共用的数据成员
public
procedure M1;virtual;
procedure M2;virtual;
procedure M3;virtual;
procedure other;virtual;abstract; end;
TExampleA(TExample,IA)
public
procedure M4;
procedure M5;
end; TExampleB(TExample,IB)
public
procedure M7;
procedure M9;
end; TExampleC(TExample,IA,IB)
private
FA: IA;
FB: IB;
public
constructor Create;
property ExampleA: IA read FA implements IA;
property ExampleB: IB read FB implements IB;
end;implement
....
constructor TExampleC.Create;
begin
FA:= TExampleA.Create;
FB:= TExampleB.Create;
end;抽象類,可以自己控制生存周期,接口自動控制,結合實際情況吧。至于提純,我覺得沒有啥原則或限制。
設計模式,只記得幾個常用的...
若三國的做法改為公布A、B的對象,這樣反而會暴露不必要的成員出來;所以看起來接口委托實現,應該是可以接受的。
就需要重构了
對比三國的做法,要是A或B的方法改名,也不會影響C的實現;
若三國的做法改為公布A、B的對象,這樣反而會暴露不必要的成員出來;所以看起來接口委托實現,應該是可以接受的。
TCustomFuncArgs = class(TObject); TM1FuncArgs = class(TCustomFuncArgs);
TM2FuncArgs = class(TCustomFuncArgs);
TM3FuncArgs = class(TCustomFuncArgs);
TM1FuncArgs = class(TCustomFuncArgs);
TM4FuncArgs = class(TCustomFuncArgs);
TM5FuncArgs = class(TCustomFuncArgs);
TM7FuncArgs = class(TCustomFuncArgs);
TM9FuncArgs = class(TCustomFuncArgs);type
TCustomFunction = class(TObject)
public
function Execute(AArgs: TCustomFuncArgs): Variant;
end; TM1Function = class(TCustomFunction);
TM2Function = class(TCustomFunction);
TM3Function = class(TCustomFunction);
TM4Function = class(TCustomFunction);
TM5Function = class(TCustomFunction);
TM7Function = class(TCustomFunction);
TM9Function = class(TCustomFunction); TFunctionFactory = class(TObject)
public
function GetFunction(AName: string): TCustomFunction;
end;type
TCustomClassInvoker = class(TObject)
private
FFunctionFactory: TFunctionFactory;
protected
function IsValidFuncName(AName: string): boolean; virtual; abstract;
public
constructor Create;
destructor Destroy; override;
procedure Call(AFuncName: string; AArgs: TCustomFuncArgs): Variant;
end;constructor TCustomClassInvoker.Create;
begin
FFunctionFactory := TFunctionFactory.Create;
end;destructor TCustomClassInvoker.Destroy;
begin
FFunctionFactory.Free;
inherited;
end;procedure TCustomClassInvoker.Call(AFuncName: string; AArgs: TCustomFuncArgs): Variant;
var
f: TCustomFunction;
begin
if not IsValidFuncName(AFuncName) then
raise exception.create('xxxxxxxxx'); f := FFunctionFactory.GetFunction(AFuncName);
if f = nil then
raise Exception.Create('xxxxxxxxx'); result := f.Execute(AArgs);
end;type
TClassAInvoker = class(TCustomClassInvoker);
protected
function IsValidFuncName(AName: string): boolean; override;
end; TClassBInvoker = class(TCustomClassInvoker);
protected
function IsValidFuncName(AName: string): boolean; override;
end; TClassCInvoker = class(TCustomClassInvoker);
protected
function IsValidFuncName(AName: string): boolean; override;
end;var
AInvoker: TClassCInvoker;
AArgs: TM3FuncArgs;
begin
AArgs := TM3FuncArgs.Create;
AInvoker := TClassCInvoker.Create;
showmessage(AInvoker.Call('M3', AArgs));
AArgs.Free;
AInvoker.Free;
end;
sanguomi的话是有道理的,从LZ题目上看,显然应该是在类A和类B已经定义并实现基础上,再来设计类C的,并非要完全重构A、B、C。所以使用接口或者从同一基类继承的方法是不符合题意的,因为我们不知道A和B是否实现了相同的接口,或者说A和B是否都派生于同一基类,题目说的仅仅是A和B的m1、m2、m3方法功能一样而已。
Delphi的类没有多重继承功能,但还是有很多方法来解决的:
我试着写了3种方法(不见得很好, 各有优缺点),为了简化,将不需要的函数(m2,m3,m5,m9)省略。方法1:type
TA = class
public
procedure m1;
procedure m7;
end; TB = class
public
procedure m1;
procedure m4;
end; TC = class
private
FA: TA;
FB: TB;
public
constructor Create;
destructor Destroy; override;
procedure m1;
procedure m4;
procedure m7;
end;
{ TA }procedure TA.m1;
begin
ShowMessage('m1');
end;procedure TA.m7;
begin
ShowMessage('m7');
end;{ TB }procedure TB.m1;
begin
ShowMessage('m1');
end;procedure TB.m4;
begin
ShowMessage('m4');
end;{ TC }constructor TC.Create;
begin
FA := TA.Create;
FB := TB.Create;
end;destructor TC.Destroy;
begin
FA.Free;
FB.Free;
inherited;
end;procedure TC.m1;
begin
FA.m1;
end;procedure TC.m4;
begin
FB.m4;
end;procedure TC.m7;
begin
FA.m7;
end;
方法2:type
TA = class
public
procedure m1;
procedure m7;
end; TB = class
public
procedure m1;
procedure m4;
end; TC = class(TA)
private
FB: TB;
public
constructor Create;
destructor Destroy; override;
procedure m4;
end;{ TA }procedure TA.m1;
begin
ShowMessage('m1');
end;procedure TA.m7;
begin
ShowMessage('m7');
end;{ TB }procedure TB.m1;
begin
ShowMessage('m1');
end;procedure TB.m4;
begin
ShowMessage('m4');
end;{ TC }constructor TC.Create;
begin
FB := TB.Create;
end;destructor TC.Destroy;
begin
FB.Free;
inherited;
end;procedure TC.m4;
begin
FB.m4;
end;方法3:type
TA = class
public
procedure m1;
procedure m7;
end; TB = class
public
procedure m1;
procedure m4;
end; TC = class(TA)
public
procedure m4;
end;{ TA }procedure TA.m1;
begin
ShowMessage('m1');
end;procedure TA.m7;
begin
ShowMessage('m7');
end;{ TB }procedure TB.m1;
begin
ShowMessage('m1');
end;procedure TB.m4;
begin
ShowMessage('m4');
end;{ TC }procedure TC.m4;
begin
with TB.Create do
begin
m4;
Free;
end;
end;
因为对象的数据也成了孤立的2、3套 TC = class
private
FA: TA;
FB: TB;
public
constructor Create;
destructor Destroy; override;
procedure m1;
procedure m4;
procedure m7;
end;
TC = class(TA)
private
FB: TB;
public
constructor Create;
destructor Destroy; override;
procedure m4;
end;
A继承基类,自有方法m7,m9
B继承基类,自有方法m4,m5
AB = class
private
Fa: A;
Fb: B;
public
procedure M1;
procedure M4
end;procedure AB.M1;
begin
Fa.M1;
end;procedure AB.M4;
begin
Fb.M4;
end;
深度怀疑!