package 工厂方法模式;
interface Fruit {
public void show();
}
class Banana implements Fruit {
public void show() {
System.out.println("我是香蕉");
}
}
class Apple implements Fruit {
public void show() {
System.out.println("我是苹果");
}
}
interface FruitFactory {
public Fruit getInstance();
}
class AppleFactory implements FruitFactory {
private Apple apple = new Apple();
public Fruit getInstance() {
return apple;
}
}
class BananaFactory implements FruitFactory {
private Banana banana = new Banana();
public Fruit getInstance() {
return banana;
}
}
public class FactoryMethod {
public static void main(String[] args) {
System.out.println("工厂方法模式");
AppleFactory afAppleFactory = new AppleFactory();
Fruit apple = afAppleFactory.getInstance();
apple.show();
}
}
interface Fruit {
public void show();
}
class Banana implements Fruit {
public void show() {
System.out.println("我是香蕉");
}
}
class Apple implements Fruit {
public void show() {
System.out.println("我是苹果");
}
}
interface FruitFactory {
public Fruit getInstance();
}
class AppleFactory implements FruitFactory {
private Apple apple = new Apple();
public Fruit getInstance() {
return apple;
}
}
class BananaFactory implements FruitFactory {
private Banana banana = new Banana();
public Fruit getInstance() {
return banana;
}
}
public class FactoryMethod {
public static void main(String[] args) {
System.out.println("工厂方法模式");
AppleFactory afAppleFactory = new AppleFactory();
Fruit apple = afAppleFactory.getInstance();
apple.show();
}
}