当前位置:首页 > 设计模式

适配器模式

canca19年前 (2007-03-26)设计模式487

//适配器模式
//CopyRight(C)CAnca Software Office.
//Created by CAnca.

public class AdapterMode{
 public static void main(String[] args){
  AdapterFactory adapter1 = new AdapterFactory(new Adaptee1());
  adapter1.printName("Adaptee1");
  AdapterFactory adapter2 = new AdapterFactory(new Adaptee2());
  adapter1.printName("Adaptee2");
 }
}

class Adaptee1{
 public void printName(String str){
  System.out.println("This is the first Adaptee.");
  System.out.println(str);
 }
}

class Adaptee2{
 public void printAdapterName(Object str){
  System.out.println("This is the second Adaptee.");
  System.out.println(str);
 }
}

class AdapterFactory{
 private Adaptee1 adaptee1 = null;
 private Adaptee2 adaptee2 = null;
 public AdapterFactory(Adaptee1 adaptee){
  this.adaptee1 = adaptee;
 }
 
 public AdapterFactory(Adaptee2 adaptee){
  this.adaptee2 = adaptee;
 }
 
 public void printName(String str){
  if(adaptee1 != null)adaptee1.printName(str);
  if(adaptee2 != null)adaptee2.printAdapterName(str);
 }
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/662.html

标签: 设计模式
分享给朋友:

“适配器模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  …

单例模式

//CopyRight CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class singletonMode{ public static void main(String[] args)…

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class Real_World_Example{ public static void…

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class BuilderMode{ public static void main(String[] arg…

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void…

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。