T
Tony Johansson
Hello!
The inheritance hierarcy is the following
First of all I have an inteface called IAnimal that the abstract Animal
class is implementing
I have Animal as the most general class.
Below this I have five classes that have Animal as the base class these are
Mammal, Bird, Marine, Reptile and Insect.
If I manage to use the factory pattern for the Mammal I can do the same with
the other.
I have made an attempt to use the Factory pattern below by creating the
class MammalFactory which is responsible to create
the classes that derive from Mammal
Here I have a Factory class called MammalFactory.
The problem is that the constructor for Cat and Bear is implemented to take
a lot of argument which is then
passed up to the base class.
So my question is simply how do I usually solve this.
I mean when the Animal class is abstract the only way to store data in this
class is those data that come from the derived class.
So I assume that the only way is to pass all the data into this method but I
doesn't feel correct in some way ?
class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
//objbase = new Bear();
break;
case MammalAnimalsType.Cat:
// objbase = new Cat();
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}
//Tony
The inheritance hierarcy is the following
First of all I have an inteface called IAnimal that the abstract Animal
class is implementing
I have Animal as the most general class.
Below this I have five classes that have Animal as the base class these are
Mammal, Bird, Marine, Reptile and Insect.
If I manage to use the factory pattern for the Mammal I can do the same with
the other.
I have made an attempt to use the Factory pattern below by creating the
class MammalFactory which is responsible to create
the classes that derive from Mammal
Here I have a Factory class called MammalFactory.
The problem is that the constructor for Cat and Bear is implemented to take
a lot of argument which is then
passed up to the base class.
So my question is simply how do I usually solve this.
I mean when the Animal class is abstract the only way to store data in this
class is those data that come from the derived class.
So I assume that the only way is to pass all the data into this method but I
doesn't feel correct in some way ?
class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
//objbase = new Bear();
break;
case MammalAnimalsType.Cat:
// objbase = new Cat();
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}
//Tony