• java设计模式之一工厂模式


    简单工厂模式是java设计模式中最简单的设计模式之一:

    工厂模式是最常用的设计模式之一。 工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时就要多个心眼,是否可以考虑使用工厂模式,虽然这样做,可能多做一些工作,但会给你系统带来更大的可扩展性和尽量少的修改量。

    工厂模式主要一般有简单工厂模式,多个工厂模式,静态工厂模式。

    首先看简单工厂模式:

     1 package com.qcf.test;
     2 /**
     3  * 普通工厂模式
     4  * 工厂模式主要是统一提供对象的引用
     5  * @author Computer
     6  *
     7  */
     8 public class Factory {
     9     public static void main(String[] args) {
    10         CarFactory carFactory=new CarFactory();
    11         Car car =carFactory.getCar("BmwCar");
    12         car.getName();
    13     }
    14 }
    15 /**
    16  * 工厂生产类
    17  * @author Computer
    18  *
    19  */
    20  class CarFactory{
    21      public static Car getCar(String type_car){
    22          if("BmwCar".equals(type_car)){
    23              return new Bmw();
    24          }else if("BigCar".equals(type_car)){
    25              return new BigCar();
    26          }else{
    27              System.out.println("请输入正确的汽车类型");
    28              return null;
    29          }
    30      }
    31      
    32  }
    33 /**
    34  * 接口
    35  * @author Computer
    36  *
    37  */
    38 interface Car{
    39     void getName();    
    40 }
    41 /**
    42  * 实现接口的子类
    43  * @author Computer
    44  *
    45  */
    46 class Bmw implements Car{
    47     @Override
    48     public void getName() {
    49         System.out.println("BMW  Car....");
    50     }
    51 }
    52 /**
    53  * 实现接口的子类
    54  * @author Computer
    55  *
    56  */
    57 class BigCar implements Car{
    58     @Override
    59     public void getName() {
    60         System.out.println("Big Car...");
    61     }
    62 }
    View Code

    多个工厂模式就是修改CarFactory如下:

     1 public class Factory {
     2     public static void main(String[] args) {
     3         CarFactory carFactory=new CarFactory();
     4         Car car =carFactory.productBmwCar();
     5         car.getName();
     6     }
     7 }
     8 /**
     9  * 工厂生产类
    10  * @author Computer
    11  *
    12  */
    13  class CarFactory{
    14      public Car productBmwCar(){
    15          return new Bmw();
    16      }
    17      public Car productBigCar(){
    18          return new BigCar();
    19      }
    20  }
    View Code

    静态工厂模式就是把CarFactory的方法加上static改成静态的,这样不能创建CarFactory的实例就可以生产Car了

     1 public class Factory {
     2     public static void main(String[] args) {
     3         Car car =CarFactory.productBmwCar();
     4         car.getName();
     5     }
     6 }
     7 /**
     8  * 工厂生产类
     9  * @author Computer
    10  *
    11  */
    12  class CarFactory{
    13      public static Car productBmwCar(){
    14          return new Bmw();
    15      }
    16      public static Car productBigCar(){
    17          return new BigCar();
    18      }
    19  }
    View Code
  • 相关阅读:
    USACO 之 Section 2.2 (已解决)
    USACO 之 Section 2.1 (已解决)
    《C++ Primer》学习 之 函数指针相关用法
    《C++ Primer》学习 之 const_cast使用
    《C++ Primer》学习 之 返回数组的引用(返回数组的指针,方法与之相同)
    USACO 之 Section 1.5 (已解决)
    USACO 之 Section 1.4 More Search Techniques (已解决)
    [NN] 对于BackPropagation(BP, 误差反向传播)的一些理解
    [CLPR] 定位算法探幽
    [LeetCode系列] 双单链表共同节点搜索问题
  • 原文地址:https://www.cnblogs.com/quchengfeng/p/4111880.html
Copyright © 2020-2023  润新知