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();
        }
    }
    

    总结

  • 相关阅读:
    Delegate(委托与事件)
    eclipse2020-06创建属于自己的JSP模板(图文)
    eclipse没有新建web项目的解决问题
    my97datepicker实现日期改变立刻触发函数
    jetty启动项目后js修改后无法保存
    js连续的日期判断,判断相差几天
    同步和异步
    面试题
    MYSQL 数据库名、表名、字段名查询
    Spring-MVC
  • 原文地址:https://www.cnblogs.com/hippieZhou/p/10127115.html
Copyright © 2011-2022 走看看