zoukankan      html  css  js  c++  java
  • 每日日报4——软件设计②|简单工厂模式(女娲造人)

    一、简单工厂模式

    https://baike.baidu.com/item/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/8801727?fr=aladdin

    二、版本

    (一)C++版本

    ① 类图:

    ② 效果:

    ③ 代码:

    factory.cpp

     1 #include<iostream>
     2 using namespace std;
     3 
     4 //
     5 class Man {
     6 public:
     7     void make() {
     8         cout << "女娲造男人了!" << endl;
     9     }
    10 };
    11 
    12 //
    13 class Woman {
    14 public:
    15     void make() {
    16         cout << "女娲造女人了!" << endl;
    17     }
    18 };
    19 
    20 //机器人
    21 class Robot {
    22 public:
    23     void make() {
    24         cout << "女娲造机器人了" << endl;
    25     }
    26 };
    27 
    28 class Nvwa {
    29 public:
    30     void factoryMethod() {
    31         Man man;
    32         man.make();
    33         Woman woman;
    34         woman.make();
    35         Robot robot;
    36         robot.make();
    37     }
    38 };
    39 
    40 int main() {
    41     Nvwa nvwa;
    42     nvwa.factoryMethod();
    43 }

    (二)JAVA版本

    ① 类图:

    ② 效果:

    ③ 目录结构:

    ④ 代码:

    Client.java

     1 package factory;
     2 
     3 /**
     4  * 造人测试
     5  * @author Administrator
     6  *
     7  */
     8 public class Client {
     9     public static void main(String[] args) {
    10         //生产男人
    11         Person M;
    12         M=Nvwa.factoryMethod("M");
    13         M.make();
    14         //生产女人
    15         Person W;
    16         W=Nvwa.factoryMethod("W");
    17         W.make();
    18         //生产机器人
    19         Person R;
    20         R=Nvwa.factoryMethod("R");
    21         R.make();
    22     }
    23 }

    Man.java

    1 package factory;
    2 
    3 public class Man implements Person {
    4     public void make() {
    5         System.out.println("女娲要造男人了");
    6     }
    7 }

    Nvwa.java

     1 package factory;
     2 
     3 public class Nvwa {
     4     public static Person factoryMethod(String arg) {
     5         // 判断参数,选择调用哪个类
     6         if (arg.equalsIgnoreCase("M")) {// 男人
     7             return new Man();
     8         } else if (arg.equalsIgnoreCase("W")) {// 女人
     9             return new Woman();
    10         } else if (arg.equalsIgnoreCase("R")) {// 机器人
    11             return new Robot();
    12         } else {
    13             return null;
    14         }
    15     }
    16 }

    Person.java

    1 package factory;
    2 
    3 public interface Person {
    4     public void make();
    5 }

    Robot.java

    1 package factory;
    2 
    3 public class Robot implements Person{
    4     public void make() {
    5         System.out.println("女娲要造机器人了");
    6     }
    7 }

    Woman.java

    1 package factory;
    2 
    3 public class Woman implements Person {
    4     public void make() {
    5         System.out.println("女娲要造女人了");
    6     }
    7 }
  • 相关阅读:
    JavaScript
    94.Binary Tree Inorder Traversal
    144.Binary Tree Preorder Traversal
    106.Construct Binary Tree from Inorder and Postorder Traversal
    105.Construct Binary Tree from Preorder and Inorder Traversal
    90.Subsets II
    78.Subsets
    83.Merge Sorted Array
    80.Remove Duplicates from Sorted Array II
    79.Word Search
  • 原文地址:https://www.cnblogs.com/miao-com/p/15362211.html
Copyright © 2011-2022 走看看