> 文档中心 > 设计模式笔记

设计模式笔记

在这里插入图片描述

命令模式

设计模式笔记

接收者角色(Receiver):负责具体执行一个请求;(doit、undo可以抽出来)
命令角色(ICommand):定义需要执行的所有命令行为;
具体的命令角色(ConcreteCommand):内部维护一个Receiver;
请求者角色(Invoker):接收客户端的命令,并执行命令;(Main)

状态模式

设计模式笔记

工厂模式

简单工厂

设计模式笔记

SimpleVehicleFactory

负责实现创建所有实例的内部逻辑。工厂类的创建产品类的方法可以被外界直接调用,创建所需的产品对象。

工厂方法

设计模式笔记

CarFactory

工厂方法模式是对简单工厂模式的进一步抽象化,其好处是可以使系统在不修改原来代码的情况下引进新的产品,即满足开闭原则

抽象工厂

设计模式笔记

比如华为产品(路由、手机)可以形成一个抽象工厂

构建模式

设计模式笔记

代理模式

静态代理

设计模式笔记

动态代理

JDK

Proxy.newProxyInstance(Tank.class.getClassLoader(),  new Class[]{Movable.class}, //tank.class.getInterfaces()  new LogHander(tank)

设计模式笔记

cgIib

public class Main {    public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Tank.class); enhancer.setCallback(new TimeMethodInterceptor()); Tank tank = (Tank)enhancer.create(); tank.move();    }}class TimeMethodInterceptor implements MethodInterceptor {    @Override    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println(o.getClass().getSuperclass().getName()); System.out.println("before"); Object result = null; result = methodProxy.invokeSuper(o, objects); System.out.println("after"); return result;    }}class Tank {    public void move() { System.out.println("Tank moving claclacla..."); try {     Thread.sleep(new Random().nextInt(10000)); } catch (InterruptedException e) {     e.printStackTrace(); }    }}

设计模式笔记

装饰模式

设计模式笔记

享元模式

设计模式笔记

组合模式

设计模式笔记

桥接模式

class RedCircle implements DrawAPI {    @Override    public void drawCircle(int x, int y, int radius) { System.out.println("Drawing Circle[ color: red, radius: "  + radius + ", x: " + x + ", " + y + "]");    }}class GreenCircle implements DrawAPI {    @Override    public void drawCircle(int x, int y, int radius) { System.out.println("Drawing Circle[ color: green, radius: "  + radius + ", x: " + x + ", " + y + "]");    }}abstract class Shape {    protected DrawAPI drawAPI;    protected Shape(DrawAPI drawAPI) { this.drawAPI = drawAPI;    }    public abstract void draw();}class Circle extends Shape {    private int x;    private int y;    private int radius;    public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius;    }    @Override    public void draw() { drawAPI.drawCircle(radius, x, y);    }}public class Main {    public static void main(String[] args) { Shape redCircle = new Circle(100, 100, 10, new RedCircle()); Shape greenCircle = new Circle(100, 100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw();    }}

设计模式笔记

适配器模式

class Adaptee {    public void specificRequest() { System.out.println("特殊请求");    }}class Adapter extends Adaptee implements Target {    @Override    public void request() { super.specificRequest();    }}class ConcreteTarget implements Target {    @Override    public void request() { System.out.println("普通请求");    }}public class Main {    public static void main(String[] args) { //原有业务逻辑 Target target = new ConcreteTarget(); target.request(); //增加适配器后的业务逻辑 Target target2 = new Adapter(); target2.request();    }}public interface Target {    void request();}

设计模式笔记

策略模式

class BackDoor implements Strategy {    @Override    public void operate() { System.out.println("找乔国老帮忙,让吴国太给孙权施加压力,使孙权不能杀刘备");    }}class GivenGreenLight implements Strategy {    @Override    public void operate() { System.out.println("求吴国太开个绿灯,放行");    }}class BlackEnemy implements Strategy {    @Override    public void operate() { System.out.println("孙夫人断后,挡住追兵");    }}class Context {    private Strategy strategy;    //构造函数,要你使用哪个妙计    public Context(Strategy strategy){ this.strategy = strategy;    }    public void setStrategy(Strategy strategy){ this.strategy = strategy;    }    public void operate(){ this.strategy.operate();    }}public class Main {    public static void main(String[] args) { Context context; System.out.println("----------刚到吴国使用第一个锦囊---------------"); context = new Context(new BackDoor()); context.operate(); System.out.println("----------刘备乐不思蜀使用第二个锦囊---------------"); context.setStrategy(new GivenGreenLight()); context.operate(); System.out.println("----------孙权的追兵来了,使用第三个锦囊---------------"); context.setStrategy(new BlackEnemy()); context.operate();    }}public interface Strategy {    public void operate();}

设计模式笔记

责任链

设计模式笔记

设计模式笔记

观察者

/** * Subject */public class Subject {    private List<Observer> observers = new ArrayList<Observer>();    private int state;    public int getState() { return state;    }    // set时(值被改变了)调用 notifyAllObservers 方法    public void setState(int state) { this.state = state; notifyAllObservers();    }    // 加入监听器(提供给监听器类使用的)    public void attach(Observer observer) { observers.add(observer);    }    // 通知所有的监听器    public void notifyAllObservers() { for (Observer observer : observers) {     observer.print(); }    }}/** * Observer */abstract class Observer {    // 绑定一个 Subject    protected Subject subject;    public abstract void print();}/** * BinaryObserver */class BinaryObserver extends Observer {    public BinaryObserver(Subject subject) { // 绑定一个 Subject this.subject = subject; // 将自己加入到 Subject 的监听器中 this.subject.attach(this);    }    @Override    public void print() { System.out.println("Binary String: " + Integer.toBinaryString(subject.getState()));    }}class Main {    public static void main(String[] args) { Subject subject = new Subject(); new BinaryObserver(subject); System.out.println("第一个状态变化: 15"); subject.setState(15); System.out.println("第二个状态变化: 10"); subject.setState(10);    }}

设计模式笔记

迭代器

设计模式笔记

访问者

设计模式笔记

模板

设计模式笔记