单身人士接口 [英] Singleton & Interfaces

查看:98
本文介绍了单身人士接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的类singleton1和singleton2,它们都是

单身。这两个类都实现了相同的功能。因此,我希望
有一个包含这些功能的界面。问题

是一个接口不允许静态函数和抽象

类在我的情况下不起作用,因为多重继承不是

允许。那么我该怎么做呢?


谢谢

I have two different classes singleton1 and singleton2 that both are
singletons. Both classes implement the same functions. Therefore, I
would like to have an interface containing these functions. The problem
is that an interface does not allow static functions and an abstract
class does not work in my case because multiple inheritence is not
allowed. So how do I go after doing this?

Thanks

推荐答案

一种方法定义一个看起来不像单身的单例

是一个普通的类必须被实例化才能使用但是所有的私有成员变量都是b $ b静态的。这不是经典的单身

模式,但它的工作方式相同。


-

Jeffrey Hornby

Hornby Consulting,Inc。


" rob"写道:
One way to define a singleton that doesn''t necessarily look like a singleton
is to have a normal class that has to be instantiated to be used but have all
the private member variables static. This isn''t the classic singleton
pattern but it works in the same way.

--
Jeffrey Hornby
Hornby Consulting, Inc.

"rob" wrote:
我有两个不同的类singleton1和singleton2都是
单例。这两个类都实现了相同的功能。因此,我希望有一个包含这些功能的界面。问题
是一个接口不允许静态函数,抽象的类在我的情况下不起作用,因为不允许多重继承。那么我该怎么做呢?

I have two different classes singleton1 and singleton2 that both are
singletons. Both classes implement the same functions. Therefore, I
would like to have an interface containing these functions. The problem
is that an interface does not allow static functions and an abstract
class does not work in my case because multiple inheritence is not
allowed. So how do I go after doing this?

Thanks



Rob,

如果您真正实现了单例模式,那么您将为每个单例暴露一个实例。这样可以轻松暴露

界面。你只需选择你要返回的单身,

并返回界面实现。


当然,你这样做而不是静态成员。 />

希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


"抢劫QUOT&; < RM ******* @ yahoo.com>在消息中写道

news:11 ********************* @ b68g2000cwa.googlegro ups.com ...
Rob,

If you were truly implementing the singleton pattern, you would be
exposing an instance for each singleton. This would make exposing an
interface easy. You would just choose which singleton you are returning,
and return the interface implementation.

Of course, you do this instead of having static members.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rob" <rm*******@yahoo.com> wrote in message
news:11*********************@b68g2000cwa.googlegro ups.com...
我有两个不同的类singleton1和singleton2都是
单例。这两个类都实现了相同的功能。因此,我希望有一个包含这些功能的界面。问题
是一个接口不允许静态函数,抽象的类在我的情况下不起作用,因为不允许多重继承。那么我该怎么做呢?

谢谢
I have two different classes singleton1 and singleton2 that both are
singletons. Both classes implement the same functions. Therefore, I
would like to have an interface containing these functions. The problem
is that an interface does not allow static functions and an abstract
class does not work in my case because multiple inheritence is not
allowed. So how do I go after doing this?

Thanks



是的,每个单身人士都会公开一个单独的实例。

不幸的是,我并没有真正遵循你的建议。你能详细说明这个吗?


谢谢


Nicholas Paldino [.NET / C#MVP]写道:
Yes, each of my singletons exposes a separate instance of itself.
Unfortunately, I don''t really follow what your suggestion is. Could you
elaborate on this?

Thanks

Nicholas Paldino [.NET/C# MVP] wrote:
Rob,

如果您真正实现了单例模式,那么您将为每个单例暴露一个实例。这样可以轻松地展示
界面。你只需选择你要返回的单身,
并返回界面实现。

当然,你这样做而不是静态成员。

希望这个帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

抢劫 < RM ******* @ yahoo.com>在消息中写道
新闻:11 ********************* @ b68g2000cwa.googlegro ups.com ...
Rob,

If you were truly implementing the singleton pattern, you would be
exposing an instance for each singleton. This would make exposing an
interface easy. You would just choose which singleton you are returning,
and return the interface implementation.

Of course, you do this instead of having static members.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rob" <rm*******@yahoo.com> wrote in message
news:11*********************@b68g2000cwa.googlegro ups.com...
I有两个不同的类singleton1和singleton2都是
单例。这两个类都实现了相同的功能。因此,我希望有一个包含这些功能的界面。问题
是一个接口不允许静态函数,抽象的类在我的情况下不起作用,因为不允许多重继承。那么我该怎么做呢?

谢谢
I have two different classes singleton1 and singleton2 that both are
singletons. Both classes implement the same functions. Therefore, I
would like to have an interface containing these functions. The problem
is that an interface does not allow static functions and an abstract
class does not work in my case because multiple inheritence is not
allowed. So how do I go after doing this?

Thanks






这篇关于单身人士接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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