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