//Adapter
//基本Ҏ(gu)有两U,一U是使用引用一U用?
//不W合标准的接口{成符合标准的接口Q接口的修改主要是参数的增减Q?
//q回值类?当然q有Ҏ(gu)?
//感觉q就是封装的另一U表CŞ式,装有用Ҏ(gu)装(在方法中调用功能Ҏ(gu))Q?
//用类装(先传入功能方法所在的cȝ对象Q通过调用此对象的功能Ҏ(gu))
//使用引用的Ş?
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中,q个Ҏ(gu)
//只vC接口转换的作?
//调用此方法是通过引用
ade.kk();
return null;
}
}
//使用l承形式?/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) {
//调用此方法是通过l承
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对象是leafq是compositeQ?
//如果是leaf则直接实现操作,如果是composite则l递归调用
component = (Component) it.next();
component.operation();
}
}
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
}
//Decorator
//对一个类的功能进行扩展时Q我可以使用l承Q但是不够灵z,所以选用?
//另外的一UŞ?引用与扉K可活得对对象的一定的使用能力Q而用引用将更灵z?
//我们要保证是对原功能的追加而不是修改,否则只能重写Ҏ(gu)Q或使用新的Ҏ(gu)
//注意concrete的可以直接new出来Q?
//而decorator的则需要用一个另外的decorator对象才能生成对象
//使用对象装Q和公用接口
//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
//非常实用的一U设计模式,我可以ؓ(f)外部提供感兴的接口
class Obj1 {
public void ope1() {}
public void ope2() {}
}
class Obj2 {
public void ope1() {}
public void ope2() {}
}
class Facade {
//我得C一个简z清晰的接口
public void fdMethod() {
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();
obj1.ope1();
obj2.ope2();
}
}
//Flyweight
//I?/span>
//Chain of Responsibility
//与Decorator的实现Ş式相cMQ?
//Decorator是在原来的方法之上进行添加功能,?
//Chain则是判断信号如果不是当前处理的则转交个下一个节点处?
//我可以用if分支来实现相同的效果Q但是不够灵z,链上的每个节Ҏ(gu)可以替换增加的,相对
//比较灉|Q我们可以设计接口实现对节点的增删操作,而实现更方便的效?
//q个是一个链状的l构Q有没有惌使用环状l构
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很类|只不q他分文l结W和非终l符
//Template Method
abstract class TemplateMethod {
abstract void amd1();
abstract void amd2();
//此方法ؓ(f)一个Template MethodҎ(gu)
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");
//也许在一个状态的处理q程中要改变状态,例如打开之后立即关闭q种效果
//context.changeStfromNum(2);
}
}
class CStatea2 implements State {
public void handle(Contexta context) {
System.out.println("state 2");
}
}
//工厂?
//Ҏ(gu)状态不通生成不同的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相类|是一U多态的表示
//Visitor
//双向引用Q用另外的一个类调用自己的方?讉K自己的数据结?
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() {
//实际的操作在q里
}
}
class Clientd {
public static void main() {
Elementd elm = new CElementd();
Visitor vis = new CVisitor();
vis.visitElement(elm);
}
}
//Iteraotr
//使用q代器对一个类的数据结构进行顺序P?
interface Structure {
interface Iteratora {
void first();
boolean hasElement();
Object next();
}
}
class Structure1 implements Structure {
Object[] objs = new Object[100];
//使用内部cLZ对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;
}
//如果我想实现q个功能我可能会(x)把他攑֜A1?
//但是q样耦合大,我不惛_A1中出现A2对象的引用,
//所以我使用了Mediator作ؓ(f)中介
public void mmed1() {
a1.operation1();
a2.operation2();
}
public void mmed2() {
a2.operation1();
a1.operation2();
}
}
//Command
//我认为就是将Ҏ(gu)转换成了c?
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
//在这里看gq个模式没有什么用
//但是如果我有一个线E监控SubjectQ如果Subject的状?
//发生了变化,则更改Observer的状态,q出发一些操作,q样有实际的意义了
//Observer与Visitor有相似的地方Q都存在双向引用
//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
//感觉此模式没有什么大?/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;
}
}
//E序最l还是顺序执行的Q是׃通部分的操作拼接h?
//不同类的代码拼接v来是通过引用实现的,有了引用我就
//相当于有了一定访问数据结构和Ҏ(gu)的能力,q与写在cd?
//差不多,例如我想一个类中的一个方法抽d去,因ؓ(f)q个Ҏ(gu)依赖与此cȝ数据和其他方?
//直接代码移走是不行的,但如果我们拥有了此类对象的引用,则与写在此类
//内部无异Q所以我们拥有了引用可以将此方法移?
public class tt1 {
public static void main(String[] args) {
}
}
里面有很多不错的设计思想.