样式问题:SetShutdown / GetShutdown .vs。 SetShutdown()被覆盖 [英] Style question: SetShutdown/GetShutdown .vs. SetShutdown() overridden

查看:74
本文介绍了样式问题:SetShutdown / GetShutdown .vs。 SetShutdown()被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于

类变量,设置setter和getter是很常见的,也是一个好主意。 IE


class MyClass

{

public:

void SetShutdown(const bool sd){关机= sd; };

bool GetShutdown(){return Shutdown};

private:

bool Shutdown;

};


我正在考虑的不是设置而是简单地重写:


class MyClass

{

public:

void shutdown(bool sd){Shutdown = sd; };

bool shutdown(){return Shutdown; };

私人:

bool关闭;

};


现在,这是严格的一个风格问题,它有点令人困惑

函数和变量's''之间的唯一区别。

但你个人而言,认为这种风格是个好主意,你喜欢它吗?


或者你能想到其他方式而不是SetXxxxx()和GetXxxxxx()吗?

解决方案

Jim Langston写道:

拥有制定者和吸气剂是常见的,而且通常是一个好主意
类变量。 IE



包括我在内的一些人认为设置者和吸气剂是一种设计气味,

问问自己为什么这些成员要求从外部访问,是吗? />
属于这个类,还是访问它们的那个?


-

Ian Collins。


" Ian Collins" < IA ****** @ hotmail.com>在消息中写道

news:11 *************** @ drone2-svc-skyt.qsi.net.nz ...

Jim Langston写道:

为类变量设置setter和getter是很常见的,也是一个好主意。 IE


包括我在内的一些人认为设置者和吸气剂是设计气味,请问自己为什么这些成员要求从外面访问,他们是否属于本课程,或者访问它们的那个?

- Ian Collins。




好​​吧,拿这个具体的例子说我是使用Shutdown for。这是一个

线程队列类,用于在工作线程(socket

连接)和主线程(服务器程序)之间进行通信。有一种方法可以将字符串推送到队列中(工作线程使用),这是一种从队列(主线程使用)中弹出
消息的方法varius线程

代码,以确保此类是线程安全的。甚至还有一种方法会将一个关闭方法发送到队列中,当poped将退出队列时,

相当不合理,我可能会添加。


添加变量shutdown只是因此主线程可以设置这个

变量(不需要锁定)所以工作线程可以定期检查它

优雅地关闭。


在我的意见中,这意味着这个变量确实属于这个类而且

需要一个方法来设置它,以及检查它的方法。是的,而不是简单的SetXxxx和GetXxxx我可以使用更多的语法方法名称

(SendShutdown()和IsShutdown()或其他东西)但Set和Get很常见。


现在,对于我使用的其他SetXxxx和GetXxxx实例,在一个图形中我使用了
类:


void CGUIElement :: SetLocation(unsigned long X,unsigned long Y)

{

BitmapX = X;

BitmapY = Y;

};


只需将位图移动到新位置即可。这可以用于移动窗户,拖动图标,等等。有人可能会说,

位图位置可以存储在设置它们的类中,但为什么呢?他们

是GUI对象本身的一部分。


来自同一GUI类的另一个例子是:


void CGUIElement :: SetOpen(bool OpenIt)

{

if(OpenIt&&!IsOpen)

{

CloseOnOpenNotifier.notifyObservers(dynamic_cast< Argument *>(

& OpenArgument(NotifyMsg)));

IsOpen = true;

OpenNotifier.notifyObservers(dynamic_cast< Argument *>(& OpenArgument(

NotifyMsg)));

}

else if(! OpenIt&& IsOpen)

{

IsOpen = false;

CloseNotifier.notifyObservers(dynamic_cast< Argument *>(& OpenArgument) (

NotifyMsg)));

}

};


如您所见这不仅仅是一个变量的简单赋值。

这个setter实际上确实做了很多,但仍然是一个制定者,而且需要让GUI本身知道它是打开的。 (

实例的窗口)但是利用可观察的模型告诉观察者它打开或关闭了



有我可以给你的许多其他例子,我不认为Setters

和Getters只是因为它们存在而必然是一种设计气味。他们

可能会给出更多appropo名称(SetOpen可以分解为

打开和关闭)但事实仍然是他们仍然是Setters和

Getters。


* Jim Langston:


添加变量shutdown只是因此主线程可以设置这个
变量(不需要锁定)所以工作线程可以定期检查它以优雅地关闭。




你会在哪些情况下调用


thread.SetShutdown(false);





我认为,要么这将是多余的,因为你知道关闭已经早先没有设置为真的
,或者,调用将包含在可能失败的一些

同步中,或者,调用可能默默地没有效果

