NULL Object空对象模式:当你在处理可能会出现null的对象时,可能要产生相对乏味的代码来做相应的处理,使用空对象模式可以接受null,并返回相应的信息。 空对象模式通常会作为一个单独的空对象类,封装一个默认的行为。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NullObjectPattern
{
public interface INullable
{
bool IsNull { get; }
}
public interface IUser : INullable
{
void Login();
void GetInfo();
}
public class User : IUser
{
public void Login()
{
Console.WriteLine("User Login now.");
}
public void GetInfo()
{
Console.WriteLine("User Logout now.");
}
public bool IsNull
{
get { return false; }
}
}
public class NullUser : IUser
{
public void Login()
{
}
public void GetInfo()
{
}
public bool IsNull
{
get { return true; }
}
}
public class UserManage
{
private IUser user = new User();
public IUser User
{
get { return user; }
set { user = value ?? new NullUser(); }
}
}
class Client
{
static void Main(string[] args)
{
UserManage manager = new UserManage();
//manager.User = new User();
manager.User = null;
manager.User.Login();
manager.User.GetInfo();
if (manager.User.IsNull)
{
Console.WriteLine("User isn't exist,Please Cheack it!");
}
}
}
}
//1.valid solves the state of when object is Nullable.
//2.ensured can return a valid default value.
//example:
//public void SendMessageAll(List<User> userList)
//{
// foreach(User user in userList)
// {
// user.SendMessage();
// }
//}
//3.Supply unified judge the Property of IsNull,by Implement INullable or Extension Method.
//4.null Object Should be keep the invariability of the Object's Members,so we Implement Singleton Pattern generally.
//5.when the executed the method,return "null object" and non "null",can avoid NullReferenceException.
using System.Linq;
using System.Text;
namespace NullObjectPattern
{
public interface INullable
{
bool IsNull { get; }
}
public interface IUser : INullable
{
void Login();
void GetInfo();
}
public class User : IUser
{
public void Login()
{
Console.WriteLine("User Login now.");
}
public void GetInfo()
{
Console.WriteLine("User Logout now.");
}
public bool IsNull
{
get { return false; }
}
}
public class NullUser : IUser
{
public void Login()
{
}
public void GetInfo()
{
}
public bool IsNull
{
get { return true; }
}
}
public class UserManage
{
private IUser user = new User();
public IUser User
{
get { return user; }
set { user = value ?? new NullUser(); }
}
}
class Client
{
static void Main(string[] args)
{
UserManage manager = new UserManage();
//manager.User = new User();
manager.User = null;
manager.User.Login();
manager.User.GetInfo();
if (manager.User.IsNull)
{
Console.WriteLine("User isn't exist,Please Cheack it!");
}
}
}
}
//1.valid solves the state of when object is Nullable.
//2.ensured can return a valid default value.
//example:
//public void SendMessageAll(List<User> userList)
//{
// foreach(User user in userList)
// {
// user.SendMessage();
// }
//}
//3.Supply unified judge the Property of IsNull,by Implement INullable or Extension Method.
//4.null Object Should be keep the invariability of the Object's Members,so we Implement Singleton Pattern generally.
//5.when the executed the method,return "null object" and non "null",can avoid NullReferenceException.