美文网首页
2.设计模式用例(二)

2.设计模式用例(二)

作者: 月巴月巴 | 来源:发表于2018-12-16 01:13 被阅读0次
  1. 备忘录模式
package designPattern;

import java.util.ArrayList;
import java.util.List;

class Text {
    String content;

    public Text(String content) {
        this.content = content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void showContent() {
        System.out.println(content);
    }

    public Record makeRecord() {
        // make a defensive copy, since Text is not immutable
        return new Record(new Text(content));
    }
}

class Record {
    Text text;
    Record(Text text) {
        this.text = text;
    }

    public Text getText() {
        return text;
    }
}

class TextHistory {
    List<Record> records = new ArrayList<>();
    void saveRecord(Record record) {
        records.add(record);
    }
    Record getRecord(int index) {
        return records.get(index);
    }
}
public class MomentoStudy {
    public static void main(String[] args) {
        TextHistory textHistory = new TextHistory();
        Text text = new Text("");
        text.setContent("apple");
        text.setContent("apple banana");
        textHistory.saveRecord(text.makeRecord()); // 1st save - apple banana
        text.setContent("tree");
        textHistory.saveRecord(text.makeRecord()); // 2nd save - tree
        text.setContent("Gladiator");
        textHistory.saveRecord(text.makeRecord()); // 3rd save - Gladiator

        textHistory.getRecord(0).getText().showContent(); // show apple banana
        textHistory.getRecord(1).getText().showContent(); // show tree
    }

}

2.装饰器模式

package designPattern;

/**
 * Decorator accepts the object and change its behavior
 * Similar to Adapter pattern, may be for a different purpose
 */
interface Animal {
    void act();
}

class Person implements Animal {
    @Override
    public void act() {
       System.out.println("Do nothing");
    }
}

class ITDecorator implements Animal {
    Person person;

    public ITDecorator(Person person) {
        this.person = person;
    }

    @Override
    public void act() {
        person.act();
        System.out.println("Write bugs");
    }
}
public class DecoratorStudy {
    public static void main(String[] args) {
        Person person = new Person();
        ITDecorator itDecorator = new ITDecorator(person);
        itDecorator.act();
    }
}
/// output
// Do nothing
// Write bugs
  1. 外观模式
class Player {
    public void play() {
        System.out.println("Start playing");
    }
}

class Video {
    public void show() {
        System.out.println("Show the map.");
    }
}

class Audio {
    public void sound() {
        System.out.println("Play BGM.");
    }
}

class GameFacade {
    private Player player;
    private Video video;
    private Audio audio;

    public GameFacade(Player player, Video video, Audio audio) {
        this.player = player;
        this.video = video;
        this.audio = audio;
    }

    public void play() {
        audio.sound();
        video.show();
        player.play();
    }
}

public class FacadeStudy {
    public static void main(String[] args) {
        // so we don't have to turn on everything separately
        GameFacade gameFacade = new GameFacade(new Player(), new Video(), new Audio());
        gameFacade.play();
    }
}
// output
//Play BGM.
//Show the map.
//Start playing

  1. State Pattern
// Task Pool - Accepted - started - finished
public abstract class TaskStatus {
    private TaskContext taskContext;

    public TaskContext getTaskContext() {
        return taskContext;
    }

    public void setTaskContext(TaskContext taskContext) {
        this.taskContext = taskContext;
    }
    public void noOperation(String opName) {
        System.out.println("Current status " + getClass().getSimpleName() + " cannot support the operation " + opName );
    }
    public abstract void show();
    // status transition
    public abstract void pool();
    public abstract void accept();
    public abstract void start();
    public abstract void finish();
}


public class TaskPool extends TaskStatus {
    public void accept() {
        getTaskContext().setTaskStatus(new TaskAccepted());
        getTaskContext().getTaskStatus().show();
    }

    public void pool() {
        noOperation("pool");
    }
    public void start(){
        noOperation("start");
    }
    public void finish(){
        noOperation("finish");
    }

    public void show() {
        System.out.println("task in city pool");
    }
}

public class TaskAccepted extends TaskStatus {
    public void accept() {
        noOperation("accept");
    }

