1、命令模式
package com.yqj.pattern.command;
class LightReceiver{
public void on(){
System.out.println("开灯");
}
public void off(){
System.out.println("关灯");
}
}
interface Command{
public void execute();
public void undo();
}
class LightOnCommand implements Command{
private LightReceiver lightReceiver;
public LightOnCommand(LightReceiver lightReceiver) {
this.lightReceiver = lightReceiver;
}
@Override
public void execute() {
lightReceiver.on();
}
@Override
public void undo() {
lightReceiver.off();
}
}
class LightOffCommand implements Command{
private LightReceiver lightReceiver;
public LightOffCommand(LightReceiver lightReceiver) {
this.lightReceiver = lightReceiver;
}
@Override
public void execute() {
lightReceiver.off();
}
@Override
public void undo() {
lightReceiver.on();
}
}
class NoCommand implements Command{
@Override
public void execute() {
}
@Override
public void undo() {
}
}
class RemoteController{
private Command[] onCommands;
private Command[] offCommands;
private Command undoCommand;
public RemoteController() {
onCommands = new Command[5];
offCommands = new Command[5];
undoCommand = new NoCommand();
for (int i=0 ; i<5 ; i++){
onCommands[i] = new NoCommand();
offCommands[i] = new NoCommand();
}
}
public void setCommand(int index,Command onCommand,Command offCommand){
onCommands[index] = onCommand;
offCommands[index] = offCommand;
}
public void onButtonWasPushed(int index){
onCommands[index].execute();
undoCommand = onCommands[index];
}
public void offButtonWasPushed(int index){
offCommands[index].execute();
undoCommand = offCommands[index];
}
public void undoButtonWasPushed(){
undoCommand.undo();
}
}
public class Client {
public static void main(String[] args) {
LightReceiver lightReceiver = new LightReceiver();
LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
RemoteController remoteController = new RemoteController();
remoteController.setCommand(0,lightOnCommand,lightOffCommand);
remoteController.onButtonWasPushed(0);
remoteController.offButtonWasPushed(0);
remoteController.undoButtonWasPushed();
}
}
2、访问者模式
package com.yqj.pattern.visitor;
import java.util.ArrayList;
import java.util.List;
abstract class People{
private String name;
public People(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract void accept(Action action);
}
class Man extends People{
public Man(String name) {
super(name);
}
@Override
public void accept(Action action) {
action.getManResult(this);
}
}
class Woman extends People{
public Woman(String name) {
super(name);
}
@Override
public void accept(Action action) {
action.getWomanResult(this);
}
}
abstract class Action{
public abstract void getManResult(Man man);
public abstract void getWomanResult(Woman woman);
}
class Success extends Action{
@Override
public void getManResult(Man man) {
System.out.println("男的赞同 "+ man.getName());
}
@Override
public void getWomanResult(Woman woman) {
System.out.println("女的赞同 " + woman.getName());
}
}
class Fail extends Action{
@Override
public void getManResult(Man man) {
System.out.println("男的反对" + man.getName());
}
@Override
public void getWomanResult(Woman woman) {
System.out.println("女的反对" + woman.getName());
}
}
class ObjectStructure{
private List<People> elements = new ArrayList<>();
public void add(People p){
elements.add(p);
}
public void remove(People p){
elements.remove(p);
}
public void display(Action action){
for (People element : elements) {
element.accept(action);
}
}
}
public class Client {
public static void main(String[] args) {
Action success = new Success();
Action fail = new Fail();
Man bob = new Man("bob");
Man tom = new Man("tom");
Woman alice = new Woman("alice");
ObjectStructure objectStructure = new ObjectStructure();
objectStructure.add(bob);
objectStructure.add(tom);
objectStructure.add(alice);
objectStructure.display(success);
objectStructure.display(fail);
}
}