空对象模式 - 行为模式

个人理解:    

模式类型:

    Null Object  空对象模式 - 行为模式
    
意图:
    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.
     提供一个对象,作为一个缺少类型对象的代理。

     空对象模式提供智能的,不作为的行为,从它的合作者中隐藏细节。
     
概述:
    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.
    (1)它可以加强系统的稳固性,能有有效地防止空指针报错对整个系统的影响,使系统更加稳定。
    (2)它能够实现对空对象情况的定制化的控制,能够掌握处理空对象的主动权。
    (3)它并不依靠Client来保证整个系统的稳定运行。
    (4)它通过isNull对==null的替换,显得更加优雅,更加易懂。

    
角色:
    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.

结构图:


模式的应用场景:

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.

代码(其实读UML图要比代码还要一目了然):
package com.lee.desingerPattener23.nullobject;

public class NullPatternDemo {
	public static void main(String[] args) {
		AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");
		AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");
		AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie");
		AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");

		System.out.println("Customers");
		System.out.println(customer1.getName());
		System.out.println(customer2.getName());
		System.out.println(customer3.getName());
		System.out.println(customer4.getName());
	}
}

abstract class AbstractCustomer {
	protected String name;
	public abstract boolean isNil();
	public abstract String getName();
}

class RealCustomer extends AbstractCustomer {
	public RealCustomer(String name) {
		this.name = name;
	}
	@Override
	public String getName() {
		return name;
	}
	@Override
	public boolean isNil() {
		return false;
	}
}

class NullCustomer extends AbstractCustomer {
	@Override
	public String getName() {
		return "Not Available in Customer Database";
	}
	@Override
	public boolean isNil() {
		return true;
	}
}

class CustomerFactory {
	public static final String[] names = { "Rob", "Joe", "Julie" };
	public static AbstractCustomer getCustomer(String name) {
		for (int i = 0; i < names.length; i++) {
			if (names[i].equalsIgnoreCase(name)) {
				return new RealCustomer(name);
			}
		}
		return new NullCustomer();
	}
}

结果:

Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database


所有模式:
创建型模式,共五种:工厂方法模式、抽象工厂模式单例模式建造者模式原型模式
结构型模式,共七种:适配器模式装饰器模式代理模式外观模式桥接模式组合模式享元模式
行为型模式,共十一种:策略模式模板方法模式观察者模式迭代子模式责任链模式命令模式备忘录模式状态模式访问者模式中介者模式解释器模式
补充模式:空对象模式
参考/转自:
http://www.tutorialspoint.com/design_pattern/null_object_pattern.htm
http://www.oodesign.com/null-object-pattern.html

转载请注明:     http://blog.csdn.net/paincupid/article/details/46992307

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值