473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Generics with singleton

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();
}
Nov 17 '05 #1
10 1678
"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
Nov 17 '05 #2
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" <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

Nov 17 '05 #3
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" <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

Nov 17 '05 #4
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
79**********************************@microsoft.com...

| 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>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 17 '05 #5
Joanna Carter [TeamB] <jo****@not.for.spam> wrote:
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
79**********************************@microsoft.com...

| 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>'

Sorry, try this :

public virtual int Save()
{
return 0;
}


And the other change needed (when I tested it earlier :) is in the
field initialization:

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

(The second <T> was missing before.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Hi

Have been looking around and found this article.
http://www.codeproject.com/csharp/Ge...sp#xx1183285xx
And after some work I came up with this(below). It looks ok as far as I can
see, can you find anything wrong with it?

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();
}

public class ProductAdapter : AdapterBase<ProductAdapter>
{
...
}

public class AdapterProvider<T> where T : new()
{
private AdapterProvider()
{
}

public static T Instance
{
get
{
return AdapterFactory.instance;
}
}

class AdapterFactory
{
static AdapterFactory()
{
}

internal static readonly T instance = new T();
}
}

public static class Adapter
{
public static ProductAdapter Product
{
get
{
return AdapterProvider<ProductAdapter>.Instance;
}
}
}
"Joanna Carter [TeamB]" wrote:
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
79**********************************@microsoft.com...

| 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>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Nov 17 '05 #7
And to call this I would use:
ProductAdapter pa = Adapter.Product;
Response.Write(pa.Count.ToString());
"Joanna Carter [TeamB]" wrote:
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
79**********************************@microsoft.com...

| 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>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Nov 17 '05 #8
Didn't actually test the code. The errors in it are...

There is also a Product class:
public class Product
{
public int Save()
{
return Adapter.Product.Save(this);
}
}

The ProductAdapter is of type Product:
public class ProductAdapter : AdapterBase<Product>
{...}

And to call it:
Product p = new Product();
Response.Write(p.Save().ToString());

So, those this setup look ok?
"Joanna Carter [TeamB]" wrote:
"Senna" <Se***@discussions.microsoft.com> a écrit dans le message de news:
79**********************************@microsoft.com...

| 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>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Nov 17 '05 #9
"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...

And the other change needed (when I tested it earlier :) is in the
field initialization:

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

(The second <T> was missing before.)

Yup, well caught; I was dashing out of the door when I wrote the sample
without even using VS :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 17 '05 #10
Joanna Carter [TeamB] <jo****@not.for.spam> wrote:
Yup, well caught; I was dashing out of the door when I wrote the sample
without even using VS :-)


I don't usually use VS for newsgroup programs :)

(That's not to say I always remember to test compile it either though -
not by a long chalk!)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
1
by: sloan | last post by:
I have 1. An interface (generic). I simplified it to have one method. 2. A class which implements this interface. This class is also a Singleton. 3. A factory, (which right now only returns...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.