/**
* 生产青水果工厂
* @author Administrator
*
*/
public class GreenFruitFactory implements IFruitFactory{
/**
* 生产青苹果
*/
@Override
public IApple productApple() {
return new GreenApple();
}
/**
* 生产青葡萄
*/
@Override
public IGrape productGrape() {
return new GreenGrape();
}
}
/**
* 生产红水果工厂
* @author Administrator
*
*/
public class RedFruitFactory implements IFruitFactory{
/**
* 生产红苹果
*/
@Override
public IApple productApple() {
return new RedApple();
}
/**
* 生产红葡萄
*/
@Override
public IGrape productGrape() {
return new RedGrape();
}
}
/**
* 苹果
* @author Administrator
*
*/
public interface IApple {
/**
* 苹果的味道
*/
public void getTaste();
}
/**
* 青苹果
* @author Administrator
*
*/
public class GreenApple implements IApple{
@Override
public void getTaste() {
System.out.println("青苹果味道不错");
}
}
/**
* 红苹果
* @author Administrator
*
*/
public class RedApple implements IApple{
@Override
public void getTaste() {
System.out.println("红苹果吃起来很爽");
}
}
/**
* 葡萄
* @author Administrator
*
*/
public interface IGrape {
/**
* 葡萄的味道
*/
public void getTaste();
}
/**
* 青葡萄
* @author Administrator
*
*/
public class GreenGrape implements IGrape{
@Override
public void getTaste() {
System.out.println("青葡萄吃起来很甜");
}
}
/**
* 红葡萄
* @author Administrator
*
*/
public class RedGrape implements IGrape{
@Override
public void getTaste() {
System.out.println("红葡萄吃起来又酸又甜");
}
}
/**
* 抽象工厂测试类
* @author Administrator
*
*/
public class TestAbstractFactory {
public static void main(String[] args) {
IFruitFactory greenFruitFactory=new GreenFruitFactory();
//生产青苹果
IApple greenApple=greenFruitFactory.productApple();
//生产青葡萄
IGrape greenGrape=greenFruitFactory.productGrape();
greenApple.getTaste();
greenGrape.getTaste();
IFruitFactory redFruitFactory=new RedFruitFactory();
//生产红苹果
IApple redApple=redFruitFactory.productApple();
//生产红葡萄
IGrape redGrape=redFruitFactory.productGrape();
redApple.getTaste();
redGrape.getTaste();
}
}
程序运行结果如下:
青苹果味道不错
青葡萄吃起来很甜
红苹果吃起来很爽
红葡萄吃起来又酸又甜