设计模式--行为模式--NULL Object--Java
第一次发现此模式时,是由于Bob大叔的经典著作《Agile Principles, Patterns, and Practices In C#》!
下面是出自书中的例子作为引例:Assets.a = g.newPixmap("a.png", PixmapFormat.ARGB8888);Assets.b = g.newPixmap("b.png", PixmapFormat.ARGB8888);Assets.c = g.newPixmap("c.png", PixmapFormat.ARGB8888);Assets.d = g.newPixmap("d.png", PixmapFormat.ARGB8888);Assets.e = g.newPixmap("e.png", PixmapFormat.ARGB8888);Assets.f = g.newPixmap("f.png", PixmapFormat.ARGB8888);Assets.g = g.newPixmap("g.png", PixmapFormat.ARGB8888);Assets.h = g.newPixmap("h.png", PixmapFormat.ARGB8888);Assets.i = g.newPixmap("i.png", PixmapFormat.ARGB8888);Assets.j = g.newPixmap("j.png", PixmapFormat.ARGB8888);Assets.NULL = new AndroidPixmap(Bitmap.createBitmap(40, 40,Config.ARGB_8888), PixmapFormat.ARGB8888);
通过使用NULL OBJECT 模式,在绘制时根本不用考虑获取的连连看元素的图片是否为null,使得代码更清晰更优美,另一方面使得渲染职责不必知道我要绘制的元素是否合法,符合OO的封装思想。在此我们使用了一个透明的连连看元素作为“NULL OBJECT”。