zoukankan      html  css  js  c++  java
  • 空对象模式和扩展方法的NULL验证

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //空对象模式和扩展方法的NULL验证
    namespace Chap2_3
    {
    public class NULLObject
    {
    public void Test()
    {
    Promation promation = PromationFactory.Create("水果促销");
    Promation promation1 = PromationFactory.Create(null);
    if (promation.IsNullOrEmpty())
    {
    //扩展方法,判断为NULL
    Console.WriteLine("promation是NULL");
    }
    else
    {
    Console.WriteLine("promation不是NULL");
    }
    if (promation.IsNull)
    {
    Console.WriteLine("promation是NULL");
    }
    else
    {
    Console.WriteLine("promation不是NULL");
    }
    if (promation1.IsNull)
    {
    Console.WriteLine("promation1是NULL");
    }
    else
    {
    Console.WriteLine("promation1不是NULL");
    }
    Console.ReadKey();
    }

    }
    public class Promation
    {
    public virtual bool IsNull
    {
    get { return false; }
    }
    }
    public class NullPromation : Promation
    {
    public override bool IsNull
    {
    get { return true; }
    }
    }
    public class PromationFactory
    {
    public static Promation Create(string promationName)
    {
    if (string.IsNullOrEmpty(promationName))
    {
    return new NullPromation();
    }
    else
    {
    return new Promation();
    }
    }

    }
    //扩展方法
    public static class ObjectIsNUllOrEmpty
    {
    public static bool IsNullOrEmpty(this object obj)
    {
    return obj == null ? true : false;
    }
    }
    }

  • 相关阅读:
    CSS——精灵图与背景图片定位
    CSS——text-indent
    CSS——样式隐藏
    CSS——规避脱标流和vertical-align
    CSS——盒子居中显示
    CSS——img
    CSS——position
    CSS——样式初始化
    CSS——清除浮动
    CSS——float
  • 原文地址:https://www.cnblogs.com/sulong/p/4919654.html
Copyright © 2011-2022 走看看