结构型设计模式---桥接模式和外观模式

一、桥接模式

       桥接模式将抽象部分与它的具体实现部分分离,使它们都可以独立地变化。它可以将类中存在的独立变化的维度都分离出来,让它们独立变化,减少它们之间的耦合,但是需要我们正确识别出系统中那些独立变化的维度,而且这些维度之间是通过组合的方式建立联系的。符合开闭原则


这个例子中,银行类依赖账户类,也就是说抽象部分是银行,具体实现是账户。

       账户接口:

	public interface Account {
	    Account openAccount();
	
	    void showAccountType();
	}

       活期账户类:

	public class SavingAccount implements Account {
	    @Override
	    public Account openAccount() {
	        System.out.println("打开活期账号");
	        return null;
	    }
	
	    @Override
	    public void showAccountType() {
	        System.out.println("这是一个活期账号");
	    }
	}

       定期账户类:

	public class DepositAccount implements Account {
	    @Override
	    public Account openAccount() {
	        System.out.println("打开定期账号");
	        return new DepositAccount();
	    }
	
	    @Override
	    public void showAccountType() {
	        System.out.println("这是一个定期账号");
	    }
	}

       抽象银行类:

	public abstract class Bank {
	    protected Account account;
	
	    public Bank(Account account) {
	        this.account = account;
	    }
	
	    abstract Account openAccount();
	}

       中国农业银行类:

	public class ABCBank extends Bank {
	    public ABCBank(Account account) {
	        super(account);
	    }
	
	    @Override
	    Account openAccount() {
	        System.out.println("打开中国农业银行账号");
	        account.openAccount();
	        return account;
	    }
	}

       中国工商银行类:

	public class ICBCBank extends Bank {
	    public ICBCBank(Account account) {
	        super(account);
	    }
	
	    @Override
	    Account openAccount() {
	        System.out.println("打开中国工商银行账号");
	        account.openAccount();
	        return account;
	    }
	}

       测试代码:

 	Bank icbcBank = new ICBCBank(new DepositAccount());
    Account icbcAccount = icbcBank.openAccount();
    icbcAccount.showAccountType();

    Bank abcBank1 = new ICBCBank(new DepositAccount());
    Account abcAccount1 = abcBank1.openAccount();
    abcAccount1.showAccountType();

    Bank abcBank2 = new ICBCBank(new SavingAccount());
    Account abcAccount2 = abcBank2.openAccount();
    abcAccount2.showAccountType();

       测试结果:
Alt


       UML图如下:

Alt




二、外观模式

       外观模式又叫门面模式,隐藏了系统的复杂性,并向客户端提供了一个统一的接口,符合迪米特法则即最少知道原则。不符合开闭原则,增加新的服务需要修改外观类。

       积分礼物类:

	public class PointsGift {
	    private String name;
	
	    public String getName() {
	        return name;
	    }
	
	    public void setName(String name) {
	        this.name = name;
	    }
	
	    public PointsGift(String name) {
	        this.name = name;
	    }
	}

       校验库存服务:

	public class QualifyService {
	    public boolean isAvailable(PointsGift pointsGift) {
	        //校验库存
	        System.out.println("校验 " + pointsGift.getName() + ", 积分资格通过");
	        return true;
	    }
	}

       抵扣积分服务:

	public class PointsPaymentService {
	    public boolean pay(PointsGift pointsGift) {
	        //扣积分
	        System.out.println("支付" + pointsGift.getName() + "积分成功");
	        return true;
	    }
	}

       地址服务:

	public class ShippingService {
	    public String shipGift(PointsGift pointsGift) {
	        //物流系统的对接逻辑
	        System.out.println(pointsGift.getName() + "进入物流系统");
	        String shippingOrderNo = "123456";
	        return shippingOrderNo;
	    }
	}

       封装的外观服务类

	public class GiftExchangeService {
	    private QualifyService qualifyService = new QualifyService();
	    private PointsPaymentService pointsPaymentService = new PointsPaymentService();
	    private ShippingService shippingService = new ShippingService();
	
	    public void giftExchange(PointsGift pointsGift) {
	        if (qualifyService.isAvailable(pointsGift)) {
	            if (pointsPaymentService.pay(pointsGift)) {
	                String shippingOrderNo = shippingService.shipGift(pointsGift);
	                System.out.println("生成的订单号为" + shippingOrderNo);
	            }
	        }
	    }
	}

       测试代码:

	GiftExchangeService giftExchangeService = new GiftExchangeService();
    giftExchangeService.giftExchange(new PointsGift("蓝牙耳机"));

       测试结果:
Alt


       UML图如下(客户端调用的只是外观类,和隐藏在外观类中的任何服务都没直接关系):

Alt

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值