适配器模式:AdvisorAdapter

public interface AdvisorAdapter {
	// 判断此适配器是否支持特定的Advice
	boolean supportsAdvice(Advice advice);
	//将一个Advisor适配成MethodInterceptor
	MethodInterceptor getInterceptor(Advisor advisor);
}
class AfterReturningAdviceAdapter implements AdvisorAdapter, Serializable {}
class ThrowsAdviceAdapter implements AdvisorAdapter, Serializable {}
class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
	@Override
	public boolean supportsAdvice(Advice advice) {
		return (advice instanceof MethodBeforeAdvice);
	}
	@Override
	public MethodInterceptor getInterceptor(Advisor advisor) {
		MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
		return new MethodBeforeAdviceInterceptor(advice);
	}
}
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
	private MethodBeforeAdvice advice;
	public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
		Assert.notNull(advice, "Advice must not be null");
		this.advice = advice;
	}
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
		return mi.proceed();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用适配器模式将不同语言和画面设置适配为统一游戏界面接口的示例代码: ``` // 游戏界面接口 interface GameInterface { void moveUp(); void moveDown(); void moveLeft(); void moveRight(); } // 英文游戏界面 class EnglishGame { void up() { /* Move Up */ } void down() { /* Move Down */ } void left() { /* Move Left */ } void right() { /* Move Right */ } } // 中文游戏界面 class ChineseGame { void 向上移动() { /* Move Up */ } void 向下移动() { /* Move Down */ } void 向左移动() { /* Move Left */ } void 向右移动() { /* Move Right */ } } // 适配器类 class GameAdapter implements GameInterface { private EnglishGame englishGame; private ChineseGame chineseGame; public GameAdapter(EnglishGame englishGame, ChineseGame chineseGame) { this.englishGame = englishGame; this.chineseGame = chineseGame; } public void moveUp() { englishGame.up(); chineseGame.向上移动(); } public void moveDown() { englishGame.down(); chineseGame.向下移动(); } public void moveLeft() { englishGame.left(); chineseGame.向左移动(); } public void moveRight() { englishGame.right(); chineseGame.向右移动(); } } // 客户端代码 public class Client { public static void main(String[] args) { EnglishGame englishGame = new EnglishGame(); ChineseGame chineseGame = new ChineseGame(); GameInterface game = new GameAdapter(englishGame, chineseGame); game.moveUp(); game.moveDown(); game.moveLeft(); game.moveRight(); } } ``` 在这个示例中,我们定义了一个游戏界面接口 `GameInterface`,并实现了两个不同的游戏界面类 `EnglishGame` 和 `ChineseGame`。然后,我们创建了一个适配器类 `GameAdapter`,它将 `EnglishGame` 和 `ChineseGame` 适配为 `GameInterface` 接口的实现。最后,在客户端代码中,我们实例化了一个适配器,并使用它来移动游戏角色。这样,在不同语言和画面设置之间切换时,游戏界面的操作就能被统一处理,而不需要修改游戏逻辑代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值