1.目录结构
2.设计产品,产品簇,抽象工厂和具体工厂的概念
3.具体实现
Fruit.java
public interface Fruit {
public void get();
}
FruitFactory.java
public interface FruitFactory {
public Fruit getApple();
public Fruit getBanana();
}
Apple.java
public abstract class Apple implements Fruit{
@Override
public abstract void get();
}
Banana.java
public abstract class Banana implements Fruit{
@Override
public abstract void get();
}
SouthApple.java
public class SouthApple extends Apple{
@Override
public void get() {
// TODO Auto-generated method stub
System.out.println("采摘南方苹果");
}
}
SouthBanana.java
public class SouthBanana extends Banana{
@Override
public void get() {
// TODO Auto-generated method stub
System.out.println("采摘南方香蕉");
}
}
SouthFactory.java
public class SouthFruitFactory implements FruitFactory{
@Override
public Fruit getApple() {
// TODO Auto-generated method stub
return new SouthApple();
}
@Override
public Fruit getBanana() {
// TODO Auto-generated method stub
return new SouthBanana();
}
}
NorthApple.java
public class NorthApple extends Apple{
@Override
public void get() {
// TODO Auto-generated method stub
System.out.println("采摘北方苹果");
}
}
NorthBanana.java
public class NorthBanana extends Banana{
@Override
public void get() {
// TODO Auto-generated method stub
System.out.println("采摘北方香蕉");
}
}
NorthFactory.java
public class NorthFruitFactory implements FruitFactory{
@Override
public Fruit getApple() {
// TODO Auto-generated method stub
return new NorthApple();
}
@Override
public Fruit getBanana() {
// TODO Auto-generated method stub
return new NorthBanana();
}
}
Client.java
public class Client {
public static void main(String[] args) {
FruitFactory fruitFactory = new SouthFruitFactory();
Fruit southApple = fruitFactory.getApple();
southApple.get();
}
}
Demo链接:http://download.csdn.net/detail/u013072976/8393895