标签:none 启动慢 sage rac stat ati out closed sea
//car接口 public interface Car { void run(); } //两个实现类 public class Audi implements Car{ public void run() { System.out.println("奥迪在跑"); } } public class Byd implements Car{ public void run() { System.out.println("Byd在跑"); } }
//第一种实现 public class CarFactory { public static Car createCar(String type){ if("奥迪".equals(type)){ return new Audi(); }else if("比亚迪".equals(type)){ return new Byd(); }else{ return null; } } } //第二种实现 public class CarFactory2 { public static Car createAudi(){ return new Audi(); } public static Car createByd(){ return new Byd(); } }
/** * 普通模式,无工厂模式 * @author benny * */ public class Client01 { public static void main(String[] args) { Car c1 = new Audi(); Car c2 = new Byd(); c1.run(); c2.run(); } } /** * 简单工厂模式 * @author benny * */ public class Client02 { public static void main(String[] args) { Car c1 = CarFactory.createCar("奥迪"); Car c2 = CarFactory.createCar("比亚迪"); c1.run(); c2.run(); } }
public interface Car { void run(); } public class Audi implements Car{ public void run() { System.out.println("奥迪在跑"); } } public class Benz implements Car{ public void run() { System.out.println("奔驰在跑"); } } public class Byd implements Car{ public void run() { System.out.println("Byd在跑"); } }
public interface CarFactory { Car createCar(); } public class AudiFactory implements CarFactory { @Override public Car createCar() { return new Audi(); } } public class BenzFactory implements CarFactory { @Override public Car createCar() { return new Benz(); } } public class BydFactory implements CarFactory { @Override public Car createCar() { return new Byd(); } }
/** * 最大的好处实现了开闭原则 * @author benny * */ public class Client { public static void main(String[] args) { Car c1 = new AudiFactory().createCar(); Car c2 = new BydFactory().createCar(); Car c3 = new BenzFactory().createCar(); c1.run(); c2.run(); c3.run(); } }
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("启动慢,不可以自动启停"); } }
public interface Tyre { void revolve(); } class LuxuryTyre implements Tyre{ @Override public void revolve() { System.out.println("旋转不磨损"); } } class LowTyre implements Tyre{ @Override public void revolve() { System.out.println("磨损快"); } }
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("不能按摩"); } }
public interface CarFactory { Engine createEngine(); Seat createSeat(); Tyre createTyre(); } public class LowCarFactory implements CarFactory{ @Override public Engine createEngine() { return new LowEngine(); } @Override public Seat createSeat() { return new LowSeat(); } @Override public Tyre createTyre() { return new LowTyre(); } } public class LuxuryCarFactory implements CarFactory{ @Override public Engine createEngine() { return new LuxuryEngine(); } @Override public Seat createSeat() { return new LuxurySeat(); } @Override public Tyre createTyre() { return new LuxuryTyre(); } }
public class Client { public static void main(String[] args) { CarFactory factory = new LuxuryCarFactory(); Engine e =factory.createEngine(); e.run(); e.start(); } }
标签:none 启动慢 sage rac stat ati out closed sea
原文地址:http://www.cnblogs.com/qingdaofu/p/7466063.html