cool hit counter Design Patterns from King's Quest - Strategic Patterns (java version)_Intefrankly

Design Patterns from King's Quest - Strategic Patterns (java version)


King's Glory

Once I was a college student who played pesticides, but have now given up, don't ask me why (cried from my pitiful teammates. ). And today we're going to turn pesticides into glory, and we're going to look at design patterns from the King's Quest for strategy patterns.

shooter

00.analysis

Let's start by thinking about the fact that King's Quest has now grown to over 60 heroes, and they are roughly divided into six roles: tank, fighter, assassin, archer, mage, and support. Since I like to play shooters, here's an illustration of a shooter. Shooters share the same trait of shooting, but each shooter has a different shooting status, can wear different summoner skills, etc.

Let's find it. shooter The same thing about these types of heroes, Same thing. Both shooting attacks., The difference is that the way you shoot is different, Houyi, mythological Chinese archer whose wife was Chang'e Take a bow and arrow., Lupin is machine gunning., Di Renjie (607-700), Tang dynasty politician, prime minister under Wu Beiji, subsequently hero of legends It's a token., There are also skills that can be worn differently by each summoner。

Identify these differences and how can they be addressed using object orientation? The three main properties of object-oriented: encapsulation, inheritance, and polymorphism. It's easy to think that we could design an abstract class of an archer with the various methods that an archer should have, such as normal attacks, summoner skills, skill attacks, etc., and then inherit such methods in its child classes, and then use polymorphism to be able to call the child class methods with the parent class. This seems to be feasible, but we know that King's Glory is always updating, there will be new heroes, new equipment, new skin, new skills, each of their new appearances, we have to override the methods in the parent class once, only with inheritance to do, not flexible, and poor support for future updates to King's Glory.

Let's narrow our thinking a bit more, just take Summoner Skills Let's talk.。 Each hero is allowed to wear the same Summoner Skills of, however Summoner Skills There are also a variety of, Here you can't use inheritance to implement heroes who have Summoner Skills I'll take it.。 Because heroes and Summoner Skills Between isHas-a, rather thanIs-a commercial sector, You've got the wit to thinkjava Interface ininterface, sure!, We can define various summoner interfaces to give the hero different Summoner Skills, But let's think about it again., We are allowed to choose before the matchup Summoner Skills of, Instead of a fixed per-hero, If you only use the interface, Summoner skills are fixed when the hero object is generated, Or rather, Each hero is there with Summoner Skills competencies, But we're not sure exactly what the skill is。 You might also be thinking., We can add some judgments to give the hero different Summoner Skills, But if so, Each of our heroes will in turn have a variety of different heroes( everyone Summoner Skills The interfaces are different), But it's certainly not a good idea to think about this approach, The workload is tooTM grown up, Surely a smart programmer wouldn't do this。

Summoner Skills

We actually want to have a way to dynamically load Summoner Skills Methods of the interface, Let's organize our thoughts, The summoner's skills are changing, But every hero can wear Summoner Skills, thus having Summoner Skills conduct, We can put Summoner Skills abstracted as shooter A data member of the abstract class, Then each Summoner Skills And then the specific implementation of this interface, This makes it easy for the hero to have a variety of Summoner Skills The conduct of the, in case King's Glory Update the new Summoner Skills, We can still implement this skill from this summoner interface, We call this interface Strategy Interface, It is also the embodiment of the strategy pattern in design patterns, This interface will not change, But the interface specific implementation class( various kinds Summoner Skills) It's changeable., This is what we usually call the rule of no change in response to change。 thus King's Glory The life of this game is extended, King's Glory feasible(keng) play with(ren) Sex has also increased significantly, Your father won't have to worry about your studies anymore(⊙﹏⊙)。

01.Code Implementation

Here's what I usedJava realized King's Glory The code for the strategy pattern of( environments:eclipse)

  • Directory structure Bad English, hero names and summoner skills are in pinyin.

image.png

