紧密耦合的课堂设计问题 [英] tightly coupled class design problem

查看:65
本文介绍了紧密耦合的课堂设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的应用程序中的一些类是图形化的。


要封装我创建的一个类的图形方面

" sprite"它包含一个位图并知道如何绘制自己等。


图形类包含一个精灵对象。


MyClass


{


私人:


Sprite this_objects_sprite;


}


精灵类需要一个指向视频表面的指针。围绕这个没有办法

。构造和绘制

精灵对象需要指针。


我的问题是:


这个设计意味着我必须将指针传递到视频表面,以便在其构造函数中传递给
myclass,例如


MyClass

{


私人:


Sprite this_objects_sprite;


public:


MyClass(VideoSurface * s)

{


this_objects_sprite = new Sprite(VideoSurface * s );


...


...


}
< br $> b $ b ....


....


}


显然这意味着我的所有图形对象都与我正在使用的

图形系统紧密耦合。另外很明显(我认为)这是

不可取的。


我在考虑安排事情,以便精灵不是

在容器对象构造函数中自动实例化。


也就是说,构造我的对象然后调用一个函数来构造对象

sprite,例如


MyClass c = new MyClass();


c.CreateSprite(VideoSurface * s);


但是这引发了其他问题,比如,如果我在创建它的精灵之前尝试绘制对象,我该如何处理会发生什么呢。


任何想法这个?


Dean



Some of the classes in my app are graphical.

To encapsulate the graphical side of things I had created a class called
"sprite" which holds a bit map and knows how to draw itself etc.

The classes that are graphical contain a sprite object.

MyClass

{

private:

Sprite this_objects_sprite;

}

The sprite class requires a pointer to a "video surface". There is no way
around this. The pointer is required for the construction and drawing of the
sprite object.

My problem is this:

This design means that I have to pass the pointer to the video surface to
myclass in its constructor, e.g.

MyClass

{

private:

Sprite this_objects_sprite;

public:

MyClass(VideoSurface* s)

{

this_objects_sprite = new Sprite(VideoSurface* s);

...

...

}

....

....

}

Obviously this means all my graphical objects are tightly coupled to the
graphics system I am using. Also obviously (I think) is that this is
undesireable.

I was thinking of maybe arranging things so that the sprite is not
automatically instantiated in the container objects constructor.

That is, I construct my object THEN call a function to construct the objects
sprite, e.g.

MyClass c = new MyClass();

c.CreateSprite(VideoSurface* s);

But then this raised other issues like, how do I handle what happens if i
try to draw the object before creating its sprite.

Any thoughts on this?

Dean

推荐答案



" Chocawok" <无**** @ nospam.com>在留言中写道

新闻:3N ******************** @ fe3.news.blueyonder.co .uk ...

"Chocawok" <no****@nospam.com> wrote in message
news:3N********************@fe3.news.blueyonder.co .uk...


我的应用程序中的一些类是图形化的。

为了封装图形方面的东西,我创建了一个名为
的类。精灵"它包含一个位图并知道如何绘制自己等。

图形类包含一个精灵对象。

MyClass

{<私有:

Sprite this_objects_sprite;

}

sprite类需要一个指向 ;视频表面没有办法围绕这个。指针是构造和绘制精灵对象所必需的。

我的问题是:

这个设计意味着我必须将指针传递给视频表面以构造函数中的myclass为例,例如

MyClass



私人:

Sprite this_objects_sprite;

公开:

MyClass(VideoSurface * s)



this_objects_sprite = new Sprite(VideoSurface) * s);

...

...

}

...

...

}

显然这意味着我的所有图形对象都与我正在使用的图形系统紧密耦合。显然(我认为)这是不可取的。

我在考虑安排事情,以便精灵不会在容器对象构造函数中自动实例化。 br />
也就是说,我构造我的对象然后调用一个函数来构造
对象精灵,例如,

MyClass c = new MyClass();

c.CreateSprite(VideoSurface * s);

