大美工设计网站官网,山东机关建设网站,影视广告制作公司,外贸网站如何传产品外观模式#xff1a;能为系统框架或其他复杂业务流程封装提供一个简单的接口。 例如抽奖过程中 设计模式#xff0c;一定要敲代码理解 调用1#xff08;抽奖系统#xff09;
/*** author ggbond* date 2024年04月08日 10:34*/
public class Lottery {public String getId…外观模式能为系统框架或其他复杂业务流程封装提供一个简单的接口。 例如抽奖过程中 设计模式一定要敲代码理解 调用1抽奖系统
/*** author ggbond* date 2024年04月08日 10:34*/
public class Lottery {public String getId(){System.out.println(获取商品id);return 00001;}public boolean stockControl(String id){System.out.println(库存减一);return true;}
}调用2快递系统
/*** author ggbond* date 2024年04月08日 10:38*/
public class Delivery {public String createSaleId(String id){System.out.println(商品打包生产快递订单号);return 00001;}public boolean send(String id){System.out.println(发货);return true;}
}门面
/*** author ggbond* date 2024年04月08日 10:43*/
public class Facade {private Delivery delivery;private Lottery lottery;public Facade( Lottery lottery,Delivery delivery) {this.delivery delivery;this.lottery lottery;}public void toLottery(){String id lottery.getId();//抽奖boolean f lottery.stockControl(id); //库存管理if(truef) {delivery.createSaleId(id);//生成订单信息delivery.send(id); //发货}}
}客户端调用
/*** author ggbond* date 2024年04月08日 10:40*/
public class Main {public static void main(String[] args) {//客户端进行抽奖普通写法Lottery lotterynew Lottery();Delivery deliverynew Delivery();String idlottery.getId();boolean stockControl lottery.stockControl(id);if (stockControl) {delivery.createSaleId(id);//生成订单信息delivery.send(id); //发货}//门面模式 抽奖封装,客户端执行封装好的一个服务方法。Facade facadenew Facade(lottery,delivery);facade.toLottery();}
}结果
获取商品id
库存减一
商品打包生产快递订单号
发货总结
门面模式目的在于封装个子系统代码流程使其独立于复杂子系统。
代码下载
代码下载