zoukankan      html  css  js  c++  java
  • 工厂模式

    简单工厂模式

     1 package com.java.factory;
     2 
     3 /**
     4  * 简单工厂模式
     5  * 
     6  */
     7 public class SimpleFactoryTest {
     8     public static void main(String[] args) {
     9         Mobile android = SimpleFactory.createMobile("安卓");
    10         System.out.println("得到android的实例" + android);
    11 
    12         Mobile iPhone = SimpleFactory.createMobile("苹果");
    13         System.out.println("得到iPhone的实例" + iPhone);
    14     }
    15 
    16 }
    17 
    18 /**
    19  * 简单工厂设计模式
    20  */
    21 class SimpleFactory {
    22 
    23     public static Mobile createMobile(String type) {
    24         if ("安卓".equals(type)) {
    25             return new Android();
    26         } else if ("苹果".equals(type)) {
    27             return new Iphone();
    28         }
    29 
    30         return null;
    31     }
    32 
    33 }
    34 
    35 /**
    36  * 手机类
    37  */
    38 interface Mobile {
    39 
    40     // 打电话
    41 
    42     // 发短信
    43 
    44     // 上网
    45 
    46 }
    47 
    48 /**
    49  * Android手机类
    50  */
    51 class Android implements Mobile {
    52 
    53     // 打电话
    54 
    55     // 发短信
    56 
    57     // 上网
    58 
    59 }
    60 
    61 /**
    62  * Iphone手机类
    63  */
    64 class Iphone implements Mobile {
    65 
    66     // 打电话
    67 
    68     // 发短信
    69 
    70     // 上网
    71 
    72 }
    73 
    74 /**
    75  * WinPhone手机类
    76  */
    77 class WinPhone implements Mobile {
    78     // 打电话
    79 
    80     // 发短信
    81 
    82     // 上网
    83 
    84 }

    工厂方法 模式

     1 package com.java.factory;
     2 
     3 /**
     4  * 工厂方法 模式
     5  * 
     6  */
     7 public class FactoryMethodTest {
     8     public static void main(String[] args) {
     9         // 生产卡车
    10         FactorySteelworks truck = new FactoryTruck();
    11         // 得到钢铁资源
    12         Truck steel = (Truck) truck.createSteel();
    13         System.out.println("得到Truck" + steel);
    14 
    15         FactorySteelworks car = new FactoryCar();
    16         Car steel2 = (Car) car.createSteel();
    17         System.out.println("得到Car" + steel2);
    18 
    19     }
    20 }
    21 
    22 interface Mobiles {
    23 
    24 }
    25 
    26 /**
    27  * 钢铁厂
    28  */
    29 interface FactorySteelworks {
    30     public Mobiles createSteel();
    31 }
    32 
    33 /**
    34  * 卡车工厂【固定生产卡车】
    35  */
    36 class FactoryTruck implements FactorySteelworks {
    37     @Override
    38     public Mobiles createSteel() {
    39         return new Truck();
    40     }
    41 }
    42 
    43 /**
    44  * 跑车厂商【固定生产跑车】
    45  */
    46 class FactoryCar implements FactorySteelworks {
    47     @Override
    48     public Mobiles createSteel() {
    49         return new Car();
    50     }
    51 }
    52 
    53 /**
    54  * 卡车
    55  */
    56 class Truck implements Mobiles {
    57 
    58 }
    59 
    60 /**
    61  * 跑车
    62  */
    63 class Car implements Mobiles {
    64 
    65 }
  • 相关阅读:
    cmd中删除、添加、修改注册表命令
    修改注册表使win server 2012R2开机进入桌面而不是开始界面
    win8.1/2012R2上面安装flash debugger
    ANT中的copy和move标签
    用maven在MANIFEST.MF文件中的Class-Path中增加当前目录(.)
    通过ANT生成MANIFEST.MF中的Class-Path属性
    Junit4进行参数化测试
    DbUnit入门实战
    oracle查看当前正在使用的数据库
    左偏树 P3377【模板】左偏树(可并堆)
  • 原文地址:https://www.cnblogs.com/yonxin/p/12554801.html
Copyright © 2011-2022 走看看