factory pattern give me problem

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
 
A

Arne Vajhøj

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;
}
}

One way would be:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
objbase = new Bear((FurColor)attribs[Bear.FurColorAttrib]);
break;
case MammalAnimalsType.Cat:
objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]);
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But I am not convinced that it is good design.

It is not type safe.

And in general factory pattern is better for
behavior heavy classes than for data heavy classes.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
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;
}
}

One way would be:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
objbase = new Bear((FurColor)attribs[Bear.FurColorAttrib]);
break;
case MammalAnimalsType.Cat:
objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]);
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But I am not convinced that it is good design.

It is not type safe.

And in general factory pattern is better for
behavior heavy classes than for data heavy classes.

Arne

But do you agree with me that the only solution is to pass the data into the
c-tor like
you give an example of here objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]); ?

//Tony
 
A

Arne Vajhøj

Arne Vajhøj said:
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;
}
}

One way would be:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
objbase = new Bear((FurColor)attribs[Bear.FurColorAttrib]);
break;
case MammalAnimalsType.Cat:
objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]);
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But I am not convinced that it is good design.

It is not type safe.

And in general factory pattern is better for
behavior heavy classes than for data heavy classes.

But do you agree with me that the only solution is to pass the data into the
c-tor like
you give an example of here objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]); ?

No. You could also do it via properties. But that would not
change anything.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
Arne Vajhøj said:
On 28-01-2011 13:43, Tony Johansson wrote:
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;
}
}

One way would be:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
objbase = new
Bear((FurColor)attribs[Bear.FurColorAttrib]);
break;
case MammalAnimalsType.Cat:
objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]);
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But I am not convinced that it is good design.

It is not type safe.

And in general factory pattern is better for
behavior heavy classes than for data heavy classes.

But do you agree with me that the only solution is to pass the data into
the
c-tor like
you give an example of here objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]); ?

No. You could also do it via properties. But that would not
change anything.

Arne

You say properties but it's not possible to set property on instans fields
in an abstract class in that case you must use static fields and static
propery.
What is that you were thinking of ?

//Tony
 
A

Arne Vajhøj

Arne Vajhøj said:
"Arne Vajhøj"<[email protected]> skrev i meddelandet
On 28-01-2011 13:43, Tony Johansson wrote:
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;
}
}

One way would be:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
objbase = new
Bear((FurColor)attribs[Bear.FurColorAttrib]);
break;
case MammalAnimalsType.Cat:
objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]);
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But I am not convinced that it is good design.

It is not type safe.

And in general factory pattern is better for
behavior heavy classes than for data heavy classes.

But do you agree with me that the only solution is to pass the data into
the
c-tor like
you give an example of here objbase = new
Cat((MouseHunterSkills)attribs[Cat.MouseHunterSkillsAttrib]); ?

No. You could also do it via properties. But that would not
change anything.

You say properties but it's not possible to set property on instans fields
in an abstract class in that case you must use static fields and static
propery.
What is that you were thinking of ?

No.

In the factory you do now the concrete class.

So:

class MammalFactory
{
public IAnimal GetObject(MammalAnimalsType thisMammal,
Dictionary<string,object> attribs)
{
IAnimal objbase = null;
switch (thisMammal)
{
case MammalAnimalsType.Bear:
Bear b = new Bear();
b.Color = (FurColor)attribs[Bear.FurColorAttrib];
objbase = b;
break;
case MammalAnimalsType.Cat:
Cat c = new Cat();
c.Skills = (MouseHunterSkills)Cat.MouseHunterSkillsAttrib;
objbase = c;
break;
default:
throw new ArgumentOutOfRangeException();
}
return objbase;
}
}

But that does not change anything.

Arne
 

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