See Design Patterns (strategy pattern) from the king of glory
One: Introduction
Before the start of the game, players need to choose a hero, and then according to the selected squad free choice of Summoner skills, the game starts, players can control the hero for normal attacks and attacks using Summoner skills
II: Strategy Mode
Policy mode variable portion from the program abstract out into separate algorithm interface, encapsulate a series of algorithms implemented on the interface
- Among these design principles are:
- Identify the part of the application needs to be changed, to make them independent algorithm interface. (The hero Summoner skills to make the algorithm interface)
- Oriented programming interface, not the implementation-oriented. (We call upon the teacher skill in the use of strategy interfaces, it does not know what kind of future will Summoner skills)
- Multi-purpose combination, less inheritance (a complete hero, is a combination of a specific interface objects, or a combination of small objects of various interfaces became a hero Object)
- The advantages of the strategy pattern
- Combination, not just inherited, making the architecture more flexible.
- Elastic, can better respond to future changes. (Open - closed principle)
- Better code reusability. (In relation to inheritance)
- Disadvantage of the strategy pattern
- Increase the number of objects (each interface are made of an object, an increase in the number of objects)
Three: 类图
Four: code implementation
Interface ISkill
package com.game.Skill;
/*
* 英雄的召唤师技能接口
*/
public interface ISkill {
/*
* 使用召唤师技能的方法
*/
public abstract void useSkill();
}
Class JiPa0 achieve ISkill Interface
package com.game.Skill.impl;
import com.game.Skill.ISkill;
/*
* 召唤师技能(疾跑)
*/
public class JiPao implements ISkill {
public void useSkill() {
System.out.println("疾跑,75秒CD,增加30%移动速度持续10秒");
}
}
Class KuangBao achieve ISkill Interface
package com.game.Skill.impl;
import com.game.Skill.ISkill;
/*
* 召唤师的技能(狂暴)
*/
public class KuangBao implements ISkill {
@Override
public void useSkill() {
System.out.println("狂暴:60秒CD,增加攻击速度60%持续5秒");
}
}
Class Hero (reflected face Interface thinking skills required to pass an interface object)
package com.game.domain;
import com.game.Skill.ISkill;
/*
* 英雄类(抽象类)
*/
public abstract class Hero {
//英雄的名称
private String heroName;
//英雄的召唤师技能的接口(组合关系,需要使用时传入一个具体的技能对象)
private ISkill iskill;
//英雄的自我介绍的方法
public abstract void display();
//加入一个新的成员(英雄单次攻击的伤害值)
private int heroHurt;
//英雄的普通攻击的方法
public abstract void normalAttack();
//英雄的召唤师技能的使用方法
public void skill() {
iskill.useSkill();
}
//set/get
public String getHeroName() {
return heroName;
}
public void setHeroName(String heroName) {
this.heroName = heroName;
}
public void setIskill(ISkill iskill) {
this.iskill = iskill;
}
public int getHeroHurt() {
return heroHurt;
}
public void setHeroHurt(int heroHurt) {
this.heroHurt = heroHurt;
}
}
Hero class inherits class HouYi
package com.game.domain;
/*
* 英雄后羿类
*/
public class HouYi extends Hero {
//编写构造方法
public HouYi() {
super.setHeroName("后羿");
}
//后羿英雄的自我介绍
public void display() {
System.out.println("觉醒吧,猎杀时刻!");
}
//后羿英雄的普通攻击
public void normalAttack() {
System.out.println("xiuxiu~ 被动:迟缓之箭");
}
}
Test class HeroTest
package com.game.test;
import com.game.Skill.impl.JiPao;
import com.game.Skill.impl.KuangBao;
import com.game.domain.Hero;
import com.game.domain.HouYi;
/*
* 英雄与技能的测试类
*/
public class HeroTest {
public static void main(String[] args) {
//1.选择英雄
Hero hero = new HouYi();
//2.英雄自我介绍
System.out.println("英雄:"+hero.getHeroName()+",自我介绍:");
hero.display();
//3.玩家根据组队情况,设置英雄的召唤师技能
hero.setIskill(new JiPao());//面向接口编程的体现
hero.setIskill(new KuangBao());
//4.游戏开始
System.out.println("游戏开始,请做好准备!");
//5.使用英雄的普通攻击
hero.normalAttack();
//6.使用英雄的召唤师技能攻击
hero.skill();
}
}