设计模式: 自己手动写一适配器和外观模式

适配器模式: 将一个类的接口,转换成客户所期待的接口,适配器让原本不兼容的类可以合作无间。有两种形式:类适配器和对象适配器。前者需要用到多重继承(java不支持),后者要用到组合。

外观模式: 提供了一个统一的简化的接口,用来访问子系统里的一群接口。外观定义了一个高层接口,让子系统更容易使用。

适配器的类图

适配器的一个简单源码:

package adapter;
/**
 * 鸭子接口
 * @author Arvon
 *
 */
public interface Duck {
	public void quack();
	public void fly();
}
package adapter;
/**
 * 火鸡接口
 * @author Administrator
 *
 */
public interface Turkey {
	public void fly();
	public void goggle();
}
package adapter;
/**
 * 一个伪装成火鸡的鸭
 * @author Administrator
 *
 */
public class TurkeyAdapter implements Duck {
	private Turkey turkey;

	public TurkeyAdapter(Turkey turkey) {
		super();
		this.turkey = turkey;
	}

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		turkey.goggle();

	}

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		for(int i=0;i<5;i++)
			turkey.fly();

	}

}
package adapter;

public class MalladDuck implements Duck {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("i am quacking...");

	}

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("i am flying ...");

	}

}

package adapter;

public class WildTurkey implements Turkey {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("i am flying short distance...");

	}

	@Override
	public void goggle() {
		// TODO Auto-generated method stub
		System.out.println("i am goggling....");

	}

}
package adapter;

public class AdapterTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Duck duck = new MalladDuck();
		System.out.println(duck.getClass().getSimpleName()+" says...");
		testDuck(duck);
		//将鸭子接口转换成了火鸡接口
		Duck fakeDuck = new TurkeyAdapter(new WildTurkey());
		System.out.println(fakeDuck.getClass().getSimpleName()+" says...");
		testDuck(fakeDuck);

	}

	private static void testDuck(Duck duck) {
		duck.quack();
		duck.fly();
	}

}

程序输出:

MalladDuck says...

i am quacking...

i am flying ...

TurkeyAdapter says...

i am goggling....

i am flying short distance...

i am flying short distance...

i am flying short distance...

i am flying short distance...

i am flying short distance...

外观模式类图

核心代码:

package facade;
/**
 * 简化了的接口
 * @author Administrator
 *
 */
public class HomeTheaterFacade {
	Amplifier amp;
	Tuner tuner;
	DvdPlayer dvd;
	CdPlayer cd;
	Projector projector;
	TheaterLights lights;
	Screen screen;
	PopcornPopper popper;
	public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayer dvd, CdPlayer cd, Projector projector,
			TheaterLights lights, Screen screen, PopcornPopper popper) {
		super();
		this.amp = amp;
		this.tuner = tuner;
		this.dvd = dvd;
		this.cd = cd;
		this.projector = projector;
		this.lights = lights;
		this.screen = screen;
		this.popper = popper;
	}

	public void watchMovie(String movie) {
		System.out.println("Get ready to watch a movie...");
		popper.on();
		popper.pop();
		lights.dim(10);
		screen.down();
		projector.on();
		projector.wideScreenMode();
		amp.on();
		amp.setDvd(dvd);
		amp.setSurroundSound();
		amp.setVolume(5);
		dvd.on();
		dvd.play(movie);
	}

	public void endMovie() {
		System.out.println("Shutting movie theater down...");
		popper.off();
		lights.on();
		screen.up();
		projector.off();
		amp.off();
		dvd.stop();
		dvd.eject();
		dvd.off();
	}

}
package facade;

public class FacadeTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Amplifier amp = new Amplifier("Top-O-Line Amplifier");
		Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);
		DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp);
		CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);
		Projector projector = new Projector("Top-O-Line Projector", dvd);
		TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
		Screen screen = new Screen("Theater Screen");
		PopcornPopper popper = new PopcornPopper("Popcorn Popper");

		HomeTheaterFacade homeTheater =
				new HomeTheaterFacade(amp, tuner, dvd, cd,
						projector, lights, screen, popper);

		homeTheater.watchMovie("Raiders of the Lost Ark");
		homeTheater.endMovie();

	}

}

其他的类都是子系统的类 这里就不贴出来啦。

程序的输出:

Get ready to watch a movie...

Popcorn Popper on

Popcorn Popper popping popcorn!

Theater Ceiling Lights dimming to 10%

Theater Screen going down

Top-O-Line Projector on

Top-O-Line Projector in widescreen mode (16x9 aspect ratio)

Top-O-Line Amplifier on

Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player

Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer)

Top-O-Line Amplifier setting volume to 5

Top-O-Line DVD Player on

Top-O-Line DVD Player playing "Raiders of the Lost Ark"

Shutting movie theater down...

Popcorn Popper off

Theater Ceiling Lights on

Theater Screen going up

Top-O-Line Projector off

Top-O-Line Amplifier off

Top-O-Line DVD Player stopped "Raiders of the Lost Ark"

Top-O-Line DVD Player eject

Top-O-Line DVD Player off

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 09:49:37

设计模式: 自己手动写一适配器和外观模式的相关文章

