package com.bjsxt.cn.abstractFactory;
public interface Engine { void run(); void start(); }
class LuxuryEngine implements Engine {
@Override public void run() { System.out.println("高端引擎,运行快"); }
@Override public void start() { System.out.println("高端引擎可自动启动停止"); } }
class LowEngine implements Engine {
@Override public void run() { System.out.println("低端汽车,运行慢"); }
@Override public void start() { System.out.println("低端汽车,不能自动启动停止"); } }
package com.bjsxt.cn.abstractFactory;
public interface Seat { void massage(); }
class LuxurySeat implements Seat {
@Override public void massage() { System.out.println("高端座椅,舒适可按摩"); } }
class LowSeat implements Seat {
@Override public void massage() { System.out.println("低端汽车,不能自动按摩"); } }
package com.bjsxt.cn.abstractFactory;
public interface Tyre { public void revolve(); }
class LuxuryTyre implements Tyre {
@Override public void revolve() { System.out.println("高端汽车,轮胎转速快,无磨损"); } } class LowTyre implements Tyre {
@Override public void revolve() { System.out.println("低端汽车转速慢,磨损较快"); } }
package com.bjsxt.cn.abstractFactory;
public abstract interface CarFactory { public Engine createEngine(); public Seat createSeat(); public Tyre creatTyre(); }
package com.bjsxt.cn.abstractFactory;
public class LowCarFactory implements CarFactory {
@Override public Engine createEngine() { return new LowEngine(); }
@Override public Seat createSeat() { return new LowSeat(); }
@Override public Tyre creatTyre() { return new LowTyre(); } }
package com.bjsxt.cn.abstractFactory;
public class LuxuryCarFactory implements CarFactory {
@Override public Engine createEngine() { return new LuxuryEngine(); }
@Override public Seat createSeat() { return new LuxurySeat(); }
@Override public Tyre creatTyre() { return new LuxuryTyre(); }
}
/** * 时间:2015年3月29日10:48:16 * 源程序代码作用:测试抽象工厂模式 * 抽象工厂模式:用来生产不同产品组的全部产品,(对于增加新产品无能为力) * 抽象工厂模式:是工厂方法的升级版本 * 在多个业务品种中,业务分类是通过抽象工厂模式,产生需要的对象是一种非常好的解决方式。 */ package com.bjsxt.cn.abstractFactory;
public class Client { public static void main(String[] args) { LuxuryCarFactory cf1 = new LuxuryCarFactory(); Engine e = cf1.createEngine(); e.run(); e.start(); Engine e2 = cf1.createEngine(); e2.run(); e2.start(); cf1.createSeat().massage(); cf1.creatTyre().revolve(); System.out.println("----------------------------"); CarFactory cf2 = new LowCarFactory(); Engine e3 = cf2.createEngine(); e3.run(); e3.start(); cf2.createSeat().massage(); cf2.creatTyre().revolve(); } } /* 高端引擎,运行快 高端引擎可自动启动停止 高端引擎,运行快 高端引擎可自动启动停止 高端座椅,舒适可按摩 高端汽车,轮胎转速快,无磨损 ---------------------------- 低端汽车,运行慢 低端汽车,不能自动启动停止 低端汽车,不能自动按摩 低端汽车转速慢,磨损较快
* */