行为型模式

Chain of Responsibility
//与Decorator的实现形式相类似,
//Decorator是在原来的方法之上进行添加功能,而
//Chain则是判断信号如果不是当前处理的则转交个下一个节点处理
//我可以使用if分支来实现相同的效果,但是不够灵活,链上的每个节点是可以替换增加的,相对
//比较灵活,我们可以设计接口实现对节点的增删操作,而实现更方便的效果
//这个是一个链状的结构,有没有想过使用环状结构

interface Handler {
void handRequest(int signal);
}


class CHandler1 implements Handler {
private Handler handler;

public CHandler1(Handler handler) {
this.handler = handler;
}


public void handRequest(int signal) {
if (signal == 1{
System.out.println(
"handle signal 1");
}

else {
handler.handRequest(signal);
}

}
 
}


class CHandler2 implements Handler {
private Handler handler;

public CHandler2(Handler handler) {
this.handler = handler;
}


public void handRequest(int signal) {
if (signal == 2{
System.out.println(
"handle signal 2");
}

else {
handler.handRequest(signal);
}

}
 
}


class CHandler3 implements Handler {
public void handRequest(int signal) {
if (signal == 3{
System.out.println(
"handle signal 3");
}

else {
throw new Error("can't handle signal");
}

}
 
}


class ChainClient {
public static void main(String[] args) {
Handler h3 
= new CHandler3();
Handler h2 
= new CHandler2(h3);
Handler h1 
= new CHandler1(h2);

h1.handRequest(
2);
}

}

 Interpreter
//感觉跟Composite很类似,只不过他分文终结符和非终结符

//Template Method

abstract class TemplateMethod {
abstract void amd1();

abstract void amd2();

//此方法为一个Template Method方法
public void tmd() {
amd1();
amd2();
}

}

State
//标准型
//状态和操作不应该耦合在一起
class Contexta {
private State st;

public Contexta(int nst) {
changeStfromNum(nst);
}


public void changeStfromNum(int nst) {
if (nst == 1{
st 
= new CStatea1();
}

else if (nst == 2{
st 
= new CStatea2();
}


throw new Error("bad state");
}


void request() {
st.handle(
this);
}

}


interface State {
void handle(Contexta context);
}


class CStatea1 implements State {
public void handle(Contexta context) {
System.out.println(
"state 1");
//也许在一个状态的处理过程中要改变状态,例如打开之后立即关闭这种效果
//context.changeStfromNum(2);
}

}


class CStatea2 implements State {
public void handle(Contexta context) {
System.out.println(
"state 2");
}

}


//工厂型
//根据状态不通生成不同的state

class StateFactory {
public static State getStateInstance(int num) {
State st 
= null;

if (num == 1{
st 
= new CStatea1();
}

else if (num == 2{
st 
= new CStatea2();
}


return st;
}

}

Visitor

//双向引用,使用另外的一个类调用自己的方法,访问自己的数据结构
interface Visitor {
void visitElement(Elementd element);
}


class CVisitor implements Visitor {
public void visitElement(Elementd element) {
element.operation();
}

}


interface Elementd {
void accept(Visitor visitor);

void operation();
}


class CElementd implements Elementd {
public void accept(Visitor visitor) {
visitor.visitElement(
this);
}


public void operation() {
//实际的操作在这里
}

}


class Clientd {
public static void main() {
Elementd elm 
= new CElementd();
Visitor vis 
= new CVisitor();

vis.visitElement(elm);
}

}
Iteretor
interface Structure {
interface Iteratora {
void first();

boolean hasElement();

Object next();

}

}


class Structure1 implements Structure {
Object[] objs 
= new Object[100];

//使用内部类是为了对Struture1的数据结构有完全的访问权
class Iteratora1 implements Iteratora {
int index = 0;

public void first() {
index 
= 0;
}


public boolean hasElement() {
return index < 100;
}
 

public Object next() {
Object obj 
= null;

if (hasElement()) {
obj 
= objs[index];
index
++;
}


return obj;
}

}

}

Meditor
class A1 {
public void operation1() {}
public void operation2() {}
}


class A2 {
public void operation1() {}
public void operation2() {}
}


class Mediator {
A1 a1;
A2 a2;

public Mediator(A1 a1, A2 a2) {
this.a1 = a1;
this.a2 = a2;

}


//如果我想实现这个功能我可能会把他放在A1中
//但是这样耦合大,我不想在A1中出现A2对象的引用,
//所以我使用了Mediator作为中介
public void mmed1() {
a1.operation1();
a2.operation2();
}


public void mmed2() {
a2.operation1();
a1.operation2();
}

}

Command
//我认为就是将方法转换成了类

class Receiver {
public void action1() {}

public void action2() {}
}


interface Command {
void Execute();
}


class CCommand1 implements Command {
private Receiver receiver;

public CCommand1(Receiver receiver) {
this.receiver = receiver;
}


public void Execute() {
receiver.action1();
}

}


class CCommand2 implements Command {
private Receiver receiver;

public CCommand2(Receiver receiver) {
this.receiver = receiver;
}


public void Execute() {
receiver.action2();
}

}

Observer
//在这里看似乎这个模式没有什么用
//但是如果我有一个线程监控Subject,如果Subject的状态
//发生了变化,则更改Observer的状态,并出发一些操作,这样就有实际的意义了
//Observer与Visitor有相似的地方,都存在双向引用
//Subject可以注册很多Observer

interface Subjectb {
void attach(Observer observer);

void detach(Observer observer);

void mynotify();

int getState();

void setState(int state);
}


class Subjectb1 implements Subjectb {
List observers 
= new ArrayList();
int state;

public void attach(Observer observer) {
observers.add(observer);
}


public void detach(Observer observer) {
observers.remove(observer);
}


public void mynotify() {
Observer observer 
= null;
Iterator it 
= observers.iterator();

while (it.hasNext()) {
observer 
= (Observer) it.next();
observer.Update();
}

}


public int getState() {
return state;
}


public void setState(int state) {
this.state = state;
}

}


interface Observer {
void Update();
}


class Observer1 implements Observer {
Subjectb subject;
int state;

public Observer1(Subjectb subject) {
this.subject = subject;
}


public void Update() {
this.state = subject.getState();
}


public void operation() {
//一些基于state的操作
}

}

Memento
class Memento {
int state;

public int getState() {
return state;
}


public void setState(int state) {
this.state = state;
}

}


class Originator {
int state;

public void setMemento(Memento memento) {
state 
= memento.getState();
}


public Memento createMemento() {
Memento memento 
= new Memento();
memento.setState(
1);
return memento;
}


public int getState() {
return state;
}


public void setState(int state) {
this.state = state;
}

}


class careTaker {
Memento memento;

public void saverMemento(Memento memento) {
this.memento = memento;
}


public Memento retrieveMemento() {
return memento;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值