java设计模式|策略模式

该博客讨论了两种编程模式:ZombieTest中的继承模式和StrategyTest中的策略模式。ZombieTest展示了如何通过继承抽象类实现不同类型的僵尸,而StrategyTest则演示了如何使用策略模式动态改变对象的行为,如改变僵尸的攻击方式。这两种模式在实际开发中都有其应用场景,前者便于创建和扩展僵尸类型,后者有利于代码的灵活性和可维护性。
摘要由CSDN通过智能技术生成

这种模式比较啰嗦,不方便维护

package strategy;

public class ZoombieTest {
    public static void main(String[] args) {
        AbstractZombie normalZombie = new NormalZombie();
        AbstractZombie flagZombie = new FlagZombie();

        normalZombie.display();
        normalZombie.move();
        normalZombie.attack();

        System.out.println("--------------");

        flagZombie.display();
        flagZombie.move();
        flagZombie.attack();
    }
}

abstract class AbstractZombie{
    public abstract void display();
    public void attack(){
        System.out.println("咬.");
    }

    public void move(){
        System.out.println("一步一步移动.");
    }
}

class NormalZombie extends AbstractZombie{

    @Override
    public void display() {
        System.out.println("我是普通僵尸.");
    }
}

class FlagZombie extends AbstractZombie{
    @Override
    public void display() {
        System.out.println("我是旗手僵尸.");
    }
}

class BigHeadZombie extends AbstractZombie{
    @Override
    public void display() {
        System.out.println("大头.");
    }

    @Override
    public void attack() {
        System.out.println("头撞");
    }
}

class XxxZombie extends BigHeadZombie{
    @Override
    public void move() {
        System.out.println("xxx移动行为");
    }
}
package strategy;

public class StrategyTest {
    public static void main(String[] args) {
        Zombie zombie = new NormalZomble();
        zombie.display();
        zombie.attack();
        zombie.move();
        //下面两句 改变僵尸的攻击方式
        zombie.setAttackable(new HitAttack());
        zombie.attack();
    }
}


interface Moveable{
    void move();
}

interface Attackable{
    void attack();
}

abstract class Zombie{

    abstract public void display();
    Moveable  moveable;
    Attackable attackable;
    abstract void move();
    abstract void attack();

    public Zombie(Moveable moveable, Attackable attackable) {
        this.moveable = moveable;
        this.attackable = attackable;
    }

    public Moveable getMoveable() {
        return moveable;
    }

    public void setMoveable(Moveable moveable) {
        this.moveable = moveable;
    }

    public Attackable getAttackable() {
        return attackable;
    }

    public void setAttackable(Attackable attackable) {
        this.attackable = attackable;
    }
}

class BiteAttack implements Attackable{

    @Override
    public void attack() {
        System.out.println("咬.");
    }
}

class HitAttack implements Attackable{

    @Override
    public void attack() {
        System.out.println("打.");
    }
}

class StepByStepMove implements Moveable{

    @Override
    public void move() {
        System.out.println("一步一步移动.");
    }
}

class FlogZombie extends Zombie{

    public FlogZombie(){
        super(new StepByStepMove(),new BiteAttack());
    }
    public FlogZombie(Moveable moveable, Attackable attackable) {
        super(moveable, attackable);
    }

    @Override
    public void display() {
        System.out.println("我是旗手僵尸");
    }

    @Override
    void move() {
        moveable.move();
    }

    @Override
    void attack() {
        attackable.attack();
    }
}

class NormalZomble extends Zombie{

    public NormalZomble(){
       super(new StepByStepMove(),new BiteAttack());
    }

    public NormalZomble(Moveable moveable, Attackable attackable) {
        super(moveable, attackable);
    }

    @Override
    public void display() {
        System.out.println("我是普通僵尸");
    }

    @Override
    void move() {
        moveable.move();
    }

    @Override
    void attack() {
        attackable.attack();
    }
}

实际运用

package strategy;

import java.util.Arrays;
import java.util.Comparator;

public class ComparatorTest {
    public static void main(String[] args) {
        Person[]  persons = {new Person(10,99),new Person(18,99),new Person(15,122)};
        //Arrays.sort(persons,new SortByAge()); //年纪进行排序
        Arrays.sort(persons,new SortByHeight());//身高进行排序
        print(persons);
    }

    static void print(Person[] array){
        for (int i = 0; i <array.length ; i++) {
            System.out.println(array[i]);
        }
    }
}


class Person{
    int age;
    int height;

    public Person(int age, int height) {
        this.age = age;
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", height=" + height +
                '}';
    }
}

class SortByAge implements Comparator<Person> {


    @Override
    public int compare(Person o1, Person o2) {
        if(o1.getAge()>o2.getAge()){
            return 1;
        }else if(o1.getAge()<o2.getAge()){
            return -1;
        }
        return 0;
    }
}

class SortByHeight implements Comparator<Person>{

    @Override
    public int compare(Person o1, Person o2) {
        if(o1.getHeight() > o2.height){
            return 1;
        }else if(o1.getHeight()<o2.getHeight()){
            return -1;
        }
        return 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值