Here is the UML class diagram

UML diagram

  • Hero Pack abstract category shooter kind, The subclass only writes Houyi, mythological Chinese archer whose wife was Chang'e, Lu Ban 7, a legendary figure of Chinese carpentry, Di Renjie (607-700), Tang dynasty politician, prime minister under Wu Beiji, subsequently hero of legends, You can also add other shooter
  1. Shooter Class Code
package character;

import strategy.SkillStrategy;

/**
 *  shooter kind( abstract category)
  * @author An ape who writes programs
 *
 */
public abstract class Shooter {
    
     //Hero Name
    private  String heroName;
    public void setHeroName(String heroName) {
        this.heroName = heroName;
    }
    public String getHeroName() {
        return heroName;
    }

    // shows shooter function, Overriding by subclasses to achieve a concrete display
    public abstract void display();
    
    // Normal attack function, everyone shooter You can shoot them all.
    public abstract void normalAttack();
    
     //Skills Strategy Member
    private SkillStrategy skillStrategy;
    public void setSkillStrategy(SkillStrategy skillStrategy) {
        this.skillStrategy = skillStrategy;
    }

    // Summoner Skills function
    public void skill()
    {
        skillStrategy.useSkill();
    }
}

2. Houyi, mythological Chinese archer whose wife was Chang'e Subclass Code

package character;

/**
 *  heroes: Houyi, mythological Chinese archer whose wife was Chang'e( shooter subclasses of)
  * @author An ape who writes programs
 *
 */

public class HouYi extends Shooter {
    
    public HouYi() {
        super.setHeroName(" Houyi, mythological Chinese archer whose wife was Chang'e");
    }
    
    @Override
    public void display() {
        System.out.println(" Awaken., The Hour of the Hunt!");
    }

    @Override
    public void normalAttack() {
        System.out.println("xiuxiuxiu~, passively: Passive Retarded Arrow");
    }

}

3.Lupin 7 subclass code

package character;

/**
  * Hero: Lupin VII (subclass of Archer)
  * @author An ape who writes programs
 *
 */

public class LuBanQiHao extends Shooter{
    
    public LuBanQiHao() {
        super.setHeroName(" Lu Ban 7, a legendary figure of Chinese carpentry");
    }
    
    @Override
    public void display() {
        System.out.println(" Lu Ban 7, a legendary figure of Chinese carpentry, IQ two hundred and fifty., Lu Ban, legendary master craftsman, called the father of Chinese carpentry, Lu Ban, legendary master craftsman, called the father of Chinese carpentry, Top Ruben!");
    }

    @Override
    public void normalAttack() {
        System.out.println("dadada~, passively: fire suppression");
    }
    
}

4.Dee subclass code

package character;

/**
  * Hero: Dee (subclass of Archer)
  * @author An ape who writes programs
 *
 */

public class DiRenJie extends Shooter {

    public DiRenJie() {
        super.setHeroName(" Di Renjie (607-700), Tang dynasty politician, prime minister under Wu Beiji, subsequently hero of legends");
    }
    
    @Override
    public void display() {
        System.out.println(" On behalf of the law.!");
    }

    @Override
    public void normalAttack() {
        System.out.println("shuashuashua~, passively: fast and nimble");
    }

}
  • Strategy Interface

1. Summoner Skills Strategy Interface

package strategy;
/**
  * Strategic interface, using the skills of the shooter
  * @author An ape who writes programs
 *
 */
public interface SkillStrategy {
    void useSkill();
}
  • Summoner Skill Pack

1. discipline Summoner Skills

package skill;

import strategy.SkillStrategy;
/**
* Skill Strategy - Discipline Skill
  * @author An ape who writes programs
 *
 */

public class ChengJie implements SkillStrategy{

    @Override
    public void useSkill() {
        System.out.println(" discipline:30 secondCD, Deals true damage and stuns wild monsters and minions around you1 second");
    }
}

2.Sprint Summoner Skills

