abstract class

F

Frank

Hello,
a question about abstract classes.
Abstract ClassA, inherited classes X and Y.
When creating it's something like:
if (true)
ClassX xx = new ClassX();
else
ClassY yy = new ClassY();

Is it in someway possible to put the if statement in the constructor of the
abstract ClassA? So I don't have to think about which inherited class to
create and leave the correct creation to ClassA?
Thanks in advance
Frank
 
P

Peter Rilling

First of all, to answer your direct question, no. A constructor plays no
role in defining what object gets created but rather just initializes the
object that you are already creating.

Now, why no place that logic in a static method on ClassA that acts as a
factor for the instance. Returning the correct object.

But, your question indicates that you do not want to be bothered by knowing
what class to create. Well, don't you kind of have to know what class to
create in order to use it. Sub class, although they inherit some
functionality, have differences that only that class know about. For
instances, streams are a great think, but you will not use a network stream
to read and write to the disk -- so one creates a FileStream for this
instance.

You are still going to have to have some flag somewhere, whether it be a
parameter or class field, that indicates what should be initialized.
 
F

Frank

Thanks Peter,
I expected that answer. But I like the idea of a static method determining
what type of class to create. I'm going to try that.
Frank
 
B

Bruce Wood

Frank take a look at the classic book "Design Patterns" by Gamma, Helm,
Johnson, and Vlissides. Or search for "Factory Method" in Google. What
you want is called the "Factory Method" design pattern.
 
J

jeffc

Bruce Wood said:
Frank take a look at the classic book "Design Patterns" by Gamma, Helm,
Johnson, and Vlissides. Or search for "Factory Method" in Google. What
you want is called the "Factory Method" design pattern.

Right - Peter's answer was quite confusing. I think instead of "factor" he
might have meant "factory". You should not be putting that code into a base
class. But it can go into a different class that acts as the creator or factory
for your objects. Then you would have a function such as "makeMyObject", and
that function would be overridden in the factory sub classes. The trick is to
get the correct factory created to begin with, so it returns the correct object
type.
 
J

Jeff Louie

Frank... You can abstract object creation in an interface such as:
public interface IConstructDrawable
{
string Name {get;}
IDrawable Instantiate();
}
We can then write a statically compiled CircleConstructor class like
this:

class CircleConstructor: IConstructDrawable
{
public string Name
{
get {return "Circle";}
}
public IDrawable Instantiate()
{
return new Circle();
}
}
Here is the code for the DrawableFactory that contains a growable
ArrayList of objects that implement the IConstructDrawable interface:

class DrawableFactory
{
private ArrayList list= new ArrayList();
public void Add(IConstructDrawable item)
{
if (item != null)
{
list.Add(item);
}
}
public int CountItems()
{
return list.Count;
}
public string NameItem(int index)
{
if (index<0 || index> list.Count)
{
return "Invalid index.";
}
else
{
return ((IConstructDrawable)list[index]).Name;
}
}
public IDrawable GetInstance(int index)
{
if (index<0 || index> list.Count)
{
return null;
}
else
{
return ((IConstructDrawable)list[index]).Instantiate();
}
}
}

Regards,
Jeff
Is it in someway possible to put the if statement in the constructor of
the abstract ClassA? So I don't have to think about which inherited
class to create and leave the correct creation to ClassA?<
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top