- 备忘录模式
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
- 外观模式
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
- 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();
}
}
网友评论