读书笔记之设计模式-适配器和外观模式

结构型:Adapter与Facade(适配器和外观模式) 一般作为阅读材料,首先想要明确的是我现在了解的设计模式的初衷,即为了解决什么问题. 适配器,如果有买过港版Iphone在内地使用的人应该会有三角大插头必须接一个转换器才能在一般的插座上使用的情况,当然这只是比较直观的感受.其实我们平时用的手机充电器都是属于适配器,试想如果我们没有这个充电器,我们如何利用普通插座给手机充电? 适配器的定义:将手头上所持有的接口转换成我们需要的接口(业务场景:经常是为了适配旧程序或者对接2套系统的时候使用,当

设计模式 8 —— 适配器和外观模式

设计模式目录: 设计模式 1 ——观察者模式 设计模式 2 —— 装饰者模式 设计模式 3 —— 迭代器和组合模式(迭代器) 设计模式 4 —— 迭代器和组合模式(组合) 设计模式 5 —— 工厂模式 设计模式 6 —— 单件模式 设计模式 7 —— 命令模式 设计模式 8 —— 适配器和外观模式 概述 第1部分 问题引入 第2部分 适配器模式定义 第3部分 对象和类的适配者 第4 部分 适配器模式与装饰者模式区别 第5 部分 外观模式 第1 部分 问题引入 OO适配器是什么,现实中到处都是.比

研磨设计模式解析及python代码实现——(二)外观模式(Facade)

一.外观模式定义 为子系统中的一组接口提供一个一致的界面,使得此子系统更加容易使用. 二.书中python代码实现 1 class AModuleApi: 2 def testA(self): 3 pass 4 class AModuleImpl(AModuleApi): 5 def testA(self): 6 print "Now Call testA in AModule!" 7 class BModuleApi: 8 def testB(self): 9 pass 10 cla

iOS开发-适配器和外观模式

适配器模式,属于结构型模式,其主要作用是将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.适配器模式有对象适配器和类适配器两种,类适配器模式需要语言实现多继承,OC不支持多继承,所以一般我们都实现对象适配器.外观模式提供了一个统一的接口,用来访问子系统中的一群接口,外观定义了一个高层接口,让子系统更容易使用.适配器是为了转换接口,外观模式是为了简化接口. 适配器模式 对象适配器模式UML类图: 关于适配模式最常见的就是手机充电的例子,

适配器和外观模式

一.   基本概述 1:现实中存在三角插头适配成双插头,等其他各种形式的适配器来连接不兼容的两个物体.同理在代码中也存在适配器模式来兼容两个不同的代码接口. 2:KTV包间打开一个启动开关,就打开party模式(音响.屏幕.灯光.换气.点歌台等),一个简单的开关来控制其他更多的任务.同理在代码中也存在外观模式来简化子系统(更多任务)的功能. 二.详细说明 1.适配器模式:将一个类的接口.转换成客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间. 这个适配器模式充满良好的OO设计原则,使

java/android 设计模式学习笔记(14)---外观模式

这篇博客来介绍外观模式(Facade Pattern),外观模式也称为门面模式,它在开发过程中运用频率非常高,尤其是第三方 SDK 基本很大概率都会使用外观模式.通过一个外观类使得整个子系统只有一个统一的高层的接口,这样能够降低用户的使用成本,也对用户屏蔽了很多实现细节.当然,在我们的开发过程中,外观模式也是我们封装 API 的常用手段,例如网络模块.ImageLoader 模块等.其实我们在开发过程中可能已经使用过很多次外观模式,只是没有从理论层面去了解它. 转载请注明出处:http://bl

适配器模式Adapter、外观模式Facade-- 学习HeadFirst设计模式记录

? 适配器模式:将一个类的接口,转换成客户期望的另一个接口,适配器让原本不兼容的类可以合作无间. 外观模式 :提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用. ? 对象适配器: 类适配器: ? 外观模式: ?

设计模式(八):外观模式

这个模式是我觉得最好懂的模式. 外观(Facade)模式 定义: 外观模式是一种结构型模式.它为更大的代码体提供了一个方便的高层次接口,能够隐藏其底层的真实复杂性.简单说就是——小接口有大智慧. 例子: 使用jQuery的$(el).css()或$(el).animate()方法时,实际上我们是在使用Facade:一种更简单的公有接口,使我们不必手动在jQuery核心调用很多内部方法以便实现某些行为. 优点: 1. 易于使用. 2. 实现该模式时占用空间小. 3. 调用者与底层代码解耦. 缺点:

《Head First 设计模式》学习笔记——适配器模式 + 外观模式

在ADO.NET中,对于我们从数据库中取出的数据都要放到一个DataSet中,不管你是Access的数据库,还是SQL的数据库,或者是Oracle的数据库都要放到DataSet中..NET中并没有提供如:SqlDataSet.OleDbDataSet.OracleDataSet等,它只提供了一种DataSet就是用SqlDataAdapte等去填充数据:为什么这一个DataSet能存放不同的数据呢?就是有这些适配器来适配.----题记 设计模式 适配器模式:将一个类的接口,转换成客户期待的另一个