package skill;

import strategy.SkillStrategy;
/**
  * Skill Strategy - Sprint Skill
  * @author An ape who writes programs
 *
 */

public class JiPao implements SkillStrategy {

    @Override
    public void useSkill() {
        System.out.println(" sprint:60 secondCD, Increased attack speed60%, And increase physical attack power10% sustainable5 second");
    }

}

3. berserk Summoner Skills

package skill;

import strategy.SkillStrategy;
/**
  * Skill Strategy - Berserk Skills
  * @author An ape who writes programs
 *
 */

public class KuangBao implements SkillStrategy {

    @Override
    public void useSkill() {
        System.out.println(" berserk:60 secondCD, Increased attack speed60%, And increase physical attack power10% sustainable5 second");
    }

}
  • Shooter test category You can think (yi) like the client of King's Glory, which is the phone you play
package character;

import skill.ChengJie;
import skill.JiPao;
import skill.KuangBao;
import skill.ZhanSha;

public class ShooterTest {
    public static void main(String[] args) {
        System.out.println("( Music is playing.。。。) Welcome to King's Pesticide, The enemy also has30 Seconds to battlefield, Please be prepared., all out (military)!");
        Shooter shooter = null;
         // Use the parent class object to point to a different subclass object to switch heroes
        shooter = new HouYi();
        //shooter = new LuBanQiHao();
        //shooter = new DiRenJie();
        System.out.print(" heroes"+shooter.getHeroName()+" go on stage, opening remarks:");
        shooter.display();
        System.out.print(" normal attack:");
        shooter.normalAttack();
         //Dynamic loading of summoner skills
        shooter.setSkillStrategy(new JiPao());
        //shooter.setSkillStrategy(new KuangBao());
        //shooter.setSkillStrategy(new ZhanSha());
        //shooter.setSkillStrategy(new ChengJie());
        System.out.print(" Summoner Skills:");
        shooter.skill();
    }
}

Screenshot of execution results

Houyi, mythological Chinese archer whose wife was Chang'e

Di Renjie (607-700), Tang dynasty politician, prime minister under Wu Beiji, subsequently hero of legends

Lu Ban 7, a legendary figure of Chinese carpentry

02.conclude

The official definition of the strategy pattern is given below.

The strategy pattern separates the mutable part from the program into an algorithmic interface, under which a series of algorithmic implementations are encapsulated separately.

Among the design principles of this are. 1. Identify the parts of the application that need to be changed, Separate them out into algorithmic interfaces。( We are taking each hero's Summoner Skills Made into an algorithmic interface, Bureau props are also available for this) 2. interface-oriented programming, Instead of being implementation-oriented。( We are using the summoner Strategy Interface time, Not knowing what else it would later Summoner Skills) 3.Use more combinations and less inheritance. (A complete hero that is combined with specific interface objects, or various interfacelet objects that combine into a hero object)

Advantages of the strategy model

  • The use of combinations, rather than inheritance alone, makes the architecture more flexible.
  • Resilient and better able to cope with future changes. (Open-close principle)
  • Better code reusability. (as opposed to succession)
  • Eliminates a large number of conditional statements.

Disadvantages of the strategy model

  • The client-side code needs to know what those are for each strategy algorithm (players need to know what kind of summoner skills are available, and when the server-side code is updated, the client-side needs to add the corresponding updates)
  • Increased the number of objects (each interface has been made into an object, has the number of objects increased)

All of the above analysis, of course, is a personal reflection and does not mean that this is how the King's Quest game is made haha.


Recommended>>
1、Butterfly Cloud Advertising System
2、Mingliu Wu Mingliu Data HAO Intelligence opens a new chapter of artificial intelligence against the trend
3、Analytical methods and models for realworld data
4、Python Algorithm Problem Dichotomous Find Testfan Punch Learn Test Open 1115
5、IPFS development application principle II file upload mechanism based on ipfs itself http protocol

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号