Giter VIP home page Giter VIP logo

Comments (1)

dzh213 avatar dzh213 commented on June 29, 2024

定义一个使用武器的行为:

public interface WeaponBehavior {
    void useWeapon();
}

定义具体的使用武器:

public class AxeBehavior implements WeaponBehavior {
    @Override
    public void useWeapon() {
        System.out.println("使用斧头");
    }
}

public class BowAndArrowBehavior implements WeaponBehavior {
    @Override
    public void useWeapon() {
        System.out.println("使用弓箭");
    }
}

public class KnifeBehavior implements WeaponBehavior {
    @Override
    public void useWeapon() {
        System.out.println("使用宝剑");
    }
}

定义角色的抽象类,角色的公共行为是使用武器,具体使用什么样的武器可根据策略调整。

public abstract class Role {
    private WeaponBehavior weaponBehavior;

    public void setWeaponBehavior(WeaponBehavior weaponBehavior) {
        this.weaponBehavior = weaponBehavior;
    }

    public abstract void display();

    public void attack() {
        weaponBehavior.useWeapon();
    }
}

实现具体的角色:

public class King extends Role {
    @Override
    public void display() {
        System.out.println("我是国王");
    }
}
public class Knight extends Role {
    @Override
    public void display() {
        System.out.println("我是骑士");
    }
}

通过上面定义,能够使角色根据策略动态的切换武器:

public class StategyMain {
    public static void main(String[] args) {
        King king = new King();
        king.setWeaponBehavior(new AxeBehavior());
        king.attack();
        System.out.println("切换武器中......");
        king.setWeaponBehavior(new BowAndArrowBehavior());
        king.attack();
    }
}

参考:
https://www.cnblogs.com/dotgua/p/strategy-pattern.html

from disign-patterns.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.