zoukankan      html  css  js  c++  java
  • 初识设计模式(策略模式)

      前言:学会总结,学会记录,与大家分享,也希望大家可以指正错误的地方。  

      为什么要学设计模式?因为在工作中,感到力不从心了,想重构却无从下手,所以借此让设计模式进入到我的大脑中。

     策略模式(Strategy)

      定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

      使用的设计原则:多用组合,少用继承。

      我认为策略模式就是让变化的地方独立起来,不是采用抽象类的继承,而是使用接口编程,从而达到可维护、可扩展的目的,并在程序运行时也可以动态改变其行为。但是使用这些策略类的客户,必须理解这些策略类并自己决定使用哪一个策略类。简单来说就是策略模式封装了变化(算法)。

      代码实现

      举个例子:多个游戏角色,可以使用不同的武器,每个角色一次只能使用一种武器,但是可以在游戏过程中换武器。

     1 /**
     2  * 策略模式实现
     3  * Created by yule on 2018/6/23 12:29.
     4  */
     5 public class Demo1 {
     6     public static void main(String[] args){
     7         Character character = new King(new KnifeBehavior());
     8         character.fight();
     9 
    10         character = new Queen(new SwordBehavior());
    11         character.fight();
    12 
    13         character = new Knight(new AxeBehavior());
    14         character.fight();
    15 
    16         character = new Troll(new BowAndArrowBehavior());
    17         character.fight();
    18     }
    19 }
    20 
    21 /**
    22  * 抽象算法类 Strategy
    23  */
    24 abstract class WeaponBehavior{
    25     public abstract void useWeapon();
    26 }
    27 
    28 class KnifeBehavior extends WeaponBehavior{
    29     @Override
    30     public void useWeapon() {
    31         System.out.println("用匕首");
    32     }
    33 }
    34 
    35 class SwordBehavior extends WeaponBehavior{
    36     @Override
    37     public void useWeapon() {
    38         System.out.println("用宝剑");
    39     }
    40 }
    41 
    42 class AxeBehavior extends WeaponBehavior{
    43     @Override
    44     public void useWeapon() {
    45         System.out.println("用斧头");
    46     }
    47 }
    48 
    49 class BowAndArrowBehavior extends WeaponBehavior{
    50     @Override
    51     public void useWeapon() {
    52         System.out.println("用弓箭");
    53     }
    54 }
    55 
    56 /**
    57  * Context
    58  * 角色
    59  */
    60 class Character{
    61    WeaponBehavior weaponBehavior;
    62 
    63    public void setWeaponBehavior(WeaponBehavior weaponBehavior){
    64        this.weaponBehavior = weaponBehavior;
    65    }
    66 
    67    public void fight(){
    68        this.weaponBehavior.useWeapon();
    69    }
    70 }
    71 
    72 class King extends Character{
    73     public King(WeaponBehavior weaponBehavior){
    74         super();
    75         super.setWeaponBehavior(weaponBehavior);
    76     }
    77 }
    78 
    79 class Queen extends Character{
    80     public Queen(WeaponBehavior weaponBehavior){
    81         super();
    82         super.setWeaponBehavior(weaponBehavior);
    83     }
    84 }
    85 
    86 class Knight extends Character{
    87     public Knight(WeaponBehavior weaponBehavior){
    88         super();
    89         super.setWeaponBehavior(weaponBehavior);
    90     }
    91 }
    92 
    93 class Troll extends Character{
    94     public Troll(WeaponBehavior weaponBehavior){
    95         super();
    96         super.setWeaponBehavior(weaponBehavior);
    97     }
    98 }

    输出结果:

     

      参考书籍:《Head First 设计模式》《大话设计模式》

  • 相关阅读:
    poj 3636
    poj 1065 Wooden Sticks
    2017北京国庆刷题Day6 afternoon
    2017北京国庆刷题Day5 afternoon
    2017北京国庆刷题Day3 afternoon
    2017北京国庆刷题Day3 morning
    2017北京国庆刷题Day2 afternoon
    2017北京国庆刷题Day2 morning
    poj2420 A Star not a Tree?
    NOIP2016 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/yuxiaole/p/9216661.html
Copyright © 2011-2022 走看看