• 桥接模式【结构模式】


    桥接模式

    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;
        }
    }
    
  • 相关阅读:
    jsp>EL表达式语言 小强斋
    jsp>JSP + Servlet + JavaBean开发模式 小强斋
    jsp>Smartupload例子代码 小强斋
    jsp>servlet 小强斋
    jsp>JSP + Servlet + JavaBean开发模式 小强斋
    五言诗
    夜深灯下行
    2011 年数据库大会的PPT下载
    去除iPhone icon的光晕效果
    望苍天
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10164320.html
Copyright © 2020-2023  润新知