java设计模式进阶_command

原创 2016年08月31日 10:11:33

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Command.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public abstract class Command {
    public abstract void execute(Target target);
    public abstract void undo();
    public abstract void redo();
    public abstract String toString();
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : InvisibilitySpell.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public class InvisibilitySpell extends Command {

    private Target target;

    public void execute(Target target) {
        target.setVisibility(Visibility.INVISIBLE);
        this.target = target;
    }

    public void undo() {
        if(target != null){
            target.setVisibility(Visibility.VISIBLE);
        }
    }

    @Override
    public void redo() {
        if(target != null){
            target.setVisibility(Visibility.INVISIBLE);
        }
    }

    public String toString() {
        return "Invisibility spell";
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ShrinkSpell.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public class ShrinkSpell extends Command {

    private Size oldSize;
    private Target target;

    public void execute(Target target) {
        oldSize = target.getSize();
        target.setSize(Size.SMALL);
        this.target = target;
    }

    public void undo() {
        if(oldSize != null && target != null)
        {
            Size temp = target.getSize();
            target.setSize(oldSize);
            oldSize = temp;
        }
    }

    public String toString() {
        return "Shrink spell";
    }

    @Override
    public void redo() {
        undo();
    }
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Target.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public abstract class Target {

    private Size size;

    private Visibility visibility;

    public Size getSize() {
        return size;
    }

    public void setSize(Size size) {
        this.size = size;
    }

    public Visibility getVisibility() {
        return visibility;
    }

    public void setVisibility(Visibility visibility) {
        this.visibility = visibility;
    }

    public abstract String toString();

    public void printStatus() {
        System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
                getSize(), getVisibility()));
        System.out.println();
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Goblin.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public class Goblin extends Target {

    public Goblin(){
        setSize(Size.NORMAL);
        setVisibility(Visibility.VISIBLE);
    }

    public String toString() {
        return "Goblin";
    }
}

import java.util.Deque;
import java.util.LinkedList;

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Wizard.java
//  @ Date : 2016/8/31
//  @ Author : 
//
//




public class Wizard extends Target {

    private Deque<Command> undoStack = new LinkedList<>();
    private Deque<Command> redoStack = new LinkedList<>();

    public Wizard() {
        setSize(Size.NORMAL);
        setVisibility(Visibility.VISIBLE);
    }

    public void castSpell(Command command, Target target) {
        System.out.println(this + " casts " + command + " at " + target);
        command.execute(target);
        undoStack.offerLast(command);
    }

    public void undoLastSpell() {
        if(!undoStack.isEmpty())
        {
            Command previousSpell = undoStack.pollLast();
            redoStack.offerLast(previousSpell);
            System.out.println(this + " undoes " + previousSpell);
            previousSpell.undo();
        }
    }

    public void redoLastSpell(){
        if(!redoStack.isEmpty())
        {
            Command previousSpell = redoStack.pollLast();
            undoStack.offerLast(previousSpell);
            System.out.println(this + " redoes " + previousSpell);
            previousSpell.redo();
        }
    }

    public String toString() {
        return "Wizard";
    }

}

public enum Size {
    SMALL("small"), 
    NORMAL("normal"), 
    LARGE("large"),
    UNDEFINED("");

    private String title;

    Size(String title){
        this.title = title;
    }

    @Override
    public String toString() {
        return title;
    }
}

public enum Visibility {
    VISIBLE("visible"), 
    INVISIBLE("invisible"),
    UNDEFINED("");

    private String title;

    Visibility(String title){
        this.title = title;
    }

    @Override
    public String toString() {
        return title;
    }
}


public class App {

    public static void main(String[] args) {
        Wizard wizard = new Wizard();
        Goblin goblin =  new Goblin();

        goblin.printStatus();

        wizard.castSpell(new ShrinkSpell(), goblin);
        goblin.printStatus();

        wizard.castSpell(new InvisibilitySpell(), goblin);
        goblin.printStatus();

        wizard.undoLastSpell();
        goblin.printStatus();

        wizard.undoLastSpell();
        goblin.printStatus();

        wizard.redoLastSpell();
        goblin.printStatus();

        wizard.redoLastSpell();
        goblin.printStatus();
    }

}

/*
Goblin, [size=normal] [visibility=visible]

Wizard casts Shrink spell at Goblin
Goblin, [size=small] [visibility=visible]

Wizard casts Invisibility spell at Goblin
Goblin, [size=small] [visibility=invisible]

Wizard undoes Invisibility spell
Goblin, [size=small] [visibility=visible]

Wizard undoes Shrink spell
Goblin, [size=normal] [visibility=visible]

Wizard redoes Shrink spell
Goblin, [size=small] [visibility=visible]

Wizard redoes Invisibility spell
Goblin, [size=small] [visibility=invisible]
*/



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Android基础进阶day02 [01]Command设计模式

摘要 在图形化编程领域,都带一个 图形库,这个图形库的控件十分灵活,耦合度低。 一个按钮的强大: 登录,注册,CRUD....体现出灵活性与扩展性,其实就是耦合度低. 1.1. 耦合度低 比...

java设计模式进阶_template-method

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Stealin...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

Java进阶-设计模式

好久没有更新博客,这段时间太忙了。到了期末,忙里偷闲,整理整理学习笔记。什么是设计模式?在软件工程中,【设计模式】是对软件设计中普遍存在的各种问题,所提出的 解决方案。换句话说,设计模式是一套被反复使...

java设计模式进阶_dao

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Custome...

java设计模式进阶_factory-method

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Blacksm...

java设计模式进阶_bridge

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : MagicWe...

java设计模式进阶_memento

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : StarMem...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)