门面模式

转:http://daimajishu.iteye.com/blog/1085017

一、门面模式说明:

门面模式的特点是:客户端不直接访问系统内部一些复杂的内部类,而通过访问门面来达到操控这些类的目的。门面模式与这些类之间存在聚合关系。可以用如下结构图描述:

门面模式

 

二、Java语言描述:

package com.solid.pattern;

/**

* 门面模式

* @author solid

*/

public class Facade {

private Ring ring;

private Clarm clarm;

private Light light;

public Facade(Ring ring, Clarm clarm, Light light) {

this.ring = ring;

this.clarm = clarm;

this.light = light;

}

public void open() {

light.turnOn();

ring.turnOn();

clarm.turnOn();

}

public void close() {

light.turnOff();

ring.turnOff();

clarm.turnOff();

}

}

package com.solid.pattern;

 

public class Light {

 

public void turnOn() {

System.out.println("===light turnOn===");

}

public void turnOff() {

System.out.println("===light turnOff===");

}

}

package com.solid.pattern;

 

public class Ring {

 

public void turnOn() {

System.out.println("===ring turnOn===");

}

public void turnOff() {

System.out.println("===ring turnOff===");

}

}

package com.solid.pattern;

 

public class Clarm {

 

public void turnOn() {

System.out.println("===clarm turnOn===");

}

public void turnOff() {

System.out.println("===clarm turnOff===");

}

}

package com.solid.pattern;

/**

* 门面模式测试方法

* @author solid

*/

public class TestFacade {

 

private static Ring ring;

private static Light light;

private static Clarm clarm;

public static void main(String[] args) {

ring = new Ring();

light = new Light();

clarm = new Clarm();

Facade facade = new Facade(ring, clarm, light);

facade.open();

facade.close();

}

}

你可能感兴趣的:(java)