且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

怎么设计这个?

更新时间:2023-12-03 20:05:16



< vl ****** @ math.uic.edu>在留言中写道

news:br ************* @ kot.podval.edu ...

<vl******@math.uic.edu> wrote in message
news:br*************@kot.podval.edu...

我有关于类层次结构设计的一个相当新的问题。
假设我有一个Class0类,需要实现一个公共的Class0.compute()接口。有三种不同的方式可供选择实施,而班级用户必须能够做出选择。这就是我的想法。我声明了三个类,分别是ClassA,ClassB,ClassC,每个类都有一个可以被Class0使用的独特的compute()方法。 Class0是一个模板类,如下所示:

模板< class T>
类Class0 {
public:
double compute(){return对 - &GT;计算(); }
私人:
T * p;
};

然后我可以这样做:

Class0< ClassA> C;
C.compute();

问题:classA.compute(),classB.compute()和classC.compute()所有
有不同的数量和类型参数。我可能还想在将来添加classD等。这意味着我需要在Class0的公共区域中实现三个不同的compute()函数。

或者,我可以在Class0中编写一个返回相应计算的方法()函数来自指针p,但这看起来并不优雅。

我的另一个想法是将Class0声明为Class {A,B,C}的后代:

模板< class T>
类Class0:public T {
...
}
然后

Class0< CLASSA&GT; C;

然后Class0将从类ClassA等继承compute()。
让我感到不舒服的是Class0和类Class {A,B,C}
真的有is-a关系。 A,B,C只提供进行某些计算的手段。

任何更好的方法来完成我想做的事情?

非常感谢,
-
弗拉基米尔

I have a rather newbie question regarding design of class hierarchy.
Suppose I have a class Class0, and need to implement a public
Class0.compute() interface. There are three different ways to choose
the implementation, and the user of the class has to be able to make
that choice. Here is what I have in mind. I declare three classes,
ClassA, ClassB, ClassC, and each of them has a distinct compute()
method that can be used by Class0. Class0 is a template class, as
follows:

template <class T>
class Class0 {
public:
double compute() { return p->compute(); }
private:
T *p;
};

Then I could do:

Class0<ClassA> C;
C.compute();

Problem: classA.compute(), classB.compute() and classC.compute() all
have different number and types of parameters. I might also want to
add classD, etc. in the future. This means I would need to implement
three different compute() functions in the public area of Class0.

Alternatively, I could write a method in Class0 that returns a
corresponding compute() function from the pointer p, but this doesn''t
seem elegant.

Another idea I have is to declare Class0 a descendant of Class{A,B,C}:

template<class T>
class Class0 : public T {
...
}
and then

Class0<classA> C;

Then Class0 will inherit compute() from classes ClassA, etc.
What makes me uncomfortable is that Class0 and classes Class{A,B,C}
do not really have "is-a" relationship. A,B,C only provide the means
to do certain computations.

Any better way to accomplish what I am trying to do?

Many thanks,
--
Vladimir




如果这三个版本的参数数量和类型不同,那么我就是b $ b don'看不出任何理由要有不同的课程。为什么不只有

三个不同的compute()函数?类似的东西:


public:

double compute();

double compute(double param1);

double compute(double param1,int param2);


这样的东西会起作用吗?


-Howard




If the three versions have different number and types of parameters, then I
don''t see any reason to have distinct classes at all. Why not just have
three different compute() functions? Something like:

public:
double compute( );
double compute( double param1 );
double compute( double param1, int param2 );

Will something like this work?

-Howard



vl ****** @ math。 uic.edu 写道:
我有一个关于类层次结构设计的新手问题。
假设我有一个Class0类,需要实现一个公共
Class0.compute()接口。有三种不同的方式可以选择


只有三种?..

的实现,而且该类的用户必须能够制作
那个选择。这就是我的想法。我声明了三个类,分别是ClassA,ClassB,ClassC,每个类都有一个可以被Class0使用的独特的compute()方法。 Class0是一个模板类,


你的意思是,Class0是_class_template_?

如下:

模板< class T>
class Class0 {
public:
double compute(){return p-> compute(); }
私人:
T * p;
};

