What are the differences between Abstract Factory and Factory design patterns?

后端 未结 17 1145
醉话见心
醉话见心 2020-11-22 01:57

I know there are many posts out there about the differences between these two patterns, but there are a few things that I cannot find.

From what I have been reading,

17条回答
  •  既然无缘
    2020-11-22 02:51

    A lot of the previous answers do not provide code comparisons between Abstract Factory and Factory Method pattern. The following is my attempt at explaining it via Java. I hope it helps someone in need of a simple explanation.

    As GoF aptly says: Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.

    public class Client {
        public static void main(String[] args) {
            ZooFactory zooFactory = new HerbivoreZooFactory();
            Animal animal1 = zooFactory.animal1();
            Animal animal2 = zooFactory.animal2();
            animal1.sound();
            animal2.sound();
    
            System.out.println();
    
            AnimalFactory animalFactory = new CowAnimalFactory();
            Animal animal = animalFactory.createAnimal();
            animal.sound();
        }
    }
    

    public interface Animal {
        public void sound();
    }
    
    public class Cow implements Animal {
    
        @Override
        public void sound() {
            System.out.println("Cow moos");
        }
    }
    
    public class Deer implements Animal {
    
        @Override
        public void sound() {
            System.out.println("Deer grunts");
        }
    
    }
    
    public class Hyena implements Animal {
    
        @Override
        public void sound() {
            System.out.println("Hyena.java");
        }
    
    }
    
    public class Lion implements Animal {
    
        @Override
        public void sound() {
            System.out.println("Lion roars");
        }
    
    }
    

    public interface ZooFactory {
        Animal animal1();
    
        Animal animal2();
    }
    
    public class CarnivoreZooFactory implements ZooFactory {
    
        @Override
        public Animal animal1() {
            return new Lion();
        }
    
        @Override
        public Animal animal2() {
            return new Hyena();
        }
    
    }
    
    public class HerbivoreZooFactory implements ZooFactory {
    
        @Override
        public Animal animal1() {
            return new Cow();
        }
    
        @Override
        public Animal animal2() {
            return new Deer();
        }
    
    }
    

    public interface AnimalFactory {
        public Animal createAnimal();
    }
    
    public class CowAnimalFactory implements AnimalFactory {
    
        @Override
        public Animal createAnimal() {
            return new Cow();
        }
    
    }
    
    public class DeerAnimalFactory implements AnimalFactory {
    
        @Override
        public Animal createAnimal() {
            return new Deer();
        }
    
    }
    
    public class HyenaAnimalFactory implements AnimalFactory {
    
        @Override
        public Animal createAnimal() {
            return new Hyena();
        }
    
    }
    
    public class LionAnimalFactory implements AnimalFactory {
    
        @Override
        public Animal createAnimal() {
            return new Lion();
        }
    
    }
    

提交回复
热议问题