当前位置:   article > 正文

设计模式二(工厂模式)

设计模式二(工厂模式)

本质:实例化对象不用new,用工厂代替,实现了创建者和调用者分离

满足:

开闭原则:对拓展开放,对修改关闭

依赖倒置原则:要针对接口编程

迪米特原则:最少了解原则,只与自己直接相关的类有关系

简单工厂模式

也被称为静态工厂

  1. public interface Car {
  2.    void name();
  3. }
  4. public class BWM implements Car{
  5.    @Override
  6.    public void name() {
  7.        System.out.println("宝马");
  8.   }
  9. }
  10. public class DaZhong implements Car{
  11.    @Override
  12.    public void name() {
  13.        System.out.println("大众");
  14.   }
  15. }
  1. public class CarFactory{
  2.    public static Car getCar(String name){
  3.        if(name.equals("大众")){
  4.            return new DaZhong();
  5.       }else if (name.equals("宝马")){
  6.            return new BWM();
  7.       }else{
  8.            return null;
  9.       }
  10.   }
  11. }
  12. public class consumer {
  13.    public static void main(String[] args) {
  14.        Car car = CarFactory.getCar("大众");
  15.        car.name();
  16.        Car car2 = CarFactory.getCar("宝马");
  17.        car2.name();
  18.   }
  19. }

总结

将创建对象的任务交给工厂去完成

缺点

不满足开闭原则,如果我们新创建一个车,就需要修改CarFactory的源代码

工厂方法模式

多个工厂对应多个实现类

  1. public interface CarFactory {
  2.    Car getCar();
  3. }
  4. public class BMWFactory implements CarFactory{
  5.    @Override
  6.    public Car getCar() {
  7.        return new BWM();
  8.   }
  9. }
  10. public class DaZhongFactory implements CarFactory{
  11.    @Override
  12.    public Car getCar() {
  13.        return new DaZhong();
  14.   }
  15. }

如果我们想要创建新的车对象,只要创建对应的车工厂即可,无需修改CarFactory的代码

  1. public class Aodi implements Car {
  2. @Override
  3. public void name() {
  4. System.out.println("奥迪");
  5. }
  6. }
  7. public class AodiFactory implements CarFactory{
  8. @Override
  9. public Car getCar() {
  10. return new Aodi();
  11. }
  12. }
  13. public class consumer {
  14. public static void main(String[] args) {
  15. Car car = new DaZhongFactory().getCar();
  16. car.name();
  17. Car car1 = new BMWFactory().getCar();
  18. car1.name();
  19. Car car2 = new AodiFactory().getCar();
  20. car2.name();
  21. }
  22. }

抽象工厂模式

围绕一个超级工厂生产工厂,该工厂又称为其他工厂的工厂 (抽象的抽象)

  1. public interface IProductFactory {
  2. IPhoneProduct iphoneproduct();
  3. IRouterProduct irouterproduct();
  4. }

具体的产品工厂

  1. public class XiaomiFactory implements IProductFactory{
  2. @Override
  3. public IPhoneProduct iphoneproduct() {
  4. return new XiaomiPhone();
  5. }
  6. @Override
  7. public IRouterProduct irouterproduct() {
  8. return new XiaomiRouter();
  9. }
  10. }
  1. public class HuaweiFactory implements IProductFactory{
  2. @Override
  3. public IPhoneProduct iphoneproduct() {
  4. return new HuaweiPhone();
  5. }
  6. @Override
  7. public IRouterProduct irouterproduct() {
  8. return new HuaweiRouter();
  9. }
  10. }

产品功能

  1. public interface IPhoneProduct {
  2. void open();
  3. void close();
  4. }
  1. public interface IRouterProduct {
  2. void open();
  3. void close();
  4. }

具体实现

  1. public class XiaomiPhone implements IPhoneProduct{
  2. @Override
  3. public void open() {
  4. System.out.println("小米手机开机");
  5. }
  6. @Override
  7. public void close() {
  8. System.out.println("小米手机关机");
  9. }
  10. }
  1. public class HuaweiPhone implements IPhoneProduct{
  2. @Override
  3. public void open() {
  4. System.out.println("华为手机开机");
  5. }
  6. @Override
  7. public void close() {
  8. System.out.println("华为手机关机");
  9. }
  10. }
  1. public class XiaomiRouter implements IRouterProduct{
  2. @Override
  3. public void open() {
  4. System.out.println("小米路由器开机");
  5. }
  6. @Override
  7. public void close() {
  8. System.out.println("小米路由器关机");
  9. }
  10. }
  1. public class HuaweiRouter implements IRouterProduct{
  2. @Override
  3. public void open() {
  4. System.out.println("华为路由器开机");
  5. }
  6. @Override
  7. public void close() {
  8. System.out.println("华为路由器关机");
  9. }
  10. }

测试

  1. public class consumer {
  2. public static void main(String[] args) {
  3. //先创建工厂
  4. System.out.println("==========小米==========");
  5. IPhoneProduct product = new XiaomiFactory().iphoneproduct();
  6. product.open();
  7. product.close();
  8. IRouterProduct irouterproduct = new XiaomiFactory().irouterproduct();
  9. irouterproduct.open();
  10. irouterproduct.close();
  11. System.out.println("===========华为==========");
  12. IPhoneProduct iphoneproduct = new HuaweiFactory().iphoneproduct();
  13. iphoneproduct.open();
  14. iphoneproduct.close();
  15. IRouterProduct irouterproduct1 = new HuaweiFactory().irouterproduct();
  16. irouterproduct1.open();
  17. irouterproduct1.close();
  18. }
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41952
推荐阅读
相关标签
  

闽ICP备14008679号