• 设计模式系列


    空对象模式取代简单的 NULL 值判断,将空值检查作为一种不做任何事情的行为。

    介绍

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

    类图描述

    代码实现

    1、定义抽象类

        public abstract class AbstractCustomer
        {
            protected string Name;
            public abstract bool IsNil();
            public abstract string GetName();
        }
    

    2、定义实体类

    public class NullCustomer : AbstractCustomer
    {
        public override string GetName()
        {
            return "Not Available in Customer Database";
        }
    
        public override bool IsNil()
        {
            return true;
        }
    }
    
    public class RealCustomer : AbstractCustomer
    {
        public RealCustomer(string name)
        {
            Name = name;
        }
        public override string GetName()
        {
            return this.Name;
        }
    
        public override bool IsNil()
        {
            return false;
        }
    }
    

    3、定义工厂类

    public class CustomerFactory
    {
        public static readonly string[] names = { "Rob", "Joe", "Julie" };
    
        public static AbstractCustomer GetCustomer(string name)
        {
            for (int i = 0; i < names.Length; i++)
            {
                if (names[i] == name)
                    return new RealCustomer(names[i]);
            }
            return new NullCustomer();
        }
    }
    

    4、上层调用

    class Program
    {
        static void Main(string[] args)
        {
            AbstractCustomer customer1 = CustomerFactory.GetCustomer("Rob");
            AbstractCustomer customer2 = CustomerFactory.GetCustomer("Bob");
            AbstractCustomer customer3 = CustomerFactory.GetCustomer("Julie");
            AbstractCustomer customer4 = CustomerFactory.GetCustomer("Laura");
            Console.WriteLine("Customers");
            Console.WriteLine(customer1.GetName());
            Console.WriteLine(customer2.GetName());
            Console.WriteLine(customer3.GetName());
            Console.WriteLine(customer4.GetName());
    
            Console.ReadKey();
        }
    }
    

    总结

  • 相关阅读:
    c++ --> #define中的三个特殊符号:#,##,#@
    网络通信 --> ZMQ安装和使用
    利用 mount 指令解决 Read-only file system的问题
    sed学习总结
    Verilog中锁存器与多路选择器
    Debian耳机声音问题
    MM32/STM32中断和事件梳理
    MM32 备份域学习(兼容STM32)
    有限状态机FSM(自动售报机Verilog实现)
    MM32 RTC学习(兼容STM32)
  • 原文地址:https://www.cnblogs.com/hippieZhou/p/10127115.html
Copyright © 2020-2023  润新知