然后这引发了其他问题,例如,如果我试图在创建它的精灵之前绘制对象,我该如何处理会发生什么。


Some of the classes in my app are graphical.

To encapsulate the graphical side of things I had created a class called
"sprite" which holds a bit map and knows how to draw itself etc.

The classes that are graphical contain a sprite object.

MyClass

{

private:

Sprite this_objects_sprite;

}

The sprite class requires a pointer to a "video surface". There is no way
around this. The pointer is required for the construction and drawing of
the sprite object.

My problem is this:

This design means that I have to pass the pointer to the video surface to
myclass in its constructor, e.g.

MyClass

{

private:

Sprite this_objects_sprite;

public:

MyClass(VideoSurface* s)

{

this_objects_sprite = new Sprite(VideoSurface* s);

...

...

}

...

...

}

Obviously this means all my graphical objects are tightly coupled to the
graphics system I am using. Also obviously (I think) is that this is
undesireable.

I was thinking of maybe arranging things so that the sprite is not
automatically instantiated in the container objects constructor.

That is, I construct my object THEN call a function to construct the
objects sprite, e.g.

MyClass c = new MyClass();

c.CreateSprite(VideoSurface* s);

But then this raised other issues like, how do I handle what happens if i
try to draw the object before creating its sprite.




一个简单的方法可能是:


继续使用MyClass ctor(适用于所有ctors)参数/>
指针,但总是给它一个默认值(例如0)。然后

让你的绘图函数在使用之前检查这个指针。


-Mike



A simple approach could be:

Continue to use a MyClass ctor (for all ctors) argument for your
pointer, but always give it a default value (e.g. 0). Then
have your draw functions check this pointer before using it.

-Mike


On Chri,2006年1月27日23:11:59 +0000,Chocawok写道:
On Fri, 27 Jan 2006 23:11:59 +0000, Chocawok wrote:

< snip>
图形类包含一个精灵对象。

MyClass

私密:

Sprite this_objects_sprite;

}


精灵类需要一个指向视频表面的指针。没有办法围绕这个。
指针是构建和绘制
精灵对象所必需的。



问题:当你说没有办法解决这个问题。时,你的意思是说当前的班级设计要求它,或者某些潜在的问题

需要你以这种方式设计课程?换句话说,你能不能改变Sprite类,以免在施工时取得VideoSurface

而是在抽奖时间?

我的问题是这样的:

这个设计意味着我必须将指针传递给视频表面到其构造函数中的myclass,例如

MyClass
<私人:

Sprite this_objects_sprite;

公开:

MyClass(VideoSurface * s)

{

this_objects_sprite = new Sprite(VideoSurface * s);


这意味着this_objects_sprite实际上是一个Sprite * (不是

Sprite)。

...

...

}






显然这意味着我的所有图形对象都紧密耦合到了
我在考虑安排事情,以便精灵不会在容器对象构造函数中自动实例化。 br />
也就是说,我构造我的对象然后调用一个函数来构造对象
精灵,例如,

MyClass c = new MyClass();

c.CreateSprite(VideoSurface * s);


这样可行。如果你可以改变Sprite的语义,你也可以

可能仍然允许构建精灵,但是有一个

" attach"稍后将其连接到视频表面的方法。

然后这引发了其他问题,例如,如果我在创建对象之前尝试绘制对象,我该如何处理会发生什么?精灵。
<snip>
The classes that are graphical contain a sprite object.

MyClass

{

private:

Sprite this_objects_sprite;

}

The sprite class requires a pointer to a "video surface". There is no way
around this.
The pointer is required for the construction and drawing of the
sprite object.


Question: when you say "There is no way around this.", do you mean that
the current class design requires it, or that some underlying issue
requires you to design the class this way? In other words, could you
change the Sprite class to not take the VideoSurface at construction time
but rather at draw time?

My problem is this:

This design means that I have to pass the pointer to the video surface to
myclass in its constructor, e.g.

MyClass

