zoukankan      html  css  js  c++  java
  • 设计模式系列

    空对象模式取代简单的 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();
        }
    }
    

    总结

  • 相关阅读:
    flex 遍历Object或者JSON对象内容的实现代码
    Flex Vector使用(转)
    Flex——Array,ArrayCollection,Vector性能比较(转)
    SQLSERVER远程备份、恢复(转)
    隐藏Jquery dialog 按钮
    GSM 短信相关AT指令(转)
    SQL Server 父子迭代查询语句,树状查询(转)
    js framework comparation
    eventEmitter
    调试 shell script 方法
  • 原文地址:https://www.cnblogs.com/hippieZhou/p/10127115.html
Copyright © 2011-2022 走看看