因为线程已经终止(然后没有用,除非

你真的热衷于重用一个可能非te的rminated线程)。


在足够高的抽象级别,同样经常是

的情况:暴露一个值使得客户端代码和类实现<通过删除对客户端和实现都有用的约束


比必要的更复杂和不安全。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


It is common, and generally a good idea, to have setters and getters for
class variables. I.E.

class MyClass
{
public:
void SetShutdown( const bool sd ) { Shutdown = sd; };
bool GetShutdown() { return Shutdown };
private:
bool Shutdown;
};

What I''m contemplating is instead of set and get simply overriding:

class MyClass
{
public:
void shutdown( bool sd ) { Shutdown = sd; };
bool shutdown() { return Shutdown; };
private:
bool Shutdown;
};

Now, this is strictly a style question, and it gets a little confusing with
the only difference between the function and the variable the capital ''S''.
But do you, personally, think this style is a good idea and do you like it?

Or can you think of some other way instead of SetXxxxx() and GetXxxxxx() ?


解决方案

Jim Langston wrote:

It is common, and generally a good idea, to have setters and getters for
class variables. I.E.


Some, me included, consider setters and getters to be a design smell,
ask yourself why these members requite access from outside, do they
belong in this class, or the one that access them?

--
Ian Collins.


"Ian Collins" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...

Jim Langston wrote:

It is common, and generally a good idea, to have setters and getters for
class variables. I.E.


Some, me included, consider setters and getters to be a design smell, ask
yourself why these members requite access from outside, do they belong in
this class, or the one that access them?

--
Ian Collins.



Well, take this specific example that I''m using Shutdown for. It''s for a
threaded queue class to communicate between the worker threads (socket
connections) and the main thread (server program). There is a method to
push strings onto the queue (which the worker threads use), a method to pop
messages from the queue (which the main thread uses) with varius threading
code to ensure this class is thread safe. There is even a method that will
send a shutdown method onto the queue, that when poped will exit the queue,
rather ungracefully I might add.

Adding the variable shutdown is simply so the main thread can set this
variable (no locking needed) so the worker threads can check it periodically
to shut down gracefully.

That, in my opionion, means that this variable does belong in the class and
there needs to be a method to set it, and a method to check it. Yes, rather
than a simple SetXxxx and GetXxxx I could use more syntactical method names
( SendShutdown() and IsShutdown() or something ) but Set and Get are common.

Now, for other instances of SetXxxx and GetXxxx I have used, in one graphic
class I use:

void CGUIElement::SetLocation( unsigned long X, unsigned long Y )
{
BitmapX = X;
BitmapY = Y;
};

Which simply moves the bitmap to a new location. This could be used for
moving windows around, dragging icons, whatever. One could argue that the
bitmap locations could be stored in the class that sets them, but why? They
are part of the GUI object itself.

Another example from this same GUI class is:

void CGUIElement::SetOpen( bool OpenIt )
{
if ( OpenIt && !IsOpen )
{
CloseOnOpenNotifier.notifyObservers( dynamic_cast<Argument*>(
&OpenArgument( NotifyMsg ) ) );
IsOpen = true;
OpenNotifier.notifyObservers( dynamic_cast<Argument*>( &OpenArgument(
NotifyMsg ) ) );
}
else if ( !OpenIt && IsOpen )
{
IsOpen = false;
CloseNotifier.notifyObservers( dynamic_cast<Argument*>( &OpenArgument(
NotifyMsg ) ) );
}
};

and as you can see this is more than a simple assignment of a variable.
This "setter" actually does quite a bit, but is still a setter, and is
needed not only to let the GUI itself know that it''s "open" (a window for
instance) but utilizing the observable model inform the watchers that it
opened or closed.

There are many other examples I can give you, and I don''t think that Setters
and Getters are neccessarily a design smell just because they exist. They
may be given more appropo names perhaps (SetOpen could be broken up into
Open and Close for instance) but the fact remains they are still Setters and
Getters.


* Jim Langston:


Adding the variable shutdown is simply so the main thread can set this
variable (no locking needed) so the worker threads can check it periodically
to shut down gracefully.



In which cases would you ever call

thread.SetShutdown( false );

?

As I see it, either it will be redundant because you know shutdown has
not been set to true earlier, or, the call will be wrapped in some
synchronization that may fail, or, the call may silently have no effect
because the thread has already terminated (and then be of no use unless
you''re really keen on reusing a possibly non-terminated thread).

At a sufficient high level of abstraction the same is very often the
case: exposing a value makes both client code and class implementation
much more complex and unsafe than necessary, by removing constraints
that are useful both to the client side and the implementation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于样式问题:SetShutdown / GetShutdown .vs。 SetShutdown()被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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