|
Introduce Null Object (1)
|
| (¥v«Òªâ, 2008/06/08, hi.steven@gmail.com) |
·íµ{¦¡¤¤¤@¦A¥X²{¡AÀˬd¬Yª«¥ó¬O§_¬°null®É¡A¥i¦Ò¼{¤Þ¤Jnullª«¥ó¡C ¬°source class«Ø¥ß¤@Ó¤lÃþ§O¡A¨Ï¨ä¦æ¬°¹³source classªºnullª©¥»¡A¦bsource class¤Î ¤lÃþ§O¤¤³£¥[¤JisNull() method¡Asource classªºisNull()¶Ç¦^false¡A¤lÃþ§OªºisNull() ¶Ç¦^true¡C §ä¥X©Ò¦³µ{¦¡¤¤¡AÀˬdsource object¬O§_¬°nullªºµ{¦¡½X¡A±N¥¦Ì³£§ï¬°¨Ï¥ÎisNull()Àˬd¡C °²³]¦³Ó«È¤áºÞ²z¨t²Î¡A¨t²Î¸Ì»Ýn°O¿ý«È¤áªº©m¦W©M¹q¤l¶l¥ó«H½c¡A¨t²Î¥i¥H¦C¥X©Ò¦³«È¤áªº ¸ê®Æ¡A¦b³o¼Ëªº°²³]«e´£¤U¡A«Ø¥ß¤F¦p¤Uªºµ{¦¡:
using System;
namespace NullObjectPrj
{
public class Customer
{
private readonly Label _name;
private readonly Label _mail;
public Customer(Label name, Label mail)
{
_name = name;
_mail = mail;
}
public Customer(Label name) : this(name, null)
{
}
public void display()
{
if (_name != null)
{
_name.display();
}
if (_mail != null)
{
_mail.display();
}
}
public override string ToString()
{
string result = "[ Customer:";
result += " name=";
if (_name == null)
{
result += "\"(none)\"";
}
else
{
result += _name;
}
result += " mail=";
if (_mail == null)
{
result += "\"(none)\"";
}
else
{
result += _mail;
}
result += " ]";
return result;
}
}
public class Label
{
private readonly string _label;
public Label(string label)
{
_label = label;
}
public void display()
{
Console.WriteLine("display: " + _label);
}
public override string ToString()
{
return "\"" + _label + "\"";
}
}
class Program
{
static void Main(string[] args)
{
Customer[] customer = {
new Customer(new Label("Steven"), new Label("steven@idealist.idv.tw")),
new Customer(new Label("Shelley"), new Label("shelley@idealist.idv.tw")),
new Customer(new Label("Alice"))
};
foreach (Customer cstm in customer)
{
Console.WriteLine(cstm.ToString());
cstm.display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
¤W±µ{¦¡¡ACustomer classªºdisplay() method¤ÎToString() method¤¤¡A
¤@¦A«½ÆªºÀˬdLabel object¬O§_¬°null¡Aµ{¦¡¬Ý°_¨Ó¬Û·íÂø¶Ã¡A³o¥¿¬O¥i¦Ò¼{¨Ï¥Înull object¥H²
¤Æµ{¦¡ªº¦a¤è¡A³z¹L¾É¤JNull Object¡A¥iÅýµ{¦¡¬Ý°_¨Ó§ó²³æ©úÁA¡C¦b¶}©l«ºc«e¡A§ÚÌ¥ý¬Ý¤@¤U°õ¦æªºµ²ªG¡A±µ¤U¨Óªº«ºc¹Lµ{¡A¨C§¹¦¨¤@Ó¨BÆJ ¡A³£À³½sĶ¨Ã´ú¸Õ¤@¤U¡A½T©w¤£ºÞµ{¦¡½X¦p¦ó§ïÅÜ¡A°õ¦æµ²ªG³£¤£ÅÜ¡C
¦p¤Uµ{¦¡¡A¬°source class - Label«Ø¥ß¤@Ó¤lÃþ§ONullLabel¡A±µµÛ¦bLabel¤ÎNullLabel¤¤³£¥[¤J isNull() method¡ALabelªºisNull()¶Ç¦^false¡ANullLabelªºisNull()¶Ç¦^true¡C§ï¦n«á½sĶ¨Ã´ú¸Õ ¤@¤U¡A¬Ýµ²ªG¬O§_¥¿½T¡C
using System;
namespace NullObjectPrj
{
public class Customer
{
private readonly Label _name;
private readonly Label _mail;
public Customer(Label name, Label mail)
{
_name = name;
_mail = mail;
}
public Customer(Label name)
: this(name, null)
{
}
public void display()
{
if (_name != null)
{
_name.display();
}
if (_mail != null)
{
_mail.display();
}
}
public override string ToString()
{
string result = "[ Customer:";
result += " name=";
if (_name == null)
{
result += "\"(none)\"";
}
else
{
result += _name;
}
result += " mail=";
if (_mail == null)
{
result += "\"(none)\"";
}
else
{
result += _mail;
}
result += " ]";
return result;
}
}
public class Label
{
private readonly string _label;
public Label(string label)
{
_label = label;
}
// ·s¼W
virtual public bool isNull()
{
return false;
}
//×§ï, ¥[¤WvirtualÅý¤lÃþ§O¥i¥Hoverride¡C
virtual public void display()
{
Console.WriteLine("display: " + _label);
}
public override string ToString()
{
return "\"" + _label + "\"";
}
}
// ·s¼W
public class NullLabel : Label
{
public NullLabel() : base("(none)") { }
public override bool isNull()
{
return true;
}
override public void display()
{
//¦]¬°¬OªÅªº¡A©Ò¥H¤£¦C¦L¥X¥ô¦óªF¦è¡C
}
}
class Program
{
static void Main(string[] args)
{
Customer[] customer = {
new Customer(new Label("Steven"), new Label("steven@idealist.idv.tw")),
new Customer(new Label("Shelley"), new Label("shelley@idealist.idv.tw")),
new Customer(new Label("Alice"))
};
foreach (Customer cstm in customer)
{
Console.WriteLine(cstm.ToString());
cstm.display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
¥ÎisNull()¨ú¥NCustomer¤¤ªº±ø¥ó§PÂ_¡A±o¨ì¦p¤Uªºµ{¦¡¡A¦¹®Éµ{¦¡³z¹LisNull()¨Ó§PÂ_¡A »y·N¤W¬O¤ñ¸û²M´·¤F¡A¦ý¤´¨ì³B³£¬O§PÂ_¦¡¡C
using System;
namespace NullObjectPrj
{
public class Customer
{
private readonly Label _name;
private readonly Label _mail;
public Customer(Label name, Label mail)
{
_name = name;
_mail = mail;
}
public Customer(Label name)
: this(name, new NullLabel()) //ק
{
}
public void display()
{
if (!_name.isNull()) //ק
{
_name.display();
}
if (!_mail.isNull()) //ק
{
_mail.display();
}
}
public override string ToString()
{
string result = "[ Customer:";
result += " name=";
if (_name.isNull()) //ק
{
result += "\"(none)\"";
_name.display();
}
else
{
result += _name;
}
result += " mail=";
if (_mail.isNull()) //ק
{
result += "\"(none)\"";
}
else
{
result += _mail;
}
result += " ]";
return result;
}
}
public class Label
{
private readonly string _label;
public Label(string label)
{
_label = label;
}
virtual public bool isNull()
{
return false;
}
virtual public void display()
{
Console.WriteLine("display: " + _label);
}
public override string ToString()
{
return "\"" + _label + "\"";
}
}
public class NullLabel : Label
{
public NullLabel() : base("(none)") { }
public override bool isNull()
{
return true;
}
override public void display()
{
}
}
class Program
{
static void Main(string[] args)
{
Customer[] customer = {
new Customer(new Label("Steven"), new Label("steven@idealist.idv.tw")),
new Customer(new Label("Shelley"), new Label("shelley@idealist.idv.tw")),
new Customer(new Label("Alice"))
};
foreach (Customer cstm in customer)
{
Console.WriteLine(cstm.ToString());
cstm.display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
²¾°£±ø¥ó§PÂ_«á¡Aµ{¦¡Åܱo²M´·©úÁA¡A«ºc¤j¥\§i¦¨! §ó¶i¶¥ªº¤âªk¡A¦b Introduce Null Object (2)¦A¤¶²Ð¡C
using System;
namespace NullObjectPrj
{
public class Customer
{
private readonly Label _name;
private readonly Label _mail;
public Customer(Label name, Label mail)
{
_name = name;
_mail = mail;
}
public Customer(Label name)
: this(name, new NullLabel())
{
}
public void display()
{
//×§ï
_name.display();
_mail.display();
}
//ק
public override string ToString()
{
string result = "[ Customer: name=" + _name + " mail=" + _mail + " ]";
return result;
}
}
public class Label
{
private readonly string _label;
public Label(string label)
{
_label = label;
}
virtual public bool isNull()
{
return false;
}
virtual public void display()
{
Console.WriteLine("display: " + _label);
}
public override string ToString()
{
return "\"" + _label + "\"";
}
}
public class NullLabel : Label
{
public NullLabel() : base("(none)") { }
public override bool isNull()
{
return true;
}
override public void display()
{
}
}
class Program
{
static void Main(string[] args)
{
Customer[] customer = {
new Customer(new Label("Steven"), new Label("steven@idealist.idv.tw")),
new Customer(new Label("Shelley"), new Label("shelley@idealist.idv.tw")),
new Customer(new Label("Alice"))
};
foreach (Customer cstm in customer)
{
Console.WriteLine(cstm.ToString());
cstm.display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
|