am a bit baffled as to why someone would take this
approach in all of our classes within the project I am
currently working on. Maybe someone else could attempt to
explain the following approach:

public class Class1 : IClass1
{
protected Class1()
{
}
public static IClass1 GetInstance()
{
return (IClass1) new Class1();
}
}

For every class in our system, there is an interface
defined. Instead of using a constructor, we use a
GetInstance method that calls new on our class but casts
the return value to the interface's type.

What advantages does this design approach have?

Re: interfaces overused or used correctly? by Jon

Jon
Thu Jan 08 17:01:29 CST 2004

Big <nymrbig@aol.com> wrote:
> am a bit baffled as to why someone would take this
> approach in all of our classes within the project I am
> currently working on. Maybe someone else could attempt to
> explain the following approach:
>
> public class Class1 : IClass1
> {
> protected Class1()
> {
> }
> public static IClass1 GetInstance()
> {
> return (IClass1) new Class1();
> }
> }
>
> For every class in our system, there is an interface
> defined. Instead of using a constructor, we use a
> GetInstance method that calls new on our class but casts
> the return value to the interface's type.
>
> What advantages does this design approach have?

Well, this factory pattern is presumably meant to allow flexibility for
later implementations. Doing it for every class is silly though.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: interfaces overused or used correctly? by Big

Big
Thu Jan 08 17:10:55 CST 2004

Thanks Jon. I appreciate your input very much.

Where is the flexibility gained? In the GetInstance method
due to the fact you could perform other operations before
instantiation or in the fact that it returns an interface
pointer?

Also, wont inherited classes break if I add a new method
to the base class interface?


>-----Original Message-----
>Big <nymrbig@aol.com> wrote:
>> am a bit baffled as to why someone would take this
>> approach in all of our classes within the project I am
>> currently working on. Maybe someone else could attempt
to
>> explain the following approach:
>>
>> public class Class1 : IClass1
>> {
>> protected Class1()
>> {
>> }
>> public static IClass1 GetInstance()
>> {
>> return (IClass1) new Class1();
>> }
>> }
>>
>> For every class in our system, there is an interface
>> defined. Instead of using a constructor, we use a
>> GetInstance method that calls new on our class but
casts
>> the return value to the interface's type.
>>
>> What advantages does this design approach have?
>
>Well, this factory pattern is presumably meant to allow
flexibility for
>later implementations. Doing it for every class is silly
though.
>
>--
>Jon Skeet - <skeet@pobox.com>
>http://www.pobox.com/~skeet
>If replying to the group, please do not mail me too
>.
>

Re: interfaces overused or used correctly? by Jon

Jon
Thu Jan 08 17:29:18 CST 2004

Big <nymrbig@aol.com> wrote:
> Thanks Jon. I appreciate your input very much.
>
> Where is the flexibility gained? In the GetInstance method
> due to the fact you could perform other operations before
> instantiation or in the fact that it returns an interface
> pointer?

It's that there's nothing to stop Class1.GetInstance later returning a
completely different implementation - or possibly the same instance
every time (for a singleton) or possibly pooling instances, etc.

> Also, wont inherited classes break if I add a new method
> to the base class interface?

Not sure what you mean by that. Could you give an example?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: interfaces overused or used correctly? by Big

Big
Thu Jan 08 18:19:52 CST 2004

Thanks Jon. I do understand the power of the Factory
pattern here as you have described in your last post.

What I don't understand is why someone would chose to cast
the return value to the classes base interface? Isn't this
redundant since all classes have a public interface
composed of all public members. What advantage does the
casting in the GetInstance method provide?
----> return (IClass1) new Class1();


>-----Original Message-----
>Big <nymrbig@aol.com> wrote:
>> Thanks Jon. I appreciate your input very much.
>>
>> Where is the flexibility gained? In the GetInstance
method
>> due to the fact you could perform other operations
before
>> instantiation or in the fact that it returns an
interface
>> pointer?
>
>It's that there's nothing to stop Class1.GetInstance
later returning a
>completely different implementation - or possibly the
same instance
>every time (for a singleton) or possibly pooling
instances, etc.
>
>> Also, wont inherited classes break if I add a new
method
>> to the base class interface?
>
>Not sure what you mean by that. Could you give an example?
>
>--
>Jon Skeet - <skeet@pobox.com>
>http://www.pobox.com/~skeet
>If replying to the group, please do not mail me too
>.
>

