package com.abstract_factory;
public interface Animal {
void sound();
}
package com.abstract_factory;
public interface AnimalFactory {
Animal creat();
}
package com.abstract_factory;
public class Cat implements Animal {
private String type;
private String color;
private int sex;
public Cat(String type, String color, int sex) {
super();
this.type = type;
this.color = color;
this.sex = sex;
}
@Override
public void sound() {
System.out.println("mewo mewo mewo!");
}
}
package com.abstract_factory;
public class CatFactory implements AnimalFactory {
@Override
public Animal creat() {
return new Cat("布偶", "white", 1);
}
}
package com.abstract_factory;
public class Dog implements Animal {
private String type;
private String color;
private int sex;
public Dog(String type, String color, int sex) {
super();
this.type = type;
this.color = color;
this.sex = sex;
}
@Override
public void sound() {
System.out.println("wang wang wang!");
}
}
package com.abstract_factory;
public class DogFactory implements AnimalFactory {
@Override
public Animal creat() {
return new Dog("哈士奇", "black", 1);
}
}
package com.abstract_factory;
public class Girl {
public void playWith(Animal animal) {
animal.sound();
}
}
package com.abstract_factory;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
new Girl().playWith(new CatFactory().creat());
new Girl().playWith(new DogFactory().creat());
}
}