设计模式一“策略模式”

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

point1:针对接口编程,而不是针对实现编程

point2:多用组合,少用继承

point3:使用委托“delegate”

简单需求定义:这是一个RPG游戏,游戏中提供多种人物角色供你选择,每个角色都有自己特定的武器。

角色:骑士(长矛),剑客(长剑),刺客(匕首)

使用设计模式:

定义基础角色类:

 1 package com.pattern.strategypattern.character;
 2 
 3 import com.pattern.strategypattern.behavior.FightBehavior;
 4 
 5 public class Character {
 6     public Character(){}
 7     
 8     public FightBehavior fightBehavior;
 9     
10     public void setFightBehavior(FightBehavior fightBehavior) {
11         this.fightBehavior = fightBehavior;
12     }
13 
14     public void performFight(){
15         fightBehavior.useWeapon();
16         //to fight with weapon
17     }
18 }

所有的攻击行为托管(delegate)给FightBehavior,并通过performFight来执行攻击行为,提供setFightBehavior方法,供给后期如果需要增加角色的武器使用行为,可以进行更丰富的设置

定义处理攻击行为的接口

1 package com.pattern.strategypattern.behavior;
2 
3 public interface FightBehavior {
4     public void useWeapon();
5 }

针对接口进行编程的目的,很显然就是为了能够动态的决定代理的行为。详细参考java多态。

定义丰富的攻击行为,用矛,用剑,用匕首

1 package com.pattern.strategypattern.behavior;
2 
3 public class FightWithLance implements FightBehavior {
4     public void useWeapon() {
5         System.out.println("fight with my lance");
6     }
7 }
1 package com.pattern.strategypattern.behavior;
2 
3 public class FightWithSword implements FightBehavior{
4     public void useWeapon() {
5         System.out.println("Fighit with my sword");    
6     }
7 }
1 package com.pattern.strategypattern.behavior;
2 
3 public class FightWithKnife implements FightBehavior{
4     public void useWeapon() {
5         System.out.println("fight with my knife");
6     }
7 }

定义具体的角色:骑士,剑客,刺客

1 package com.pattern.strategypattern.character;
2 import com.pattern.strategypattern.behavior.FightWithLance;;
3 
4 public class Knight extends Character {
5     public Knight(){
6         this.fightBehavior = new FightWithLance();
7     }
8 }
1 package com.pattern.strategypattern.character;
2 
3 import com.pattern.strategypattern.behavior.FightWithSword;
4 
5 public class SwordsMan extends Character {
6     public SwordsMan(){
7         this.fightBehavior = new FightWithSword();
8     }
9 }
1 package com.pattern.strategypattern.character;
2 
3 import com.pattern.strategypattern.behavior.FightWithKnife;
4 
5 public class Assassin extends Character {
6     public Assassin(){
7         this.fightBehavior = new FightWithKnife();
8     }
9 }

进行测试:

 1 package com.pattern.strategypattern;
 2 
 3 import com.pattern.strategypattern.character.Assassin;
 4 import com.pattern.strategypattern.character.Character;
 5 import com.pattern.strategypattern.character.Knight;
 6 import com.pattern.strategypattern.character.SwordsMan;
 7 
 8 public class Test {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         Character knight = new Knight();
15         Character swordsMan = new SwordsMan();
16         Character assassin = new Assassin();
17         
18         knight.performFight();
19         swordsMan.performFight();
20         assassin.performFight();
21     }
22 
23 }

反思:如果我们用继承来实现各类角色会导致哪些缺点?

1.代码在多个子类中重复 

2.运行时的行为不容易改变

3.如果改动基础的Character类,会导致全部的代码都需要改动,可维护性差!

 

总结:在维护的过程中,这个游戏可能会有新的角色产生,新的角色攻击行为,或者其他的行为,我们都可以采用这样一种模式来进行维护,使用策略模式的结果就是维护的代价非常的低,并伴随着很好的扩展性。

 

 

转载于:https://www.cnblogs.com/richardhu1991/archive/2013/03/04/2942309.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值