单例的泛型 [英] Generics with singleton

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

问题描述




有这样的单身课程。


公共密封课程ProductAdapter

{

private static readonly ProductAdapter instance = new

ProductAdapter();

private ProductAdapter(){}


public static ProductAdapter Instance

{

get

{

返回实例;

}

}


public int保存()

{

返回0;

}

}


但是会有更多的xxxAdapter类,所以我想知道如何使用

泛型有一个抽象的AdapterBase类,如下所示,但作为一个

单例。


公共抽象类AdapterBase< T>

{

public abstract int Save(T t);


public abstract void Update(T t);


public abstract void删除(int id);


public abstract void DeleteAll(int id);


public abstract T GetObject();


public abstract List< T> GetCollection();

}

Hi

Have a singleton class like this.

public sealed class ProductAdapter
{
private static readonly ProductAdapter instance = new
ProductAdapter();
private ProductAdapter(){}

public static ProductAdapter Instance
{
get
{
return instance;
}
}

public int Save()
{
return 0;
}
}

But there will be many more xxxAdapter classes so I wonder how I can use
generics to have a abstract AdapterBase class, like below, but as a
singleton.

public abstract class AdapterBase<T>
{
public abstract int Save(T t);

public abstract void Update(T t);

public abstract void Delete(int id);

public abstract void DeleteAll(int id);

public abstract T GetObject();

public abstract List<T> GetCollection();
}

推荐答案

" Senna" <硒*** @ discussions.microsoft.com> écritdansle message de news:
4F ********************************** @ microsoft.com ...


|有这样的单身课。

|

|公共密封类ProductAdapter

| {

| private static readonly ProductAdapter instance = new

| ProductAdapter();

| private ProductAdapter(){}

|

| public static ProductAdapter实例

| {

|得到

| {

|返回实例;

| }

| }

|

| public int Save()

| {

|返回0;

| }

| }

|

|但是会有更多的xxxAdapter类,所以我想知道如何使用

|泛型有一个抽象的AdapterBase类,如下所示,但作为一个

|单身。


我会使用类似下面的内容(未经测试)


公共类适配器< T>

{

private static readonly Adapter< T> instance = new Adapter();


private Adapter(){}

public static Adapter< T>实例

{

get

{

返回实例;

}

}


public abstract int保存()

{

返回0;

}

}


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
4F**********************************@microsoft.com...

| Have a singleton class like this.
|
| public sealed class ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
| get
| {
| return instance;
| }
| }
|
| public int Save()
| {
| return 0;
| }
| }
|
| But there will be many more xxxAdapter classes so I wonder how I can use
| generics to have a abstract AdapterBase class, like below, but as a
| singleton.

I would use something like the following (not tested)

public class Adapter<T>
{
private static readonly Adapter<T> instance = new Adapter();

private Adapter(){}

public static Adapter<T> Instance
{
get
{
return instance;
}
}

public abstract int Save()
{
return 0;
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


嗨Joanna


感谢您的帮助。我正在去参加生日派对的路上,所以我必须测试你的

代码并稍后回复。 :)


干杯

" Joanna Carter [TeamB]"写道:
Hi Joanna

Thanks for helping. I''m on my way to a birthday party so I have to test your
code and respond to it later. :)

Cheers
"Joanna Carter [TeamB]" wrote:
" Senna" <硒*** @ discussions.microsoft.com> a ??? crit dans le message de news:
4F ************** @微软。 com ......

|有这样的单身课程。
|
|公共密封类ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
|得到
| {
|返回实例;
| }
| }
|
| public int Save()
| {
|返回0;
| }
| }
|
|但是会有更多的xxxAdapter类,所以我想知道如何使用
|泛型有一个抽象的AdapterBase类,如下所示,但作为一个
|单身。

我会使用类似下面的内容(未经测试)

公共类适配器< T>
私有静态只读适配器< T> ; instance = new Adapter();

私有Adapter(){}
公共静态适配器< T>实例
{
获得
返回实例;
}

公共抽象int保存()
{
返回0;
}

Joanna

-
Joanna Carter [TeamB] 顾问软件工程师
"Senna" <Se***@discussions.microsoft.com> a ??crit dans le message de news:
4F**********************************@microsoft.com...

| Have a singleton class like this.
|
| public sealed class ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
| get
| {
| return instance;
| }
| }
|
| public int Save()
| {
| return 0;
| }
| }
|
| But there will be many more xxxAdapter classes so I wonder how I can use
| generics to have a abstract AdapterBase class, like below, but as a
| singleton.

I would use something like the following (not tested)

public class Adapter<T>
{
private static readonly Adapter<T> instance = new Adapter();

private Adapter(){}

public static Adapter<T> Instance
{
get
{
return instance;
}
}

public abstract int Save()
{
return 0;
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer



嗨再次


得到此错误。


错误1 c:\ ... \ ProductAdapter.cs(61):错误CS0513:''适配器< T> .Save()''是

摘要但它包含在非抽象类''适配器< T>''/ b $ b" Joanna Carter [TeamB]"写道:
Hi again

Get this error.

Error 1 c:\...\ProductAdapter.cs(61): error CS0513: ''Adapter<T>.Save()'' is
abstract but it is contained in nonabstract class ''Adapter<T>''
"Joanna Carter [TeamB]" wrote:
" Senna" <硒*** @ discussions.microsoft.com> a ??? crit dans le message de news:
4F ************** @微软。 com ......

|有这样的单身课程。
|
|公共密封类ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
|得到
| {
|返回实例;
| }
| }
|
| public int Save()
| {
|返回0;
| }
| }
|
|但是会有更多的xxxAdapter类,所以我想知道如何使用
|泛型有一个抽象的AdapterBase类,如下所示,但作为一个
|单身。

我会使用类似下面的内容(未经测试)

公共类适配器< T>
私有静态只读适配器< T> ; instance = new Adapter();

私有Adapter(){}
公共静态适配器< T>实例
{
获得
返回实例;
}

公共抽象int保存()
{
返回0;
}

Joanna

-
Joanna Carter [TeamB] 顾问软件工程师
"Senna" <Se***@discussions.microsoft.com> a ??crit dans le message de news:
4F**********************************@microsoft.com...

| Have a singleton class like this.
|
| public sealed class ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
| get
| {
| return instance;
| }
| }
|
| public int Save()
| {
| return 0;
| }
| }
|
| But there will be many more xxxAdapter classes so I wonder how I can use
| generics to have a abstract AdapterBase class, like below, but as a
| singleton.

I would use something like the following (not tested)

public class Adapter<T>
{
private static readonly Adapter<T> instance = new Adapter();

private Adapter(){}

public static Adapter<T> Instance
{
get
{
return instance;
}
}

public abstract int Save()
{
return 0;
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer



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

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