⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 设计模式描述设计模式描述设计模式描述设计模式描述
💻 JAVA
字号:
/*
 * Created on 2005-5-11
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.test.pattern.struct.adapter;

/**
 * @author Administrator
 * 
 * 意图 将一个类的接口转换成客户希望的另外一个接口。
 * A d a p t e r 模式使得原本由于接口不兼容而不能
 * 一起工作的那些类可以一起工作。 
 * 适用性你想使用一个已经存在的类,而它的接口不符合
 * 你的需求。
 * 你想创建一个可以复用的类,该类可以与其他不相关的
 * 类或不可预见的类(即那些接口可能不一定兼容的类)
 * 协同工作。 
 * (仅适用于对象A d a p t e r)你想使用一些已经
 * 存在的子类,但是不可能对每一个都进行子类化以匹配
 * 它们的接口。对象适配器可以适配它的父类接口。
 * 
 *  
 */
class FrameworkXTarget {
	public void SomeRequest(int x) {
		// normal implementation of SomeRequest goes here
		System.out.println("X target request");
	}
}

class FrameworkYAdaptee {
	public void QuiteADifferentRequest(String str) {
		System.out.println("QuiteADifferentRequest = " + str);
	}
}

class OurAdapter extends FrameworkXTarget {
	private FrameworkYAdaptee adaptee = new FrameworkYAdaptee();

	public void SomeRequest(int a) {
		String b;
		b = "" + a;
		adaptee.QuiteADifferentRequest(b);
	}
}

/// <summary>
/// Summary description for Client.
/// </summary>

public class Client {
	void GenericClientCode(FrameworkXTarget x) {
		// We assume this function contains client-side code that only
		// knows about FrameworkXTarget.
		x.SomeRequest(4);
		// other calls to FrameworkX go here
		// ...
	}

	public static void main(String[] args) {
		Client c = new Client();
		FrameworkXTarget x = new OurAdapter();
		c.GenericClientCode(x);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -