zoukankan      html  css  js  c++  java
  • 行为型模型 空对象模式

    行为型模型 空对象模式

    空对象模式

    在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查。Null 对象不是检查空值,而是反应一个不做任何动作的关系。这样的 Null 对象也可以在数据不可用的时候提供默认的行为。

    在空对象模式中,我们创建一个指定各种要执行的操作的抽象类和扩展该类的实体类,还创建一个未对该类做任何实现的空对象类,该空对象类将无缝地使用在需要检查空值的地方。

    实现

    我们将创建一个定义操作(在这里,是客户的名称)的 AbstractCustomer 抽象类,和扩展了 AbstractCustomer 类的实体类。工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomer 或 NullCustomer 对象。

    NullPatternDemo,我们的演示类使用 CustomerFactory 来演示空对象模式的用法。

    /**
     * 行为型模型 空对象模式
     * 创建一个未对该类做任何实现的空对象类,该空对象类将无缝地使用在需要检查空值的地方。
     * 不要为了屏蔽null而使用空对象,应保持用null,远比用非null的值来替代“无值”要好。(慎用)
     *
     */
    
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <iostream>
    #include <string>
    
    class AbstractCustomer
    {
    public:
        virtual bool isNil() = 0;
        virtual std::string getName() = 0;
        virtual ~AbstractCustomer() {}
    protected:
        std::string name;
    };
    
    class RealCustomer: public AbstractCustomer
    {
    public:
        RealCustomer(std::string name)
        {
            this->name = name;
        }
        virtual std::string getName() override
        {
            return this->name;
        }
        virtual bool isNil() override
        {
            return false;
        }
    };
    
    class NullCustomer: public AbstractCustomer
    {
    public:
        virtual std::string getName() override
        {
            return "Not Available in Customer Database";
        }
        virtual bool isNil()
        {
            return true;
        }
    };
    
    class CustomerFactory
    {
    public:
        static std::string names[];
    
        static AbstractCustomer * getCustomer(std::string name)
        {
            for (unsigned i = 0; i < names->length(); i++)
            {
                if (names[i] == name)
                {
                    return new RealCustomer(name);
                }
            }
            return new NullCustomer();
        }
    
    };
    
    std::string CustomerFactory::names[] = {"Rob", "Joe", "Julie"};
    
    void mytest()
    {
        AbstractCustomer * customer1 = CustomerFactory::getCustomer("Rob");
        AbstractCustomer * customer2 = CustomerFactory::getCustomer("Bob");
        AbstractCustomer * customer3 = CustomerFactory::getCustomer("Julie");
        AbstractCustomer * customer4 = CustomerFactory::getCustomer("Laura");
        
        std::cout << "Customers" << std::endl;
        std::cout << customer1->getName() << std::endl;
        std::cout << customer2->getName() << std::endl;
        std::cout << customer3->getName() << std::endl;
        std::cout << customer4->getName() << std::endl;
    
        delete customer1;
        delete customer2;
        delete customer3;
        delete customer4;
    
        return;
    }
    
    int main()
    {
        mytest();
    
        system("pause");
        return 0;
    }

    验证输出。

    Customers
    Rob
    Not Available in Customer Database
    Julie
    Not Available in Customer Database
  • 相关阅读:
    基于Hadoop和Mapnik的矢量数据渲染技术研究
    GeoServer Cookbook
    Mapnik for java
    GeoServer集群部署-Linux集群
    云原生是什么?Cloud Native
    从0到1开发云GIS
    Python进程池
    【每日一具2】GuoZi(工具箱)一款能听歌、看电影、读小说、下载壁纸的软件
    Python数据格式-CSV
    Python-xlsx文件与csv文件相互转换
  • 原文地址:https://www.cnblogs.com/lsgxeva/p/7783946.html
Copyright © 2011-2022 走看看