• 桥接模式【结构模式】


    桥接模式

    Decouple an abstraction from its implementation so that the two can very independently.
    将抽象和实现解耦,这样两者就可以独立发展。
    
    public class GitBridge {
    
        @Test
        public void all() {
            /**
             * Weapon 和 Enchantment 可以任意组合,独立发展。
             */
            final FlyingEnchantment flyingEnchantment = FlyingEnchantment.builder().build();
            final SoulEatingEnchantment soulEatingEnchantment = SoulEatingEnchantment.builder().build();
    
            final Sword sword = Sword.builder().enchantment(flyingEnchantment).build();
            sword.wield();
            sword.swing();
            sword.unwield();
    
            final Hammer hammer = Hammer.builder().enchantment(soulEatingEnchantment).build();
            hammer.wield();
            hammer.swing();
            hammer.unwield();
        }
    }
    
    interface Weapon {
        void wield();
    
        void swing();
    
        void unwield();
    
        Enchantment getEnchantment();
    }
    
    interface Enchantment {
        void onActivate();
    
        void apply();
    
        void onDeactivate();
    }
    
    @Builder
    @Slf4j
    class FlyingEnchantment implements Enchantment {
    
        @Override
        public void onActivate() {
            log.info("The item begins to glow faintly.");
        }
    
        @Override
        public void apply() {
            log.info("The item flies and strikes the enemies finally returning to owner's hand.");
        }
    
        @Override
        public void onDeactivate() {
            log.info("The item's glow fades.");
        }
    }
    
    @Slf4j
    @Builder
    class SoulEatingEnchantment implements Enchantment {
    
        @Override
        public void onActivate() {
            log.info("The item spreads bloodlust.");
        }
    
        @Override
        public void apply() {
            log.info("The item eats the soul of enemies.");
        }
    
        @Override
        public void onDeactivate() {
            log.info("Bloodlust slowly disappears.");
        }
    }
    
    @Slf4j
    @Builder
    class Sword implements Weapon {
        private final Enchantment enchantment;
    
        @Override
        public void wield() {
            log.info("The sword is wielded.");
            enchantment.onActivate();
        }
    
        @Override
        public void swing() {
            log.info("The sword is swinged.");
            enchantment.apply();
        }
    
        @Override
        public void unwield() {
            log.info("The sword is unwielded.");
            enchantment.onDeactivate();
        }
    
        @Override
        public Enchantment getEnchantment() {
            return enchantment;
        }
    }
    
    @Slf4j
    @Builder
    class Hammer implements Weapon {
        private final Enchantment enchantment;
    
        @Override
        public void wield() {
            log.info("The hammer is wielded.");
            enchantment.onActivate();
        }
    
        @Override
        public void swing() {
            log.info("The hammer is swinged.");
            enchantment.apply();
        }
    
        @Override
        public void unwield() {
            log.info("The hammer is unwielded.");
            enchantment.onDeactivate();
        }
    
        @Override
        public Enchantment getEnchantment() {
            return enchantment;
        }
    }
    
  • 相关阅读:
    Linux 小知识点
    Nginx 源码安装
    MySQL user表详解
    Python 资源
    Python 迭代dict的value
    著作权和专利权的区别
    软件设计师05-信息安全基础知识
    记录一次服务器突然宕机的排查
    支付宝微信拉取账单到本地
    软件设计师04-计算机网络
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10164320.html
Copyright © 2020-2023  润新知