Re: interfaces overused or used correctly? by Manoj

Manoj
Thu Jan 08 21:43:21 CST 2004

This design is reminiscent of the way COM worked( coCreateInstance). -
Factory pattern. From what I see, this design is applicable if you are
adopting SingleTon pattern where you have only a single instance ever
created. The constructor would need to be private in that case. Apart from
Singletons I dont see any other purpose of this design.

--
HTH,
Manoj G [.NET MVP]
Site: http://www15.brinkster.com/manoj4dotnet
Blog: http://msmvps.com/manoj/
"Big" <nymrbig@aol.com> wrote in message
news:03cd01c3d646$4d399520$a601280a@phx.gbl...
> Thanks Jon. I do understand the power of the Factory
> pattern here as you have described in your last post.
>
> What I don't understand is why someone would chose to cast
> the return value to the classes base interface? Isn't this
> redundant since all classes have a public interface
> composed of all public members. What advantage does the
> casting in the GetInstance method provide?
> ----> return (IClass1) new Class1();
>
>
> >-----Original Message-----
> >Big <nymrbig@aol.com> wrote:
> >> Thanks Jon. I appreciate your input very much.
> >>
> >> Where is the flexibility gained? In the GetInstance
> method
> >> due to the fact you could perform other operations
> before
> >> instantiation or in the fact that it returns an
> interface
> >> pointer?
> >
> >It's that there's nothing to stop Class1.GetInstance
> later returning a
> >completely different implementation - or possibly the
> same instance
> >every time (for a singleton) or possibly pooling
> instances, etc.
> >
> >> Also, wont inherited classes break if I add a new
> method
> >> to the base class interface?
> >
> >Not sure what you mean by that. Could you give an example?
> >
> >--
> >Jon Skeet - <skeet@pobox.com>
> >http://www.pobox.com/~skeet
> >If replying to the group, please do not mail me too
> >.
> >



Re: interfaces overused or used correctly? by Jon

Jon
Fri Jan 09 01:24:40 CST 2004

Big <nymrbig@aol.com> wrote:
> Thanks Jon. I do understand the power of the Factory
> pattern here as you have described in your last post.
>
> What I don't understand is why someone would chose to cast
> the return value to the classes base interface? Isn't this
> redundant since all classes have a public interface
> composed of all public members. What advantage does the
> casting in the GetInstance method provide?
> ----> return (IClass1) new Class1();

Indeed, that cast is redundant. No idea why it's there.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: interfaces overused or used correctly? by Jon

Jon
Fri Jan 09 01:25:51 CST 2004

Manoj G [MVP] <manuthegreat@hotmail.com> wrote:
> This design is reminiscent of the way COM worked( coCreateInstance). -
> Factory pattern. From what I see, this design is applicable if you are
> adopting SingleTon pattern where you have only a single instance ever
> created. The constructor would need to be private in that case. Apart from
> Singletons I dont see any other purpose of this design.

There are other uses for factories apart from singletons. Pooling and
later implementation changes (so that the client interface is still
exactly the same, but a different implementation class is actually
used) are just two of them.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: interfaces overused or used correctly? by Daniel

Daniel
Fri Jan 09 09:10:53 CST 2004

Could this possibly offer any protection from future use of the class by a
language that wasn't so strongly typed? I mean in C# if you calling a
method defined as:
public static IClass1 GetInstance()
you obviously have to place the result in a IClass1 type. But could you in
a less strongly typed language potentially do
object someObject = Class1.GetInstance()
and then manipulate someObject in unintended ways because it could see
Class1.SecretMethod() for example (which wasn't part of IClass1)?

As I'm typing this I'm thinking no, of course not, that isn't the way it
works since GetInstance() is going to return an IClass1 regardless, so I
guess I just need verification I'm thinking correctly at this point. Maybe
the original coder didn't understand that fully.

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a68679228567dd6989d6b@msnews.microsoft.com...
> Big <nymrbig@aol.com> wrote:
> > Thanks Jon. I do understand the power of the Factory
> > pattern here as you have described in your last post.
> >
> > What I don't understand is why someone would chose to cast
> > the return value to the classes base interface? Isn't this
> > redundant since all classes have a public interface
> > composed of all public members. What advantage does the
> > casting in the GetInstance method provide?
> > ----> return (IClass1) new Class1();
>
> Indeed, that cast is redundant. No idea why it's there.
>