美文网首页
策略模式

策略模式

作者: 起嚸_ | 来源:发表于2018-03-07 11:57 被阅读0次
    //技能接口
    public interface IAttackBehavior  {  
        void attack();  
    }  
    
    public interface IDefendBehavior  {  
        void defend();  
    }  
    
    public interface IDisplayBehavior  {  
        void display();  
    } 
    
    //技能实现类 如果技能一样可以抽出来,不然可以用匿名
    public class AttackJY implements IAttackBehavior  {  
      
        @Override  
        public void attack()  
        {  
            System.out.println("九阳神功!");  
        }  
      
    }  
    
    public class DefendTBS implements IDefendBehavior  {  
      
        @Override  
        public void defend()  
        {  
            System.out.println("铁布衫");  
        }  
      
    } 
    
    public class RunJCTQ implements IRunBehavior  {  
      
        @Override  
        public void run()  
        {  
            System.out.println("金蝉脱壳");  
        }  
      
    }  
    
    //抽象角色类
    public abstract class Role  {  
        protected String name;  
      
        protected IDefendBehavior defendBehavior;  
        protected IDisplayBehavior displayBehavior;  
        protected IRunBehavior runBehavior;  
        protected IAttackBehavior attackBehavior;  
      
        public Role setDefendBehavior(IDefendBehavior defendBehavior){  
            this.defendBehavior = defendBehavior;  
            return this;  
        }  
      
        public Role setDisplayBehavior(IDisplayBehavior displayBehavior){  
            this.displayBehavior = displayBehavior;  
            return this;  
        }  
      
        public Role setRunBehavior(IRunBehavior runBehavior){  
            this.runBehavior = runBehavior;  
            return this;  
        }  
      
        public Role setAttackBehavior(IAttackBehavior attackBehavior){  
            this.attackBehavior = attackBehavior;  
            return this;  
        }  
      
        protected void display()     {  
            displayBehavior.display();  
        }  
      
        protected void run()      {  
            runBehavior.run();  
        }  
      
        protected void attack()      {  
            attackBehavior.attack();  
        }  
      
        protected void defend()      {  
            defendBehavior.defend();  
        }  
      
    }  
    
    //角色实现
    
    public class RoleA extends Role  {  
        public RoleA(String name)  
        {  
            this.name = name;  
        }  
      
    } 
    
    public class Test  {  
        public static void main(String[] args)  
        {  
      
            Role roleA = new RoleA("A");  
      
            roleA.setAttackBehavior(new AttackXL())//  
                    .setDefendBehavior(new DefendTBS())//  
                    .setDisplayBehavior(new DisplayA())//  
                    .setRunBehavior(new RunJCTQ());  
            System.out.println(roleA.name + ":");  
            roleA.run();  
            roleA.attack();  
            roleA.defend();  
            roleA.display();  
        }  
    }  
    
    image

    相关文章

      网友评论

          本文标题:策略模式

          本文链接:https://www.haomeiwen.com/subject/ghljfftx.html