[5] [接口隔离] ( 1 ) 门面模式 facade
总结
-
facade模式有什么用?
解耦Client和subSystem.
![image]()
-
怎么做到的"解耦Client和subSystem"?
在Client和subSystem中间加一个facade层,
facade层负责提供一个统一稳定的交互接口.
(这就做到了, 无论subSystem怎么变, facede不变, 客户端代码不变)
.
![image]()
-
facade的常见形式?
facade更注重架构层次看整个系统, 而不是单个类的层次.
facede很多时候更是一种架构设计模式.
-
非集装箱
facede简化了客户端和复杂子系统的交互方式,
但facade并非一个集装箱,任意放进任何多个对象,
facade组件的内部是"相互耦合关系比较大的一系列组件",
而不是一个简单的功能集合.
-
GOF定义
为子系统中的一组接口提供一个一致(稳定)的界面,
Facade模式定义了一个高层接口,
这个接口使得这一子系统更加容易使用(复用)。
-
场景
复杂的library
电商系统...
java例子1
重构前
package v19_facade.java;
class Projector {
public void on() {
System.out.println("Turning on the projector...");
}
public void off() {
System.out.println("Turning off the projector...");
}
}
class DVDPlayer {
public void play() {
System.out.println("Playing the movie...");
}
public void stop() {
System.out.println("Stopping the movie...");
}
}
class SoundSystem {
public void on() {
System.out.println("Turning on the sound system...");
}
public void off() {
System.out.println("Turning off the sound system...");
}
}
public class Movie1 {
public static void main(String[] args) {
Projector projector = new Projector();
DVDPlayer dvdPlayer = new DVDPlayer();
SoundSystem soundSystem = new SoundSystem();
projector.on();
dvdPlayer.play();
soundSystem.on();
}
}

重构后
package v19_facade.java;
class Projector {
public void on() {
System.out.println("Turning on the projector...");
}
public void off() {
System.out.println("Turning off the projector...");
}
}
class DVDPlayer {
public void play() {
System.out.println("Playing the movie...");
}
public void stop() {
System.out.println("Stopping the movie...");
}
}
class SoundSystem {
public void on() {
System.out.println("Turning on the sound system...");
}
public void off() {
System.out.println("Turning off the sound system...");
}
}
class HomeTheaterFacade {
private Projector projector;
private DVDPlayer dvdPlayer;
private SoundSystem soundSystem;
public HomeTheaterFacade(Projector projector, DVDPlayer dvdPlayer, SoundSystem soundSystem) {
this.projector = projector;
this.dvdPlayer = dvdPlayer;
this.soundSystem = soundSystem;
}
public void watchMovie() {
projector.on();
dvdPlayer.play();
soundSystem.on();
}
public void stopMovie() {
projector.off();
dvdPlayer.stop();
soundSystem.off();
}
}
public class Movie2 {
public static void main(String[] args) {
Projector projector = new Projector();
DVDPlayer dvdPlayer = new DVDPlayer();
SoundSystem soundSystem = new SoundSystem();
HomeTheaterFacade homeTheater = new HomeTheaterFacade(projector, dvdPlayer, soundSystem);
homeTheater.watchMovie();
}
}
java例子2
package v19_facade.java;
// 支付
class Payment {
private double amount; // 数额
public Payment(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
// 退款状态
enum RefundStatus {
SUCCESS, FAILURE
}
// 退款相关
class Billing {
public Payment getPaymentForOrder(int orderId) {
// Simple Implementation
return new Payment(50.0);
}
public RefundStatus processRefund(Payment payment) {
// Simple Implementation
if (payment.getAmount() > 0) {
return RefundStatus.SUCCESS;
} else {
return RefundStatus.FAILURE;
}
}
}
// 订单
class Shipping {
public void updateShippingAddress(int orderId, String newAddress) {
System.out.println("Shipping address for order " + orderId + " updated to: " + newAddress);
}
}
// 特殊问题
class Issue {
private String description;
public Issue(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
// 消费者服务
class CustomerService {
public void notifyCustomer(String message) {
System.out.println("Notification sent to customer: " + message);
}
public void escalateToManager(Issue issue) {
System.out.println("Issue escalated to manager: " + issue.getDescription());
}
}
// 外观类
class CustomerSupportFacade {
private Billing billing;
private Shipping shipping;
private CustomerService customerService;
public CustomerSupportFacade(Billing billing, Shipping shipping, CustomerService customerService) {
this.billing = billing;
this.shipping = shipping;
this.customerService = customerService;
}
// 处理退款请求
public void handleRefundRequest(int orderId) {
Payment payment = billing.getPaymentForOrder(orderId);
RefundStatus refundStatus = billing.processRefund(payment);
customerService.notifyCustomer("Refund status: " + refundStatus);
}
// 修改配送地址
public void changeShippingAddress(int orderId, String newAddress) {
shipping.updateShippingAddress(orderId, newAddress);
customerService.notifyCustomer("Shipping address updated");
}
// 升级至经理
public void escalateToManager(Issue issue) {
customerService.escalateToManager(issue);
}
}
public class CustomerSuportClient {
public static void main(String[] args) {
Billing billing = new Billing();
Shipping shipping = new Shipping();
CustomerService customerService = new CustomerService();
// 外观类
CustomerSupportFacade customerSupport = new CustomerSupportFacade(billing, shipping, customerService);
// 给消费者退款
customerSupport.handleRefundRequest(12345);
// 修改订单信息
customerSupport.changeShippingAddress(12345, "123 New Street, New York, NY 10001");
// 找经理处理问题
Issue issue = new Issue("Product not working properly");
customerSupport.escalateToManager(issue);
}
}


浙公网安备 33010602011771号