设计模式之工厂模式

39 篇文章 1 订阅
35 篇文章 2 订阅

工厂模式只适用于对象的创建。说白了就是封装了new‘一个对象的操作。


简单工厂模式

1. 目的 
        工厂模式就是专门负责将大量有共同接口的类实例化,

而且不必事先知道每次是要实例化哪一个类的模式。

它定义一个用于创建对象的接口,由子类决定实例化哪一个类。
2 . 简单工厂模式的结构 
 https://i-blog.csdnimg.cn/blog_migrate/d4323c8156b71b0675d08c42ac1327b4.gif

3. 一个简单例子
java 代码
  1. // 产品接口         
  2. public interface Product {   
  3.   
  4.     public void getName();   
  5.   
  6. }   
  7.   
  8. // 具体产品A   
  9. public class ProductA implements Product {   
  10.   
  11.     public void getName() {   
  12.         System.out.println("  I am ProductA  ");   
  13.     }   
  14.   
  15. }   
  16.   
  17. // 具体产品B   
  18. public class ProductB implements Product {   
  19.   
  20.     public void getName() {   
  21.         System.out.println("  I am ProductB  ");   
  22.     }   
  23.   
  24. }   
  25.   
  26. // 工厂类   
  27. public class ProductCreator {   
  28.   
  29.     public Product createProduct(String type) {   
  30.         if (" A ".equals(type)) {   
  31.             return new ProductA();   
  32.         }   
  33.         if (" B ".equals(type)) {   
  34.             return new ProductB();   
  35.         } else  
  36.             return null;   
  37.     }   
  38.   
  39.     public static void main(String[] args) {   
  40.         ProductCreator creator = new ProductCreator();   
  41.         creator.createProduct(" A ").getName();   
  42.         creator.createProduct(" B ").getName();   
  43.     }   
  44. }  
这样做的好处直白一点讲就是: 
1. 类的创建可以被配置。 字符串A和B都可以写在配置文件里。
2. 类的创建隐藏了new操作符。

 

抽象工厂模式 

1. 抽象工厂模式可以说是简单工厂模式的扩展,

它们主要的区别在于需要创建对象的复杂程度上。 
在抽象工厂模式中,创建对象的方法变成了接口(对应与c++的虚函数),可以让具体的创建对象的子类来实现。

2. 抽象工厂模式的结构 

 https://i-blog.csdnimg.cn/blog_migrate/784869f71398b122971e48cf1509f842.gif

3. 一个简单例子

java 代码
  1. //  产品 Plant接口         
  2. public interface Plant {   
  3. }   
  4.   
  5. // 具体产品PlantA,PlantB   
  6. public class PlantA implements Plant {   
  7.   
  8.     public PlantA() {   
  9.         System.out.println(" create PlantA ! ");   
  10.     }   
  11.   
  12.     public void doSomething() {   
  13.         System.out.println("  PlantA do something  ");   
  14.     }   
  15. }   
  16.   
  17. public class PlantB implements Plant {   
  18.     public PlantB() {   
  19.         System.out.println(" create PlantB ! ");   
  20.     }   
  21.   
  22.     public void doSomething() {   
  23.         System.out.println("  PlantB do something  ");   
  24.     }   
  25. }   
  26.   
  27. // 产品 Fruit接口   
  28. public interface Fruit {   
  29. }   
  30.   
  31. // 具体产品FruitA,FruitB   
  32. public class FruitA implements Fruit {   
  33.     public FruitA() {   
  34.         System.out.println(" create FruitA ! ");   
  35.     }   
  36.   
  37.     public void doSomething() {   
  38.         System.out.println("  FruitA do something  ");   
  39.     }   
  40. }   
  41.   
  42. public class FruitB implements Fruit {   
  43.     public FruitB() {   
  44.         System.out.println(" create FruitB ! ");   
  45.     }   
  46.   
  47.     public void doSomething() {   
  48.         System.out.println("  FruitB do something  ");   
  49.     }   
  50. }   
  51.   
  52. // 抽象工厂方法   
  53. public interface AbstractFactory {   
  54.     public Plant createPlant();   
  55.   
  56.     public Fruit createFruit();   
  57. }   
  58.   
  59. // 具体工厂方法   
  60. public class FactoryA implements AbstractFactory {   
  61.     public Plant createPlant() {   
  62.         return new PlantA();   
  63.     }   
  64.   
  65.     public Fruit createFruit() {   
  66.         return new FruitA();   
  67.     }   
  68. }   
  69.   
  70. public class FactoryB implements AbstractFactory {   
  71.     public Plant createPlant() {   
  72.         return new PlantB();   
  73.     }   
  74.   
  75.     public Fruit createFruit() {   
  76.         return new FruitB();   
  77.     }   
  78. }  
任何设计模式都是服务于实际的项目的。用复杂的术语来描述的设计模式你读一万遍你
都无法理解,还不如在你实际的项目中采用。或者看现有项目的代码是如何用的。

工厂模式是很简单的,不要被复杂的名词或者是术语迷惑。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值