    public void pool() {
        getTaskContext().setTaskStatus(new TaskPool());
        getTaskContext().getTaskStatus().show();
    }
    public void start(){
        getTaskContext().setTaskStatus(new TaskStarted());
        getTaskContext().getTaskStatus().show();
    }
    public void finish(){
        noOperation("finish");
    }
    public void show() {
        System.out.println("task accepted");
    }
}

public class TaskStarted extends TaskStatus {
    public void accept() {
        getTaskContext().setTaskStatus(new TaskAccepted());
        getTaskContext().getTaskStatus().show();
    }

    public void pool() {
        noOperation("pool");
    }
    public void start(){
        noOperation("start");
    }
    public void finish(){
        getTaskContext().setTaskStatus(new TaskFinished());
        getTaskContext().getTaskStatus().show();
    }

    public void show() {
        System.out.println("task started");
    }
}

public class TaskFinished extends TaskStatus {
    public void accept() {
        noOperation("accept");
    }

    public void pool() {
        noOperation("pool");
    }
    public void start(){
        getTaskContext().setTaskStatus(new TaskStarted());
        getTaskContext().getTaskStatus().show();
    }
    public void finish(){
        noOperation("finish");
    }

    public void show() {
        System.out.println("task finished");
    }
}

public class TaskContext {
    private TaskStatus taskStatus;

    public TaskContext() {
        this.taskStatus = new TaskPool();
        taskStatus.setTaskContext(this);
    }

    public TaskStatus getTaskStatus() {
        return taskStatus;
    }

    public void setTaskStatus(TaskStatus taskStatus) {
        this.taskStatus = taskStatus;
        taskStatus.setTaskContext(this);
    }

    public void pool() {
        taskStatus.pool();
    }
    public void accept() {
        taskStatus.accept();
    }
    public void start() {
        taskStatus.start();
    }
    public void finish() {
        taskStatus.finish();
    }
}

public class StatusStudy {
    public static void main(String[] args) {
        TaskContext taskContext = new TaskContext();
        // begin with the task pool
        // happy path of status transition back and forth
        taskContext.accept();
        taskContext.pool();
        taskContext.accept();
        taskContext.start();
        taskContext.accept();
        taskContext.start();
        taskContext.finish();
        taskContext.start();
        taskContext.finish();

        // transition fail
        TaskContext taskContextFail = new TaskContext();
        taskContextFail.finish();
        taskContextFail.start();
        taskContextFail.accept();
        taskContextFail.start();
        taskContextFail.pool();
        taskContextFail.finish();
        taskContextFail.accept();
    }
}

相关文章

  • 2.设计模式用例(二)

    备忘录模式 2.装饰器模式 外观模式 State Pattern

  • 23设计模式之一

    简述 一、设计模式的六大原则 二、23种设计模式 1.单例模式 懒汉式单例 饿汉单例 2.三种工厂模式 1>.简单...

  • 2.设计模式用例(一)

    工厂模式: 根据不同的歌手,生产出对应的歌。歌手是Visitor, 歌曲是Acceptor 访问者模式: Visi...

  • 设计模式一、单例模式

    系列传送门设计模式一、单例模式设计模式二、简单工厂模式设计模式三、工厂模式设计模式四、抽象工厂模式 简单单例(推荐...

  • 设计模式(Swift) - 3.观察者模式、建造者模式

    上一篇 设计模式(Swift) - 2.单例模式、备忘录模式和策略模式中讲了三种常见的设计模式. 单例模式: 限制...

  • 2.架构设计(单例模式设计)

    1.设计模式分为三个类 创建型 结构型 行为型 2.创建型:单例模式 为什么用单例模式?如果你看到这个问题,你怎么...

  • Python 面向对象7: 单例模式

    一、内容 1.1、单例设计模式 1.2、__new__方法 1.3、Python 中的单例 二、单例设计模式 2....

  • 项目开发-------iOS设计模式

    iOS的设计模式大体可以分为以下几种设计模式 1.创建型:单例设计模式、抽象工厂设计模式 2.结构型:MVC 模式...

  • Java基础(3)——抽象类和单例设计模式

    本节内容1.单例设计模式2.抽象类实现模板设计模式3.抽象类实现造房子 一、单例设计模式1.设计模式:对经常出现的...

  • 前端设计模式

    JS设计模式一:工厂模式jS设计模式二:单例模式JS设计模式三:模块模式JS设计模式四:代理模式JS设计模式五:职...

网友评论

      本文标题:2.设计模式用例(二)

      本文链接:https://www.haomeiwen.com/subject/wxozhqtx.html