Re: Factory method



The base class should not be bound to the list of possible inheriting
children. The base class must not create the children. That defeats the
purpose.

The child object can have a public constructor. That doesn't mean that you
will use it. If you don't want a public constructor, then define a Factory
Method in the base class and, in the child classes, implement the factory
method to return the object.

You won't be able to "enforce" that the child object cannot have a public
constructor. Why would you want to? But you can provide a mechanism that
the child class must implement that returns an object that derives from the
base type. The users of the class will always use this method, which means
you no longer have to worry about it. If a child class shouldn't have a
public constructor, write it so that the contstructor is private.

The constructor in your subclasses can call the constructor in your base
class. See this topic from Jon's site:
http://www.yoda.arachsys.com/csharp/constructors.html

To implement a factory method, in the base class define an abstract method
that returns an object of the base type.

<Warning -- uncompiled air code>

public abstract myBaseWidget
{
private myBaseWidget(int myParam)
{
}
public abstract myBase CreateWidget();
}

public myChildWidget : myBaseWidget
{
private myChildWidget() : base(4)
{
// go do constructor things
}
public myChildWidget CreateWidget()
{
// this is your factory method!
return new myChildWidget();
}
}



--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Mark" <Mark@xxxxxxxxxxx> wrote in message
news:uQz4Cf0RFHA.2348@xxxxxxxxxxxxxxxxxxxxxxx
>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 creates subclasses. The
>constructor in my subclasses must be able to call the constructor in my
>base class. For the factory method in my abstract class to call the
>constructor on my subclasses, the constructor in the subclass MUST be
>public (or internal). They cannot be private or protected. However,
>doen't that basically defeat the purpose of factory? I'd love to be able to
>make all these constructors private or similar so that someone can't create
>subclass instances without using the Factory. If this was all in a
>separate DLL, I suppose I'd be saved by "internal", but we have small team
>of developers working on projects where that type of seperation really
>shouldn't be necessary.
>
> Suggestions? Code sample below ...
>
> Thanks in advance.
>
> Mark
>
>
> public abstract class MyBase
> {
> protected string first;
>
> public MyBase(string first )
> {
> this.first = first;
> }
>
> public static MyBase Factory(int stateId)
> {
> switch (stateId)
> {
> case 700:
> return new MySubClass1(first);
> default:
> throw new Exception("Bogus code!");
> }
> }
> }
>
>


.



Relevant Pages

  • Re: Create objects using dynamic class names
    ... classes from a common base class and have an array of class references ... Not necessarily an open array, although that neatly coalesces the steps ... The constructor of the base class can only construct an instance of the ... TPageClass = class of TPage; ...
    (comp.lang.pascal.delphi.misc)
  • Re: Create objects using dynamic class names
    ... classes from a common base class and have an array of class references ... Not necessarily an open array, although that neatly coalesces the steps ... The constructor of the base class can only construct an instance of the ... TPageClass = class of TPage; ...
    (comp.lang.pascal.delphi.misc)
  • Re: Unification of Methods and Functions
    ... I am suggesting that we write factory methods using classmethod to give ... my classes all implement an interface which I'll call 'shape ... Rectangle and Ellipse have a common base class, ...
    (comp.lang.python)
  • Re: What does the CLR do when you instantiate an object? - Continue
    ... I did think about looking at the rotor source but the original question ... asked was meant for the MS CLR not another implementation. ... > the complier will automaticaly insert the defaut constructor in the IL ... > The CLR WILL NOT call the base class automatically for you. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Am I missing something in private inheritance?
    ... >> virtual base class is public, ... > constructor, I do not know. ... > class SomeClass: Sealed ... decides to change public inheritance to private. ...
    (comp.lang.cpp)

Loading