//Adapter
//鍩烘湰鏂規硶鏈変袱縐嶏紝涓縐嶆槸浣跨敤寮曠敤涓縐嶄嬌鐢ㄧ戶鎵?
//灝嗕笉絎﹀悎鏍囧噯鐨勬帴鍙h漿鎴愮鍚堟爣鍑嗙殑鎺ュ彛錛屾帴鍙g殑淇敼涓昏鏄弬鏁扮殑澧炲噺錛?
//榪斿洖鍊肩被鍨?褰撶劧榪樻湁鏂規硶鍚?
//鎰熻榪欏氨鏄皝瑁呯殑鍙︿竴縐嶈〃紺哄艦寮忥紝灝佽鏈夌敤鏂規硶灝佽(鍦ㄦ柟娉曚腑璋冪敤鍔熻兘鏂規硶)錛?
//鐢ㄧ被灝佽(鍏堜紶鍏ュ姛鑳芥柟娉曟墍鍦ㄧ殑綾葷殑瀵硅薄錛岄氳繃璋冪敤姝ゅ璞$殑鍔熻兘鏂規硶)
//浣跨敤寮曠敤鐨勫艦寮?
class Adapteea {
public void kk() {}
}
interface Targeta {
String vv(int i, int k);
}
class Adaptera implements Targeta{
Adapteea ade;
public Adaptera(Adapteea ade) {
this.ade = ade;
}
public String vv(int i, int k) {
//鍏蜂綋鐨勪笟鍔℃柟娉曞疄鐜板湪Adaptee涓紝榪欎釜鏂規硶
//鍙搗鍒頒簡鎺ュ彛杞崲鐨勪綔鐢?
//璋冪敤姝ゆ柟娉曟槸閫氳繃寮曠敤
ade.kk();
return null;
}
}
//浣跨敤緇ф壙褰㈠紡鐨?/span>
class Adapteeb {
public void kk() {}
}
interface Targetb {
String vv(int i, int k);
}
class Adapterb extends Adapteeb implements Targetb {
public String vv(int i, int k) {
//璋冪敤姝ゆ柟娉曟槸閫氳繃緇ф壙
kk();
return null;
}
}
//Proxy
interface Subject {
void request();
}
class realSubject implements Subject {
public void request() {
//do the real business
}
}
class Proxy implements Subject {
Subject subject;
public Proxy(Subject subject) {
this.subject = subject;
}
public void request() {
System.out.println("do something");
subject.request();
System.out.println("do something");
}
}
//Bridge
//鎰熻灝辨槸澶氭佺殑瀹炵幇
interface Imp {
void operation();
}
class Cimp1 implements Imp {
public void operation() {
System.out.println("1");
}
}
class Cimp2 implements Imp {
public void operation() {
System.out.println("2");
}
}
class Invoker {
Imp imp = new Cimp1();
public void invoke() {
imp.operation();
}
}
//Composite
interface Component {
void operation();
void add(Component component);
void remove(Component component);
}
class Leaf implements Component {
public void operation() {
System.out.println("an operation");
}
public void add(Component component) {
throw new UnsupportedOperationException();
}
public void remove(Component component) {
throw new UnsupportedOperationException();
}
}
class Composite implements Component {
List components = new ArrayList();
public void operation() {
Component component = null;
Iterator it = components.iterator();
while (it.hasNext()) {
//涓嶇煡閬撴component瀵硅薄鏄痩eaf榪樻槸composite錛?
//濡傛灉鏄痩eaf鍒欑洿鎺ュ疄鐜版搷浣滐紝濡傛灉鏄痗omposite鍒欑戶緇掑綊璋冪敤
component = (Component) it.next();
component.operation();
}
}
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
}
//Decorator
//瀵逛竴涓被鐨勫姛鑳借繘琛屾墿灞曟椂錛屾垜鍙互浣跨敤緇ф壙錛屼絾鏄笉澶熺伒媧伙紝鎵浠ラ夌敤浜?
//鍙﹀鐨勪竴縐嶅艦寮?寮曠敤涓庣戶鎵塊兘鍙椿寰楀瀵硅薄鐨勪竴瀹氱殑浣跨敤鑳藉姏錛岃屼嬌鐢ㄥ紩鐢ㄥ皢鏇寸伒媧?
//鎴戜滑瑕佷繚璇佹槸瀵瑰師鍔熻兘鐨勮拷鍔犺屼笉鏄慨鏀癸紝鍚﹀垯鍙兘閲嶅啓鏂規硶錛屾垨浣跨敤鏂扮殑鏂規硶
//娉ㄦ剰concrete鐨勫彲浠ョ洿鎺ew鍑烘潵錛?
//鑰宒ecorator鐨勫垯闇瑕佺敤涓涓彟澶栫殑decorator瀵硅薄鎵嶈兘鐢熸垚瀵硅薄
//浣跨敤瀵硅薄灝佽錛屽拰鍏敤鎺ュ彛
//Decorator閾句笂鍙互鏈夊涓厓绱?/span>
interface Componenta {
void operation();
}
class ConcreteComponent implements Componenta {
public void operation() {
System.out.println("do something");
}
}
class Decorator implements Componenta {
private Componenta component;
public Decorator(Componenta component) {
this.component = component;
}
public void operation() {
//do something before
component.operation();
//do something after
}
}
//Facade
//闈炲父瀹炵敤鐨勪竴縐嶈璁℃ā寮忥紝鎴戝彲浠ヤ負澶栭儴鎻愪緵鎰熷叴瓚g殑鎺ュ彛
class Obj1 {
public void ope1() {}
public void ope2() {}
}
class Obj2 {
public void ope1() {}
public void ope2() {}
}
class Facade {
//鎴戝緱鍒頒簡涓涓畝媧佹竻鏅扮殑鎺ュ彛
public void fdMethod() {
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();
obj1.ope1();
obj2.ope2();
}
}
//Flyweight
//絀?/span>
//Chain of Responsibility
//涓嶥ecorator鐨勫疄鐜板艦寮忕浉綾諱技錛?
//Decorator鏄湪鍘熸潵鐨勬柟娉曚箣涓婅繘琛屾坊鍔犲姛鑳斤紝鑰?
//Chain鍒欐槸鍒ゆ柇淇″彿濡傛灉涓嶆槸褰撳墠澶勭悊鐨勫垯杞氦涓笅涓涓妭鐐瑰鐞?
//鎴戝彲浠ヤ嬌鐢╥f鍒嗘敮鏉ュ疄鐜扮浉鍚岀殑鏁堟灉錛屼絾鏄笉澶熺伒媧伙紝閾句笂鐨勬瘡涓妭鐐規槸鍙互鏇挎崲澧炲姞鐨勶紝鐩稿
//姣旇緝鐏墊椿錛屾垜浠彲浠ヨ璁℃帴鍙e疄鐜板鑺傜偣鐨勫鍒犳搷浣滐紝鑰屽疄鐜版洿鏂逛究鐨勬晥鏋?
//榪欎釜鏄竴涓摼鐘剁殑緇撴瀯錛屾湁娌℃湁鎯寵繃浣跨敤鐜姸緇撴瀯
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
//鎰熻璺烠omposite寰堢被浼鹼紝鍙笉榪囦粬鍒嗘枃緇堢粨絎﹀拰闈炵粓緇撶
//Template Method
abstract class TemplateMethod {
abstract void amd1();
abstract void amd2();
//姝ゆ柟娉曚負涓涓猅emplate Method鏂規硶
public void tmd() {
amd1();
amd2();
}
}
//State
//鏍囧噯鍨?
//鐘舵佸拰鎿嶄綔涓嶅簲璇ヨ﹀悎鍦ㄤ竴璧?/span>
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
//璺烞ridge鐩哥被浼鹼紝灝辨槸涓縐嶅鎬佺殑琛ㄧず
//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];
//浣跨敤鍐呴儴綾繪槸涓轟簡瀵筍truture1鐨勬暟鎹粨鏋勬湁瀹屽叏鐨勮闂潈
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涓嚭鐜癆2瀵硅薄鐨勫紩鐢紝
//鎵浠ユ垜浣跨敤浜哅ediator浣滀負涓粙
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
//鍦ㄨ繖閲岀湅浼間箮榪欎釜妯″紡娌℃湁浠涔堢敤
//浣嗘槸濡傛灉鎴戞湁涓涓嚎紼嬬洃鎺ubject錛屽鏋淪ubject鐨勭姸鎬?
//鍙戠敓浜嗗彉鍖栵紝鍒欐洿鏀筄bserver鐨勭姸鎬侊紝騫跺嚭鍙戜竴浜涙搷浣滐紝榪欐牱灝辨湁瀹為檯鐨勬剰涔変簡
//Observer涓嶸isitor鏈夌浉浼肩殑鍦版柟錛岄兘瀛樺湪鍙屽悜寮曠敤
//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() {
//涓浜涘熀浜巗tate鐨勬搷浣?
}
}
//Memento
//鎰熻姝ゆā寮忔病鏈変粈涔堝ぇ鐢?/span>
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) {
}
}
閲岄潰鏈夊緢澶氫笉閿欑殑璁捐鎬濇兂.