I have an abstract class “MainClass” which composes “Animal” class. I derive two classes TypeA and TypeB from abstract class which contains common functionality. TypeA and TypeB classes need to be extended to include their own specific functionality.
For example, TypeA would require to add cat functionality under Animal class. So that test application will be accessing the cat class like this typeA._animals._cat?
I know types cannot be added at runtime but is there any other design pattern that could solve my problem?
public abstract class MainClass
{
public Animal _animals;
}
public class Animal
{
public Tiger _tiger;
}
public class Tiger
{
public int type { get { return "Tiger" ; } }
}
public class Cat
{
public int type { get { return "Car" ; } }
}
public class Leopard
{
public int type { get { return "Leopard" ; } }
}
public class TypeA : MainSession
{
//Would like to add type Cat to Animal class
}
public class TypeB : MainSession
{
//Would like to add type Leopard to Animal class
}
The first thing you should do is to create an inheritance:
And implement it:
Let’s use it:
You could also convert the
AddAnimalinto a factory method:Having the factory in
MainSessionbreaks Single Responsibility Principle, so I would break it out into a separate class.