mardi 26 juillet 2016

Factory Design Pattern and Dimond OOP issue

Hello OOP Experts and Design Pattern Experts,

In one of my project, I have to implement Factory design pattern to solve a specific issue.

I have one parent interface and two child interfaces. In next stage, I have to create a factory which will return an instance of a specific class based on given input. Please see below SAMPLE code which is easy to understand my problem and sample image as well.

Sample Diagram

enter image description here

Sample Code

enum AnimalType{ DOG, CAT }

Class Factory{
    public Animal getInstance(AnimalType animalType){
        Animal animal = null;
        switch(animalType){
            case DOG: animal = new Dog();
                break;

            case CAT: animal = new Cat();
                break;
            default:
                break;
        }
        return animal;
    }
}

/*Actual Problem */
Animal animal = Factory.getInstance(AnimalType.DOG);

  /* When I use any IDE like IntellijIdea or Eclipse it only provides eat() method after animal and dot (ie. animal. ) */
animal.<SHOULD PROVIDE eat() and woof() from Dog> but it is only providing eat()

Aucun commentaire:

Enregistrer un commentaire