然后我可以这样做:

Class0< ClassA> C;
C.compute();

问题:classA.compute(),classB.compute()和classC.compute()所有
有不同的数量和类型参数。我可能还想在将来添加classD等。这意味着我需要在Class0的公共区域中实现三个不同的compute()函数。

或者,我可以在Class0中编写一个返回相应计算的方法()函数来自指针p,但这看起来并不优雅。


_A_方法?我认为你实际上需要编写尽可能多的方法

,因为会有用于实例化Class0的类。你可以通过引入一个成员_template_

模板< class T>来隐含地实现
。 class Class0 {

...

template< class U> U get_compute(){return& T :: compute;不知道怎么回事(不确定这会有什么用呢),怎么会推断出来呢?

我的另一个想法就是宣告Class0是一个后代类{A,B,C}:

模板< class T>
类Class0:public T {
...
}
和然后

Class0< classA> C;

然后Class0将从类ClassA等继承compute()。
让我感到不舒服的是Class0和类Class {A,B,C}
真的有is-a关系。


嗯?首先,你不应该把''Class0''称为_class_,因为它是

不是一个类。这是一个模板。第二,Class0< ClassA> _class_确实有

is-a与ClassA的关系,因为它是公开继承的

。 Class0< ClassB>也是如此。使用ClassB,Class0< ClassC>分别为

ClassC。

A,B,C只提供进行某些计算的手段。

任何更好的方法完成我想做的事情?
I have a rather newbie question regarding design of class hierarchy.
Suppose I have a class Class0, and need to implement a public
Class0.compute() interface. There are three different ways to choose
Only three?..
the implementation, and the user of the class has to be able to make
that choice. Here is what I have in mind. I declare three classes,
ClassA, ClassB, ClassC, and each of them has a distinct compute()
method that can be used by Class0. Class0 is a template class,
You mean, Class0 is a _class_template_?
as
follows:

template <class T>
class Class0 {
public:
double compute() { return p->compute(); }
private:
T *p;
};

Then I could do:

Class0<ClassA> C;
C.compute();

Problem: classA.compute(), classB.compute() and classC.compute() all
have different number and types of parameters. I might also want to
add classD, etc. in the future. This means I would need to implement
three different compute() functions in the public area of Class0.

Alternatively, I could write a method in Class0 that returns a
corresponding compute() function from the pointer p, but this doesn''t
seem elegant.
_A_ method? I think you actually will need to write as many methods
as there will be classes used to instantiate Class0. You can do it
implicitly by introducing a member _template_

template<class T> class Class0 {
...
template<class U> U get_compute() { return &T::compute; }

somehow (not sure this is going to work, though, how would U be deduced?)
Another idea I have is to declare Class0 a descendant of Class{A,B,C}:

template<class T>
class Class0 : public T {
...
}
and then

Class0<classA> C;

Then Class0 will inherit compute() from classes ClassA, etc.
What makes me uncomfortable is that Class0 and classes Class{A,B,C}
do not really have "is-a" relationship.
Huh? First of all, you shouldn''t call ''Class0'' a _class_ because it is
not a class. It''s a template. Second, Class0<ClassA> _class_ does have
the "is-a" relationship with ClassA because it is publicly inherited
from it. And so does Class0<ClassB> with ClassB, and Class0<ClassC> with
ClassC, respectively.
A,B,C only provide the means
to do certain computations.

Any better way to accomplish what I am trying to do?




我可能会投票给第三种方式。


V



I''d probably vote for the third way.

V


Victor Bazarov< v。******** @ comAcast.net>写道:
Victor Bazarov <v.********@comAcast.net> writes:
vl ****** @ math.uic.edu 写道:
我有一个关于类层次结构设计的新手问题。
假设我有一个Class0类,需要实现一个公共的Class0.compute( )界面。有三种不同的选择方式
只有三种?...
I have a rather newbie question regarding design of class hierarchy.
Suppose I have a class Class0, and need to implement a public
Class0.compute() interface. There are three different ways to choose
Only three?..




可能会有更多。



There could be more.

实现,并且类的用户必须能够做出选择。这就是我的想法。我声明了三个类,分别是ClassA,ClassB,ClassC,每个类都有一个可以被Class0使用的独特的compute()方法。 Class0是一个模板类,
你的意思是,Class0是_class_template _?
the implementation, and the user of the class has to be able to make
that choice. Here is what I have in mind. I declare three classes,
ClassA, ClassB, ClassC, and each of them has a distinct compute()
method that can be used by Class0. Class0 is a template class,
You mean, Class0 is a _class_template_?




吧。



right.

>如下:
模板< class T>
类Class0 {
public:
double compute(){return p-> compute(); }
私人:
T * p;
};
然后我可以这样做:
Class0< ClassA> C;
C.compute();
问题:classA.compute(),classB.compute()和classC.compute()都有不同数量和类型的参数。我可能还想在将来添加classD等。这意味着我需要在Class0的公共区域中实现三个不同的compute()函数。
或者,我可以在Class0中编写一个返回
对应的compute()函数的方法。指针p,但这看起来并不优雅。
> as
follows:
template <class T>
class Class0 {
public:
double compute() { return p->compute(); }
private:
T *p;
};
Then I could do:
Class0<ClassA> C;
C.compute();
Problem: classA.compute(), classB.compute() and classC.compute() all
have different number and types of parameters. I might also want to
add classD, etc. in the future. This means I would need to implement
three different compute() functions in the public area of Class0.
Alternatively, I could write a method in Class0 that returns a
corresponding compute() function from the pointer p, but this doesn''t
seem elegant.



_A_方法?我认为你实际上需要编写尽可能多的方法,因为会有用于实例化Class0的类。你可以通过引入一个成员_template_

模板< class T>隐式地这样做。 class Class0 {
...
模板<类U> U get_compute(){return& T :: compute;不知怎的(不确定这会起作用,怎么会推断出来?)



_A_ method? I think you actually will need to write as many methods
as there will be classes used to instantiate Class0. You can do it
implicitly by introducing a member _template_

template<class T> class Class0 {
...
template<class U> U get_compute() { return &T::compute; }

somehow (not sure this is going to work, though, how would U be deduced?)

我的另一个想法就是宣告Class0是一个后代类{A,B,C}:
模板<类T>
类Class0:public T {
...
}
然后> Class0< classA> C;
然后Class0将从类ClassA等继承compute()。
让我感到不舒服的是Class0和类Class {A,B,C}
实际上没有是-A&QUOT;关系。
Another idea I have is to declare Class0 a descendant of Class{A,B,C}:
template<class T>
class Class0 : public T {
...
}
and then
Class0<classA> C;
Then Class0 will inherit compute() from classes ClassA, etc.
What makes me uncomfortable is that Class0 and classes Class{A,B,C}
do not really have "is-a" relationship.



嗯?首先,你不应该把''Class0''称为_class_因为它不是一个类。这是一个模板。第二,Class0< ClassA> _class_确实有is-a和is-a。与ClassA的关系因为它是公开继承的。 Class0< ClassB>也是如此。使用ClassB,Class0< ClassC>分别使用
ClassC。



Huh? First of all, you shouldn''t call ''Class0'' a _class_ because it is
not a class. It''s a template. Second, Class0<ClassA> _class_ does have
the "is-a" relationship with ClassA because it is publicly inherited
from it. And so does Class0<ClassB> with ClassB, and Class0<ClassC> with
ClassC, respectively.



原谅我的语言。 Class0&LT; ClassA的&GT;当然,这与声明的编写方式有is-a

关系。我的意思

说是_logically_我感觉不舒服

Class0< ClassA>来自ClassA,因为ClassA仅为Class0< ClassA>提供了compute()函数的

实现。


-

弗拉基米尔


Forgive my language. Class0<ClassA> does of course have the "is-a"
relationship the way the declarations are written. What I meant to
say was that _logically_ I don''t feel comfortable deriving
Class0<ClassA> from ClassA, since ClassA only provides the
implementation of a compute() function for Class0<ClassA>.

--
Vladimir