当前位置: 代码迷 >> 综合 >> Java 工厂设计模式
  详细解决方案

Java 工厂设计模式

热度:15   发布时间:2024-02-05 06:31:39.0

 


/** 设计模式:* 1、模板设计模式(了解):抽象类* 2、单例设计模式(重点):多线程时* 3、装饰者设计模式(IO流体系结构就体现了)* 4、迭代器设计模式(集合,在集合的内部设计迭代器类型)* 5、工厂设计模式(了解)* 6、代理设计模式(了解)* * 一、工厂设计模式:为了解耦合,把对象的创建者与对象的使用者分开。* 生活中:批量生产产品* Java中:批量生产对象* * 生活中:* 	   最早没有工厂,自给自足,男耕女织* 	  作坊* 	 大工厂* Java:* 	 没有工厂,需要对象,自己new* * 分工:* 	  把生产(创建)对象与使用对象分开了,解耦合* * 1、简单工厂模式* class SimpleFactory2{public static Car getCar(String type){if("宝马".equals(type)){return new BMW();}else if("奔驰".equals(type)){return new Benz();}return null;}}**	优点:代码比较简洁*  缺点:如果增加新的产品类型,需要修改工厂类*     违反了面向对象的一个开发原则:对扩展开放,对修改关闭** 2、工厂方法模式* 特点:一个产品就对应一个工厂类* * interface Che{void run();
}
class BaoMa implements Che{@Overridepublic void run() {System.out.println("宝马");}}* interface GongChang{Che getChe();
}
class BaoMaFactory implements GongChang{@Overridepublic Che getChe() {return new BaoMa();}}*/
public class TestFactory {@Testpublic void test03(){Car c = SimpleFactory2.getCar("奔驰");c.run();}@Testpublic void test02(){Car c = SimpleFactory.getCar();c.run();//这里也是运行宝马的run(),但是从头至尾没有出现BMW类型}@Testpublic void test01(){//没有工厂BMW b = new BMW();//使用对象,调用方法b.run();}
}
interface Car{void run();
}
class BMW implements Car{@Overridepublic void run() {System.out.println("宝马让你在车里面哭");}}
class Benz implements Car{@Overridepublic void run() {System.out.println("奔驰让你在车盖上哭");}
}
class Audi implements Car{@Overridepublic void run() {System.out.println("奥迪让你在...");}
}//工厂类
class SimpleFactory{public static Car getCar(){return new BMW();}
}class SimpleFactory2{public static Car getCar(String type){if("宝马".equals(type)){return new BMW();}else if("奔驰".equals(type)){return new Benz();}return null;}
}
/** 2、工厂方法模式* 出发点:* 	(1)为了生产对象与使用对象分开   * 	(2)如果增加新产品,就不需要修改原来的工厂类* * 优点:遵循了增加新产品,不修改原来的类的原则,* 缺点:类太多了*/
public class TestFactory2 {public static void main(String[] args) {BaoMaFactory bf = new BaoMaFactory();Che c = bf.getChe();c.run();}
}
interface Che{void run();
}
class BaoMa implements Che{@Overridepublic void run() {System.out.println("宝马");}}class BenChi implements Che{@Overridepublic void run() {System.out.println("奔驰");}
}
class AoDi implements Che{@Overridepublic void run() {System.out.println("奥迪");}
}interface GongChang{Che getChe();
}
class BaoMaFactory implements GongChang{@Overridepublic Che getChe() {return new BaoMa();}}
class BenChiFactory implements  GongChang{@Overridepublic Che getChe() {return new BenChi();}}
//只需要增加一个工厂类,专门生产奥迪车
public class TestFactory3 {public static void main(String[] args)throws Exception {Vehicle c = SimpleFactory3.getVehicle("com.atguigu.test12.QQ");c.run();Vehicle c2 = SimpleFactory3.getVehicle("com.atguigu.test12.Aoto");c2.run();Vehicle c3 = SimpleFactory3.getVehicle("com.atguigu.test12.Atguigu");c3.run();}
}
interface Vehicle{void run();
}
class QQ implements Vehicle{@Overridepublic void run() {System.out.println("qq车");}}
class Aoto implements  Vehicle{@Overridepublic void run() {System.out.println("奥拓");}}
class Atguigu implements Vehicle{@Overridepublic void run() {System.out.println("尚硅谷");}}
class SimpleFactory3{public static Vehicle getVehicle(String className)throws Exception{Class clazz = Class.forName(className);return (Vehicle) clazz.newInstance();}
}