Facade概述
玩具代码案例 - 订单服务门面
门面角色
IOrderService
package online.javabook.gof.structural.patterns5.facde.mall.service.api;
public interface IOrderService {
void submitOrder();
}
OrderService
package online.javabook.gof.structural.patterns5.facde.mall.service.impl;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.ICartRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IExpressRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IPayRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.CartRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.ExpressRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.PayRepository;
import online.javabook.gof.structural.patterns5.facde.mall.service.api.IOrderService;
public class OrderService implements IOrderService {
private ICartRepository cartRepository = new CartRepository();
private IPayRepository payRepository = new PayRepository();
private IExpressRepository expressRepository = new ExpressRepository();
@Override
public void submitOrder() {
cartRepository.buy();
payRepository.pay();
expressRepository.ship();
}
}
被聚合的角色
ICartRepository
package online.javabook.gof.structural.patterns5.facde.mall.repository.api;
public interface ICartRepository {
void buy();
}
package online.javabook.gof.structural.patterns5.facde.mall.repository.impl;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.ICartRepository;
public class CartRepository implements ICartRepository {
@Override
public void buy() {
System.out.println("buy.....");
}
}
IExpressRepository
package online.javabook.gof.structural.patterns5.facde.mall.repository.api;
public interface IExpressRepository {
void ship();
}
package online.javabook.gof.structural.patterns5.facde.mall.repository.impl;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IExpressRepository;
public class ExpressRepository implements IExpressRepository {
@Override
public void ship() {
System.out.println("ship.....");
}
}
IPayRepository
package online.javabook.gof.structural.patterns5.facde.mall.repository.api;
public interface IPayRepository {
void pay();
}
package online.javabook.gof.structural.patterns5.facde.mall.repository.impl;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IPayRepository;
public class PayRepository implements IPayRepository {
@Override
public void pay() {
System.out.println("pay.....");
}
}
不基于门面模式的实现
Main
package online.javabook.gof.structural.patterns5.facde.mall.app.bad;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.ICartRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IExpressRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.api.IPayRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.CartRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.ExpressRepository;
import online.javabook.gof.structural.patterns5.facde.mall.repository.impl.PayRepository;
public class Main {
public static void main(String[] args) {
ICartRepository cartRepository = new CartRepository();
IPayRepository payRepository = new PayRepository();
IExpressRepository expressRepository = new ExpressRepository();
cartRepository.buy();
payRepository.pay();
expressRepository.ship();
}
}
基于门面模式的实现
Main
package online.javabook.gof.structural.patterns5.facde.mall.app.good;
import online.javabook.gof.structural.patterns5.facde.mall.service.api.IOrderService;
import online.javabook.gof.structural.patterns5.facde.mall.service.impl.OrderService;
public class Main {
public static void main(String[] args) {
IOrderService orderService = new OrderService();
orderService.submitOrder();
}
}