设计模式之适配器模式
2017-04-18 本文已影响10人
smallstrong
适配器UML.png适配器模式
interface Rule{
void driveCar();
boolean haveHouse();
long spendMoneyForHer();
}
程序员哥哥找女朋友,女朋友对其有这三点简单要求
class MeAdapter implements Rule{
private Car car;
private House house;
private Money money;
public MeAdapter(Car car,House house,Money money){
this.car = car;
this.house = house;
this.moeny = money;
}
public void driveCar(){
car.drive();//开出我的五菱宏光
}
public boolean haveHouse(){
house.live();
return true;//亮出我的农村小土屋
}
public long spendMoneyForHer(){
money.spend();
return 10000000;//做梦都想给你花这么多钱
}
}
一个程序员哥哥想做到有房有车有钱的梦想适配器
class Car{
public void drive(){
//司机开车
}
}
实体类车, 方法开车
class House{
public void live(){
//住房子
}
}
实体类房子, 住房子
class Money{
public void spend{
//花钱
}
}
实体类钱, 花钱
class Client{
Rule rule;
rule = new MeAdapter(new Car(),new House,new Money);
rule.driveCar();
rule.haveHouse();
rule.spendMoneyForHer();
}
满足女票的几点小需求