{

private:

Sprite this_objects_sprite;

public:

MyClass(VideoSurface* s)

{

this_objects_sprite = new Sprite(VideoSurface* s);
This implies that "this_objects_sprite" is actually a "Sprite *" (not
"Sprite").

...

...

}

...

...

}

Obviously this means all my graphical objects are tightly coupled to the
graphics system I am using. Also obviously (I think) is that this is
undesireable.

I was thinking of maybe arranging things so that the sprite is not
automatically instantiated in the container objects constructor.

That is, I construct my object THEN call a function to construct the objects
sprite, e.g.

MyClass c = new MyClass();

c.CreateSprite(VideoSurface* s);

That would work. If you can change Sprite''s semantics, you could also
potentially still allow the construction of the sprite, but have an
"attach" method to later connect it to a video surface.


But then this raised other issues like, how do I handle what happens if i
try to draw the object before creating its sprite.




如果精灵成员是指针,请在构造函数

中将其初始化为0并检查。另外,如果以某种方式画出,方法知道

视频表面(例如它作为参数传递),你可以在第一次尝试绘制时创建

精灵如果它没有'尚未创建。


- Jay



If the sprite member is a pointer, initialize it to 0 in the constructor
and check. Additionally, if somehow the "draw" method knows about the
video surface (e.g. it''s passed as a parameter), you could create the
sprite the first time you try to draw if it hasn''t been created yet.

- Jay


文章< 3N ****** **************@fe3.news.blueyonder.co.uk> ,

" Chocawok" <无**** @ nospam.com>写道:
In article <3N********************@fe3.news.blueyonder.co.uk> ,
"Chocawok" <no****@nospam.com> wrote:
显然这意味着我的所有图形对象都与我正在使用的
图形系统紧密耦合。显然(我认为)这是不可取的。


您的MyClass

对象中包含Sprite对象的事实是它与图形系统紧密耦合的原因。如果

你不想要那个耦合,你需要抽象出来。


如果你不能改变Sprite类,那么你将不得不使用

适配器。


class Image {

public:

virtual~ Image(){}

virtual void draw()= 0;

};


class SpriteAdaptor:public Image {

Sprite _sprite;

public:

SpriteAdaptor(VideoSurface * s):_ sprite(s){}

void draw(){

_sprite.draw();

}

};


class MyClass {

Image * _image;

public:

MyClass(Image * i):_ image(i){assert(i); }

//在某个成员函数中调用_image-> draw()

};

我在考虑安排这样的事情精灵没有在容器对象构造函数中自动实例化。

也就是说,我构造我的对象然后调用一个函数构造对象
精灵,例如

MyClass c = new MyClass();

c.CreateSprite(VideoSurface * s);
Obviously this means all my graphical objects are tightly coupled to the
graphics system I am using. Also obviously (I think) is that this is
undesireable.
The fact that you have a Sprite object contained within your MyClass
object is what is making it tightly coupled to the graphics system. If
you don''t want that coupling, you need to abstract it out.

If you can''t change the Sprite class, then you will have to use an
Adaptor.

class Image {
public:
virtual ~Image() { }
virtual void draw() = 0;
};

class SpriteAdaptor : public Image {
Sprite _sprite;
public:
SpriteAdaptor( VideoSurface* s ): _sprite( s ) { }
void draw() {
_sprite.draw();
}
};

class MyClass {
Image* _image;
public:
MyClass( Image* i ): _image( i ) { assert( i ); }
// call _image->draw() in some member-function
};
I was thinking of maybe arranging things so that the sprite is not
automatically instantiated in the container objects constructor.

That is, I construct my object THEN call a function to construct the objects
sprite, e.g.

MyClass c = new MyClass();

c.CreateSprite(VideoSurface* s);




你可以这样做,但在这种情况下仍然存在耦合。 MyClass有

包括Sprite标题和可能的VideoSurface标题为

井。



You can do that, but the coupling still exists in that case. MyClass has
to include the Sprite header and possibly the VideoSurface header as
well.


这篇关于紧密耦合的课堂设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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