473,320 Members | 1,848 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,320 software developers and data experts.

Factory and Generic class: are wildcards necessary?

Hi all,
Could anybody suggest what should be the return type of the method Create in
the Factory class?
In Java I would have used the wildcards, but here in C# apparently there is
no way do do it?!?
public class B<T>
{
}

public class D1 : B<int>
{
}

public class D2 : B<float>
{
}

public static class Factory
{
public static B<?> Create(string _name)
{
if (_name.Equals("D1"))
{
return new D1();
}
else
{
return new D2();
}
}
}

Jock


Feb 27 '06 #1
7 4560
Hi
from : http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx
Not surprisingly, he'd like very much to be able to add this to C#, but
there are additional complications in C# that make that very difficult
(in Java, wildcard types were added at the same time as generics - in
C# we've got a significant compatability problem, plus in Java generics
exist only in the compiler, not in the run-time type system like in the
CLR). So, unfortunately, I wouldn't hold your breath waiting for this
in C# 3.0

A.Hadi

Feb 27 '06 #2
You could do something like:

public class B<T>

{

}

public class D1<T> : B<T>

{

}

public class D2<T> : B<T>

{

}

public static class Factory

{

public static B<int> CreateInt()

{

return new D1<int>();

}

public static B<float> CreateFloat()

{

return new D2<float>();

}

}
You want to know the type at compile time. That is kinda the point of
generics.

--
William Stacey [MVP]

"Jock" <Jo**@nospam.com> wrote in message
news:45********************@twister2.libero.it...
| Hi all,
| Could anybody suggest what should be the return type of the method Create
in
| the Factory class?
| In Java I would have used the wildcards, but here in C# apparently there
is
| no way do do it?!?
|
|
| public class B<T>
| {
| }
|
| public class D1 : B<int>
| {
| }
|
| public class D2 : B<float>
| {
| }
|
| public static class Factory
| {
| public static B<?> Create(string _name)
| {
| if (_name.Equals("D1"))
| {
| return new D1();
| }
| else
| {
| return new D2();
| }
| }
| }
|
| Jock
|
|
|
|
Feb 27 '06 #3
This is a silly solution, but it works. Sometimes you'll have to add some
polymorphic methods to AnyB to make sure you can get the values from its
inheritors without explicit casts, and sometimes it is not even possible to
apply this pattern, but it's the only one I know of that works.

class AnyB
{
protected AnyB() {}
}

class B<T> : AnyB {...}

class D1 : B<int> {...}
class D2 : B<string> {...}

public class Factory
{
public static AnyB Create(string name)
{
if (name.Equals("D1"))
{
return new D1();
}
else
{
return new D2();
}
}
}

"Jock" <Jo**@nospam.com> wrote in message
news:45********************@twister2.libero.it...
Hi all,
Could anybody suggest what should be the return type of the method Create
in the Factory class?
In Java I would have used the wildcards, but here in C# apparently there
is no way do do it?!?
public class B<T>
{
}

public class D1 : B<int>
{
}

public class D2 : B<float>
{
}

public static class Factory
{
public static B<?> Create(string _name)
{
if (_name.Equals("D1"))
{
return new D1();
}
else
{
return new D2();
}
}
}

Jock

Feb 27 '06 #4
>You could do something like:

public class B<T>

{

}

public class D1<T> : B<T>

{

}

public class D2<T> : B<T>

{

}

public static class Factory

{

public static B<int> CreateInt()

{

return new D1<int>();

}

public static B<float> CreateFloat()

{

return new D2<float>();

}

}
You want to know the type at compile time. That is kinda the point of
generics.

William,

this is not the aim of a factory that should be called without any
knoledge of the type of the object it is going to create. On your
solution the user of factory is supposed to know in advance which type
of object to create, indeed is up to the user to choose between
CreateFloat() or CreateInt() or CreateWhatEver().
Thanks anyway

Jock
Feb 28 '06 #5
> This is a silly solution, but it works. Sometimes you'll have to add some
polymorphic methods to AnyB to make sure you can get the values from its
inheritors without explicit casts, and sometimes it is not even possible to apply this pattern, but it's the only one I know of that works.

class AnyB
{
protected AnyB() {}
}

class B<T> : AnyB {...}

class D1 : B<int> {...}
class D2 : B<string> {...}

public class Factory
{
public static AnyB Create(string name)
{
if (name.Equals("D1"))
{
return new D1();
}
else
{
return new D2();
}
}
}


Ok it works but it forces to have a super class of B<T> which might be
inappropriate :)

Anyway I belive that without wildcards or something like that it is
impossible

Thanks
Jock


Feb 28 '06 #6
> Hi
from : http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx Not surprisingly, he'd like very much to be able to add this to C#, but there are additional complications in C# that make that very difficult (in Java, wildcard types were added at the same time as generics - in C# we've got a significant compatability problem, plus in Java generics exist only in the compiler, not in the run-time type system like in the CLR). So, unfortunately, I wouldn't hold your breath waiting for this in C# 3.0 A.Hadi


Thanks for the good link!

Do you know what kind of improvement Generics will have in C# 3.0?

Do you know when C# 3.0 it is going to be released?

Thanks

Jock
Feb 28 '06 #7
| this is not the aim of a factory that should be called without any
| knoledge of the type of the object it is going to create.

In efffect, that is exactly what your doing anyway. Your just using a
string label instead of a clr type. You could forget about generics on the
types and use containment of your generics such as:

public abstract class B

{

public static B Create(string name)

{

if (name == "D")

return new D();

else if (name == "C")

return new C();

else

throw new ArgumentException("name invalid.");

}

}

public class D : B

{

List<int> list = new List<int>();

}

public class C : B

{

List<float> list = new List<float>();

}
Mar 1 '06 #8

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
10
by: Mark | last post by:
I have an abstract class, and a set of classes that inherit from my abstract class. The fact that it is abstract is likely irrelevant. I have a static factory method in my abstract class that...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
0
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms {...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
6
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called...
8
by: googlegroups | last post by:
Hi, I need to parse a binary file produced by an embedded system, whose content consists in a set of events laid-out like this: <event 1<data 1<event 2<data 2... <event n<data n> Every...
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...
5
by: Agrona | last post by:
I found this thread: http://www.thescripts.com/forum/thread233505.html only marginally helpful. It seems that going through the callstack is the only way to determine which function may have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.