zoukankan      html  css  js  c++  java
  • 软件设计简单工厂模式

    使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。

    类图:

    Java

    package SFP;
    
    /**
     * 简单工厂模式
     */
    
    //女娲造人工厂
    public class NvwaPersonFactory {
    
        //抽象类--人类
        public abstract class Person{
            public void birth(){}
        }
    
        //具体产品类
        public class Man extends Person{
            @Override
            public void birth(){
                System.out.println("女娲造男人。");
                super.birth();
            }
        }
        public class Woman extends Person{
            @Override
            public void birth(){
                System.out.println("女娲造女人。");
                super.birth();
            }
        }
        public class Robot extends Person{
            @Override
            public void birth(){
                System.out.println("女娲造机器人。");
                super.birth();
            }
        }
    
        //造人
        public Person getPerson(String type) {
            Person Person = null;
            if (type.equalsIgnoreCase("M")) {
                Person = new Man();
            } else if (type.equalsIgnoreCase("W")) {
                Person = new Woman();
            } else if (type.equalsIgnoreCase("R")) {
                Person = new Robot();
            }
            return Person;
        }
    
        public static void main(String[] args) {//入口
            Person person;
            NvwaPersonFactory nvwaPersonFactory = new NvwaPersonFactory();
    
            person = nvwaPersonFactory.getPerson("M");
            person.birth();
            person = nvwaPersonFactory.getPerson("W");
            person.birth();
            person = nvwaPersonFactory.getPerson("R");
            person.birth();
        }
    }

    C++

    #include <iostream>
    using namespace std;
    
    /**
     * 简单工厂模式
     */
    
    class Person {
    public:
        virtual void make() {
            cout << "女娲造人" << endl;
        }
    };
    
    class Woman :public Person {
    public:
        void make() {
            cout << "女娲造女人" << endl;
        }
    };
    
    class Man :public Person {
    public:
        void make() {
            cout << "女娲造男人" << endl;
        }
    };
    
    class Robot :public Person {
    public:
        void make() {
            cout << "女娲造机器人" << endl;
        }
    };
    
    class Nvwa {
    public:
        Person* makePerson(string type) {
            Person* person;
            if (type == "W" || type == "w") {
                Woman person1;
                person = &person1;
            }
            else if (type == "M" || type == "m") {
                Man person1;
                person = &person1;
            }
            else if (type == "R" || type == "r") {
                Robot person1;
                person = &person1;
            }
            else if (type == "0") {
                cout << "退出" << endl;
                Person person1;
                person = &person1;
            }
            else {
                cout << "不能乱输嗷!" << endl;
                Person person1;
                person = &person1;
            }
            return person;
        }
    };
    
    int main(){
        string str = "";
        Nvwa nv;
        Person* person;
        do {
            cout << "输入W、M、R  让女娲造人,输入0结束" << endl;
            cin >> str;
            person = nv.makePerson(str);
            person->make();
        } while (str != "0");
        return 0;
    }

  • 相关阅读:
    REST easy with kbmMW #16 – Multiple servers using HTTP.sys transport
    Delphi直接实现分享图片功能
    kbmMW随机数与强密码
    kbmMW基于硬件生成随机数
    用StringHelper.Split分解字符串
    安装和配置HyperServer
    深入了解HyperServer
    Delphi revelations #1 – kbmMW Smart client on NextGen (Android) – Scope problems
    ClientAsTemplate用法
    Java8 Optional总结
  • 原文地址:https://www.cnblogs.com/Arisf/p/15652754.html
Copyright © 2011-2022 走看看