您的位置:首页 > 其它

设计模式笔记之六 (适配器模式)

2013-05-13 15:50 274 查看
适配器模式

适配器模式就是将一个类的接口转变成另一个客户类需要的接口,使两个类都不用改动各自的逻辑。通俗点就是亡羊补牢。

我们的实验室最近异常尴尬,起因是我们实验室正在环球展出我们的产品,其中有个环节是展示产品出厂时的状态。

在展示兽人和人妖的时候没什么问题,但是展示正常男人和女人的时候,却通不过国家有关部门的检查,因为我们的产品出厂的时候都是红果果的啊。

class Ren {
}

class RenYao extends Ren {
public String head = "人妖头";
public String body = "人妖身体 - 无"; // 大家都知道什么是‘无’
public String foot = "人妖脚";

public void show() {
System.out.println(this.head + " " + this.body + " " + this.foot);
}
}
class ShouRen extends Ren {
public String head = "兽人头";
public String body = "兽人身体 - 异种"; // 只要是不同就没关系,也没见小狗因为红果果被有关部门抓
public String foot = "兽人脚";

public void show() {
System.out.println(this.head + " " + this.body + " " + this.foot);
}
}
class NanRen extends Ren {
public String head = "男人头";
public String body = "男人身体 - 男性";
public String foot = "男人脚";

public void show() {
System.out.println(this.head +  " " + this.foot); // 不能红果果的展出
}
}
class NvRen extends Ren {
public String head = "女人头";
public String body = "女人身体 - 女性";
public String foot = "女人脚";

public void show() {
System.out.println(this.head + " " + this.foot); // 不能红果果的展出
}
}

public class Adapter {
public static void main(String[] args) {
new ShouRen().show();//展示全部
new RenYao().show();//展示全部
new NanRen().show();//只展示头脚--这与我们的初衷不合
new NvRen().show();//只展示头脚--这与我们的初衷不合
}
}


这是我们遇到最大的尴尬,我们的产品已经造好了,但是我们展出的时候,发现有些展品却不能用,而且这两个因素(类)都不能轻易改变。此时我们研究所的某个太阳国文艺片爱好者说到,这个问题好解决啊,太阳国就有很好的先例啊,他们的文艺片分为外销型和内销型,当外销型的片子想转内销的话,只要上个马就行(好吧,这里给广大丑矮穷搓撸的岛国爱情动作片片友们普及下知识:岛国的内销片子是不允许裸露XXX的,只有外销欧美的片子可以,所以出口转内销的话需要一些后期的上码处理的,当然也有坊间零售,不公映的就没人管的着了)。

这下我们一下子都懂了。

class Ren {
}

class RenYao extends Ren {
public String head = "人妖头";
public String body = "人妖身体 - 无"; // 大家都知道什么是‘无’
public String foot = "人妖脚";

public void show() {
System.out.println(this.head + " " + this.body + " " + this.foot);
}
}
class ShouRen extends Ren {
public String head = "兽人头";
public String body = "兽人身体 - 异种"; // 只要是不同就没关系,也没见小狗因为红果果被有关部门抓
public String foot = "兽人脚";

public void show() {
System.out.println(this.head + " " + this.body + " " + this.foot);
}
}
class NanRen extends Ren {
public String head = "男人头";
public String body = "男人身体 - 男性";
public String foot = "男人脚";

public void show() {
System.out.println(this.head +  " " + this.foot); // 不能红果果的展出
}
}
class NvRen extends Ren {
public String head = "女人头";
public String body = "女人身体 - 女性";
public String foot = "女人脚";

public void show() {
System.out.println(this.head + " " + this.foot); // 不能红果果的展出
}
}

class NanRenAdapter {
NanRen nr;
public NanRenAdapter() {
nr = new NanRen();
}
public void show() {
System.out.println(nr.head + " " + "打码的:" + nr.body + " " + nr.foot); // 打码
}
}
class NvRenAdapter {
NvRen nr;
public NvRenAdapter() {
nr = new NvRen();
}
public void show() {
System.out.println(nr.head + " " + "打码的:" + nr.body + " " + nr.foot); // 打码
}
}

public class Adapter {
public static void main(String[] args) {
new ShouRen().show();//展示全部
new RenYao().show();//展示全部
new NanRenAdapter().show();//展示全部-打码
new NvRenAdapter().show();//展示全部-打码
}
}


适配器模式是最不推荐使用的模式,只要有可能统一接口,就应该在设计阶段统一。

适配器模式简直都不是设计模式,而是维护模式。但现今的IT工作者们做的大都数都是在前辈们的代码尸体上缝缝补补,所以轻易不要改动前辈们的代码,因为你不知道是不是还有别人在使用这个接口,所以使用适配器模式吧!

亡羊补牢总比不补好。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: