Java 工厂设计模式

 


/*
 * 设计模式:
 * 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{

	@Override
	public void run() {
		System.out.println("宝马");
	}
	
}

 * interface GongChang{
	Che getChe();
}
class BaoMaFactory implements GongChang{

	@Override
	public Che getChe() {
		return new BaoMa();
	}
	
}
 */
public class TestFactory {
	@Test
	public void test03(){
		Car c = SimpleFactory2.getCar("奔驰");
		c.run();
	}
	
	@Test
	public void test02(){
		Car c = SimpleFactory.getCar();
		c.run();//这里也是运行宝马的run(),但是从头至尾没有出现BMW类型
	}
	
	@Test
	public void test01(){
		//没有工厂
		BMW b = new BMW();
		
		//使用对象,调用方法
		b.run();
	}
}
interface Car{
	void run();
}
class BMW implements Car{

	@Override
	public void run() {
		System.out.println("宝马让你在车里面哭");
	}
	
}
class Benz implements Car{
	@Override
	public void run() {
		System.out.println("奔驰让你在车盖上哭");
	}
}
class Audi implements Car{
	@Override
	public 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{

	@Override
	public void run() {
		System.out.println("宝马");
	}
	
}

class BenChi implements Che{

	@Override
	public void run() {
		System.out.println("奔驰");
	}
}
class AoDi implements Che{
	@Override
	public void run() {
		System.out.println("奥迪");
	}
}

interface GongChang{
	Che getChe();
}
class BaoMaFactory implements GongChang{

	@Override
	public Che getChe() {
		return new BaoMa();
	}
	
}
class BenChiFactory implements  GongChang{

	@Override
	public 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{

	@Override
	public void run() {
		System.out.println("qq车");
	}
	
}
class Aoto implements  Vehicle{

	@Override
	public void run() {
		System.out.println("奥拓");
	}
	
}
class Atguigu implements Vehicle{

	@Override
	public void run() {
		System.out.println("尚硅谷");
	}
	
}
class SimpleFactory3{
	public static Vehicle getVehicle(String className)throws Exception{
		Class clazz = Class.forName(className);
		return (Vehicle) clazz.newInstance();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值