空对象模式
来源:互联网 发布:辅舒良与内舒拿 知乎 编辑:程序博客网 时间:2024/05/16 23:47
原文地址:http://www.oodesign.com/null-object-pattern.html
Null Object Pattern
Motivation
There are some cases when a system has to use some functionality and some cases when it doesn't. Let's say we have to implement a class that should send the results to a log file or to the console. But this is just an additional option and the data is logged depending on the configuration values.
If there are cases when the client module does not have to log any data then it has to check the configuration parameter in and if block and then to call or not the Logger class. But as we know the 'if' block is not an elegant solution.
Intent
- Provide an object as a surrogate for the lack of an object of a given type.
- The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators.
Implementation
The participants classes in this pattern are:- AbstractClass - defines abstract primitive operations that concrete implementations have to define.
- RealClass - a real implementation of the AbstractClass performing some real actions.
- NullClass - a implementation which do nothing of the abstract class, in order to provide a non-null object to the client.
- Client - the client gets an implementation of the abstract class and uses it. It doesn't really care if the implementation is a null object or an real object since both of them are used in the same way.
Applicability & Examples
Example: Log System
Let's say we need a logging framework in order to support the logging of an application. The framework must fulfill the following requirements:- The destination of the output messages should be selected from a configuration file and it can be one of the following options: Log File, Standard Console or Log Disabled.
- Must be open for extension; new logging mechanism can be added without touching the existing code.
Specific problems and implementation
Null Object and Factory
The Null Object design pattern is more likely to be used in conjunction with the Factory pattern. The reason for this is obvious: A Concrete Classes need to be instantiated and then to be served to the client. The client uses the concrete class. The concrete class can be a Real Object or a Null Object.
Null Object and Template Method
The Template method design pattern need to define an abstract class that define the template and each concrete class implements the steps for the template. If there are cases when sometimes template is called and sometimes not then, in order to avoid the checking a Null Object can be use to implement a Concrete Template that does nothing.
Removing old functionality
The Null Object can be used to remove old functionality by replacing it with null objects. The big advantage is that the existing code doesn't need to be touched.
Conclusion
The Null Object Pattern is used to avoid special if blocks for do nothing code, by putting the “do nothing” code in the Null Object which becomes responsible for doing nothing. The client is not aware anymore if the real object or the null object is called so the 'if' section is removed from client implementation.
- 空对象模式
- 空对象模式
- 空对象模式
- 空对象模式
- Java空对象模式
- Java空对象模式
- 空对象模式 - 行为模式
- Java空对象设计模式
- 设计模式学习笔记--空对象模式
- 行为型模式--空对象模式
- 设计模式---空对象模式(C++实现)
- java设计模式之空对象模式
- 设计模式【空对象模式NullObjectPattern】
- 设计模式(19)--空对象模式
- 空对象模式(Null Object Pattern)
- Java Null Object Pattern(空对象模式)
- 设计模式:空对象模式(Null Object Pattern)
- 设计模式之空对象(Null Object)模式
- MySQL 求时间差
- cocos2dx3.6 实现带光标的输入框(二);光标可移动
- 编程思想之多线程与多进程(1)——以操作系统的角度述说线程与进程
- leetcode 16: 3Sum Closest
- MAC处理windows上传的txt文件 换行问题
- 空对象模式
- 设计模式--开放/封闭原则(OCP)
- Ubuntu12 安装XX-Net完整流程
- 2012年5月SAT香港真题解析
- leetcode 17: Letter Combinations of a Phone Number
- Linux内核源码分析--内核启动之(2)Image内核启动(汇编部分)(Linux-3.0 ARMv7)
- iOS--NSString---过滤换行符和空白字符
- leetcode 18: 4Sum
- Navicat for mysql 远程连接 mySql数据库10061错误问题