|
********行為型模式*************
//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;
//}
//}
//Strategy
//跟Bridge相類似,就是一種多態的表示
//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);
}
}
//Iteraotr
//使用迭代器對一個類的資料結構進行順序迭代
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;
}
}
//程式最終還是順序執行的,是由不通部分的操作拼接起來的
//將不同類的代碼拼接起來是通過引用實現的,有了引用我就
//相當於有了一定訪問資料結構和方法的能力,這與寫在類內部
//差不多,例如我想將一個類中的一個方法抽離出去,因為這個方法依賴與此類的資料和其他方法
//直接將代碼移走是不行的,但如果我們擁有了此類對象的引用,則與寫在此類
//內部無異,所以我們擁有了引用就可以將此方法移出
public class tt1 {
public static void main(String[] args) {
}
} |