用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

Java设计模式 外观模式建模与实现

2015-04-19 作者: java源代码大全举报

[java]代码库

/**
 * 示例:外观模式,也称门面模式
 * 
 * 优点:为了解决类与类之间的依赖关系,降低了类与类之间的耦合度
 * 
 * 该模式中没有涉及到接口
 */

class Memory {
	public void startup() {
		System.out.println("this is memory startup...");
	}

	public void shutdown() {
		System.out.println("this is memory shutdown...");
	}

}

class CPU {
	public void startup() {
		System.out.println("this is CPU startup...");
	}

	public void shutdown() {
		System.out.println("this is CPU shutdown...");
	}
}

/**
 * 作为facade,持有Memory、CPU的实例
 * 
 * 任务让Computer帮咱们处理,我们无需直接和Memory、CPU打交道
 * 
 * 这里有点像去商店里买东西:咱们买东西只需要到商店去买,而无需去生产厂家那里买。
 * 
 * 商店就可以称为是一个facade外观(门面)模式。--> 商品都在商店里
 */
class Computer {
	private Memory memory;
	private CPU cpu;

	public Computer() {
		memory = new Memory();
		cpu = new CPU();
	}

	public void startup() {
		System.out.println("begin to start the computer...");
		memory.startup();
		cpu.startup();
		System.out.println("computer start finished...");
	}

	public void shutdown() {
		System.out.println("begin to close the computer...");
		memory.shutdown();
		cpu.shutdown();
		System.out.println("computer close finished...");
	}
}

/**
 * 客户端测试类
 * 
 * @author Leo
 */
public class Test {
	public static void main(String[] args) {
		Computer computer = new Computer();
		computer.startup();
		System.out.println("\n");
		computer.shutdown();
	}
}//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...