标签:des style blog class code java
索引
通过对缺失对象的封装,以提供默认无任何行为的对象替代品。
Encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior.
In short, a design where "nothing will come of nothing".
AbstractObject
RealObject
NullObject
Client
当以下情况成立时可以使用 Null Object 模式:
1 namespace NullObjectPattern.Implementation1 2 { 3 public interface ILog 4 { 5 void Write(string message); 6 } 7 8 public class ConsoleLog : ILog 9 { 10 public void Write(string message) 11 { 12 Console.WriteLine(message); 13 } 14 } 15 16 public class NullLog : ILog 17 { 18 public void Write(string message) 19 { 20 // do nothing 21 } 22 } 23 24 public class Client 25 { 26 public void TestCase1() 27 { 28 ILog log1 = new ConsoleLog(); 29 ILog log2 = new NullLog(); 30 31 log1.Write("message to log"); 32 log2.Write("message to log"); 33 } 34 } 35 }
《设计模式之美》为 Dennis Gao 发布于博客园的系列文章,任何未经作者本人同意的人为或爬虫转载均为耍流氓。
设计模式之美:Null Object(空对象),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/gaochundong/p/design_pattern_null_object.html