java设计模式之工厂模式详解
更新时间:2024年11月08日 09:30:48 作者:北极熊不在北极
本文介绍了三种常见的设计模式:简单厂模式、工厂方法模式和抽象工厂模式,简单厂模式通过一个工厂类来生产不同的产品实例,例如同时生产华为和小米手机,工厂方法模式则通过定义一个创建对象的接口,让子类决定实例化哪一个类
一、简单厂模式
一个工厂既代工生产华为手机也生产小米手机。
- 手机接口类
package com.liuyc.designpattern.factory; public interface Phone { void showInfo(); }
- 华为手机实现类
package com.liuyc.designpattern.factory; public class HuaWeiPhone implements Phone{ @Override public void showInfo() { System.out.println("华为手机"); } }
- 小米手机实现类
package com.liuyc.designpattern.factory; public class XiaoMiPhone implements Phone{ @Override public void showInfo() { System.out.println("小米手机"); } }
- 生产手机工厂类
package com.liuyc.designpattern.factory.simplefactory; import com.liuyc.designpattern.factory.Phone; public class SimpleFactory { public static Phone getPhone(String className){ Phone phone = null; try { Class<Phone> aClass = (Class<Phone>) Class.forName(className); phone = aClass.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return phone; } }
- 具体使用类
package com.liuyc.designpattern.factory.simplefactory; import com.liuyc.designpattern.factory.Phone; public class SimpleFactoryMain { public static void main(String[] args) { Phone phone = SimpleFactory.getPhone("com.liuyc.designpattern.factory.XiaoMiPhone"); phone.showInfo(); } }
二、工厂方法模式
- 一个生产手机的工厂接口类
package com.liuyc.designpattern.factory.factorymethod; import com.liuyc.designpattern.factory.Phone; public interface PhoneFactory { Phone getPhone(); }
- 具体生产华为手机的工厂
package com.liuyc.designpattern.factory.factorymethod; import com.liuyc.designpattern.factory.HuaWeiPhone; import com.liuyc.designpattern.factory.Phone; public class HuaWeiPhoneFactory implements PhoneFactory { @Override public Phone getPhone() { return new HuaWeiPhone(); } }
- 具体生产小米手机的工厂
package com.liuyc.designpattern.factory.factorymethod; import com.liuyc.designpattern.factory.Phone; import com.liuyc.designpattern.factory.XiaoMiPhone; public class XiaoMiPhoneFactory implements PhoneFactory { @Override public Phone getPhone() { return new XiaoMiPhone(); } }
- 具体使用类
package com.liuyc.designpattern.factory.factorymethod; import com.liuyc.designpattern.factory.Phone; public class FactoryMethodMain { public static void main(String[] args) { PhoneFactory phoneFactory = new XiaoMiPhoneFactory(); Phone phone = phoneFactory.getPhone(); phone.showInfo(); } }
三、抽象工厂模式
现在,不管是华为也好,小米也好,他们已不满足于只生产手机了,他们也开始造车了,现在有一个“组”的概念了,或者说族。
- 小车的接口类
package com.liuyc.designpattern.factory; public interface Car { void showInfo(); }
- 华为汽车
package com.liuyc.designpattern.factory; public class HuaWeiCar implements Car { @Override public void showInfo() { System.out.println("华为小汽车"); } }
- 小米汽车
package com.liuyc.designpattern.factory; public class XiaoMiCar implements Car { @Override public void showInfo() { System.out.println("小米汽车"); } }
- 抽象工厂,定义既能生产手机,也是生产汽车
package com.liuyc.designpattern.factory.abstactfactory; import com.liuyc.designpattern.factory.Car; import com.liuyc.designpattern.factory.Phone; public interface AbstractFactory { Phone getPhone(); Car getCar(); }
- 华为工厂
package com.liuyc.designpattern.factory.abstactfactory; import com.liuyc.designpattern.factory.Car; import com.liuyc.designpattern.factory.HuaWeiCar; import com.liuyc.designpattern.factory.HuaWeiPhone; import com.liuyc.designpattern.factory.Phone; public class HuaWeiFactory implements AbstractFactory{ @Override public Phone getPhone() { return new HuaWeiPhone(); } @Override public Car getCar() { return new HuaWeiCar(); } }
- 小米工厂
package com.liuyc.designpattern.factory.abstactfactory; import com.liuyc.designpattern.factory.*; public class XiaoMiFactory implements AbstractFactory{ @Override public Phone getPhone() { return new XiaoMiPhone(); } @Override public Car getCar() { return new XiaoMiCar(); } }
- 具体使用
package com.liuyc.designpattern.factory.abstactfactory; import com.liuyc.designpattern.factory.Car; import com.liuyc.designpattern.factory.Phone; public class AbstractFactoryMain { public static void main(String[] args) { AbstractFactory abstractFactory = new XiaoMiFactory(); Car car = abstractFactory.getCar(); Phone phone = abstractFactory.getPhone(); car.showInfo(); phone.showInfo(); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring boot框架JWT实现用户账户密码登录验证流程
这篇文章主要介绍了Springboot框架JWT实现用户账户密码登录验证,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-06-06解读@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstr
这篇文章主要介绍了解读@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstructor的区别及在springboot常用地方,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12Java concurrency线程池之Callable和Future_动力节点Java学院整理
这篇文章主要为大家详细介绍了Java concurrency线程池之Callable和Future,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06Springboot工具类FileCopyUtils使用教程
这篇文章主要介绍了Springboot内置的工具类之FileCopyUtils的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧2022-12-12
最新评论