具体的水果工厂实现类 | 具体的苹果实现类 | 具体的香蕉实现类 | 具体的枣实现类 |
中国水果工厂实现类 | 中国苹果 | 中国香蕉 | 中国枣 |
日本水果工厂实现类 | 日本苹果 | 日本香蕉 | 日本枣 |
以此类推,在工厂的接口中创建所有水果的方法声明。
public interface IFruit {
public void get();
}
public abstract class AbstractApple implements IFruit{
public abstract void get();
}
public abstract class AbstractBanana implements IFruit{
public abstract void get();
}
public interface IFruitFactory {
public IFruit getApple();
public IFruit getBanana();
}
public class NorthApple extends AbstractApple {
public void get() {
System.out.println("北方苹果");
}
}
public class NorthBanana extends AbstractBanana {
public void get() {
System.out.println("北方香蕉");
}
}
public class NorthFruitFactory implements IFruitFactory {
public IFruit getApple() {
return new NorthApple();
}
public IFruit getBanana() {
return new NorthBanana();
}
}
public class SouthApple extends AbstractApple {
public void get() {
System.out.println("南方苹果");
}
}
public class SouthBanana extends AbstractBanana {
public void get() {
System.out.println("南方香蕉");
}
}
public class SouthFruitFactory implements IFruitFactory {
public IFruit getApple() {
return new SouthApple();
}
public IFruit getBanana() {
return new SouthBanana();
}
}