行为型设计模式---模板方法模式和状态模式

一、模板方法模式

       模板方法模式定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤,其中各子类中公共的行为被提取出来并集中到一个公共父类中。符合开闭原则


       定义的游戏骨架抽象类:

	public abstract class Game {
	    protected final void initialize() {
	        if(isNeedChangeShoes()) {
	            System.out.println("先换鞋...");
	        }
	    }
	
		//钩子方法
	    protected boolean isNeedChangeShoes() {
	        return false;
	    }
	
	    abstract void startPlay();
	
	    abstract void endPlay();
	
	    //模板
	    protected final void play() {
	        //初始化游戏
	        initialize();
	
	        //开始游戏
	        startPlay();
	
	        //结束游戏
	        endPlay();
	    }
	}

       足球的实现类:

	public class Football extends Game {
	    @Override
	    protected boolean isNeedChangeShoes() {
	        return true;
	    }
	
	    @Override
	    void startPlay() {
	        System.out.println("Football Game Started. Enjoy the game!");
	    }
	
	    @Override
	    void endPlay() {
	        System.out.println("Football Game Finished!");
	    }
	}

       乒乓的实现类:

	public class PingPong extends Game {
	    @Override
	    void startPlay() {
	        System.out.println("Basketball Game Started. Enjoy the game!");
	    }
	
	    @Override
	    void endPlay() {
	        System.out.println("Basketball Game Finished!");
	    }
	}

       测试类:

	public class GameTest {
	    public static void main(String[] args) {
	        Game football = new Football();
	        football.play();
	        Game basketball = new PingPong();
	        basketball.play();
	    }
	}

       测试结果:

Alt




二、状态模式

       状态模式允许一个对象在其内部状态改变时,改变它的行为,并且一个对象存在多个状态,且状态可相互转换。每次需要增加状态的时候新增一个类即可,符合开闭原则


       上下文的类:

	public class CourseVideoContext {
	    private CourseVideoState courseVideoState;
	    public final static PlayState PLAY_STATE = new PlayState();
	    public final static PauseState PAUSE_STATE = new PauseState();
	    public final static StopState STOP_STATE = new StopState();
	    public final static SpeedState SPEED_STATE = new SpeedState();
	
	    public CourseVideoState getCourseVideoState() {
	        return courseVideoState;
	    }
	
	    public void setCourseVideoState(CourseVideoState courseVideoState) {
	        this.courseVideoState = courseVideoState;
	        this.courseVideoState.setCourseVideoContext(this);
	    }
	
	    public void play() {
	        this.courseVideoState.play();
	    }
	
	    public void pause() {
	        this.courseVideoState.pause();
	    }
	
	    public void stop() {
	        this.courseVideoState.stop();
	    }
	
	    public void speed() {
	        this.courseVideoState.speed();
	    }
	}

       表示对象状态的抽象类:

	public abstract class CourseVideoState {
	    protected CourseVideoContext courseVideoContext;
	
	    public void setCourseVideoContext(CourseVideoContext courseVideoContext) {
	        this.courseVideoContext = courseVideoContext;
	    }
	
	    public abstract void play();
	
	    public abstract void pause();
	
	    public abstract void stop();
	
	    public abstract void speed();
	}

       播放状态类:

	public class PlayState extends CourseVideoState {
	    @Override
	    public void play() {
	        System.out.println("正常播放课程视频状态");
	    }
	
	    @Override
	    public void pause() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
	    }
	
	    @Override
	    public void stop() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
	    }
	
	    @Override
	    public void speed() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
	    }
	}

       暂停状态类:

	public class PauseState extends CourseVideoState {
	    @Override
	    public void play() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
	    }
	
	    @Override
	    public void pause() {
	        System.out.println("暂停播放课程视频状态");
	    }
	
	    @Override
	    public void stop() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
	    }
	
	    @Override
	    public void speed() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);
	    }
	}

       加速状态类:

	public class SpeedState extends CourseVideoState {
	    @Override
	    public void play() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
	    }
	
	    @Override
	    public void pause() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);
	    }
	
	    @Override
	    public void stop() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);
	    }
	
	    @Override
	    public void speed() {
	        System.out.println("快进播放课程视频状态");
	    }
	}

       停止状态类:

	public class StopState extends CourseVideoState {
	    @Override
	    public void play() {
	        super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
	    }
	
	    @Override
	    public void pause() {
	        System.out.println("ERROR 停止状态不能暂停");
	    }
	
	    @Override
	    public void stop() {
	        System.out.println("停止播放课程视频状态");
	    }
	
	    @Override
	    public void speed() {
	        System.out.println("ERROR 停止状态不能快进");
	    }
	}

       测试类:

	public class StateTest {
	    public static void main(String[] args) {
	        CourseVideoContext courseVideoContext = new CourseVideoContext();
	        courseVideoContext.setCourseVideoState(new PlayState());
	
	        System.out.println("当前状态: "+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
	        courseVideoContext.pause();
	
	        System.out.println("当前状态: "+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
	        courseVideoContext.speed();
	
	        System.out.println("当前状态: "+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
	        courseVideoContext.stop();
	
	        System.out.println("当前状态: "+courseVideoContext.getCourseVideoState().getClass().getSimpleName());
	        courseVideoContext.speed();
	    }
	}

       测试结果:

Alt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值