设计模式
滚筒洗衣机xin
这个作者很懒,什么都没留下…
展开
-
设计模式汇总
一、设计模式六大原则1.开闭原则:类应该对修改封闭,对扩展开放。2.单一职责原则:一个类只负责一件事。3.里氏替换原则:子类可以替换父类,子类在继承父类时,只做功能的扩展,而不修改父类原有功能。4.依赖倒置原则:细节应该依赖抽象,而抽象不应该依赖细节,抽象位于程序的高层保持稳定不变,具体的实现由细节负责。5.迪米特法则:一个类不应知道自己操作的类的细节。6.接口隔离原则:当接口的部分方法被空实现时,应该将接口进行拆分,让类实现对应的接口。二、创建型设计模式设计模式之单例模式(S原创 2021-12-12 21:24:33 · 337 阅读 · 0 评论 -
设计模式之访问者模式(Visitor模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { ObjectStructure objectStructure = new ObjectStructure(); objectStructure.add(new ConcreteElementA()); objectStructure.add(new ConcreteEleme原创 2021-12-12 20:58:29 · 447 阅读 · 0 评论 -
设计模式之解释器模式(Interpreter模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Context context = new Context(); Expression terminalExpression = new TerminalExpression(); Expression expression = new NonTerminalExpressio原创 2021-12-12 20:33:57 · 232 阅读 · 0 评论 -
设计模式之状态模式(State模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Context context = new Context(); context.setState(new ConcreteStateA()); context.request(); }}State:public interface State {原创 2021-12-11 00:51:56 · 244 阅读 · 0 评论 -
设计模式之中介者模式(Mediator模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Mediator mediator = new ConcreteMediator(); Colleague colleague = new ConcreteColleague(); mediator.register(colleague); colleague.原创 2021-12-11 00:19:13 · 685 阅读 · 0 评论 -
设计模式之备忘录模式(Memento模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Caretaker caretaker = new Caretaker(); Originator originator = new Originator(); caretaker.setMemento(originator.createMemento());原创 2021-12-10 00:24:21 · 142 阅读 · 0 评论 -
设计模式之策略模式(Strategy模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Context context = new Context(new ConcreteStrategy()); context.contextMethod(); }}Strategy:public interface Strategy { void strategyM原创 2021-12-10 00:08:08 · 437 阅读 · 0 评论 -
设计模式之模板方法模式(Template Method模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { AbstractClass abstractClass = new ConcreteClass(); abstractClass.templateMethod(); }}AbstractClass:public abstract class AbstractClass {原创 2021-12-10 00:00:17 · 263 阅读 · 0 评论 -
设计模式之命令模式(Command模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Command command = new ConcreteCommand(new Receiver()); Invoker invoker = new Invoker(command); invoker.call(); }}Command:public i原创 2021-12-09 23:53:07 · 562 阅读 · 0 评论 -
设计模式之责任链模式(Chain of Responsibility模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Handler handlerA = new ConcreteHandlerA(); Handler handlerB = new ConcreteHandlerB(); handlerA.setNextHandler(handlerB); handlerA.r原创 2021-12-09 00:25:28 · 191 阅读 · 0 评论 -
设计模式之迭代器模式(Iterator模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Aggregate aggregate = new ConcreteAggregate(); Iterator iterator = aggregate.iterator(); }}Iterator:public interface Iterator { boole原创 2021-12-09 00:12:14 · 359 阅读 · 0 评论 -
设计模式之观察者模式(Observer模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Subject subject = new ConcreteSubject(); Observer observer = new ConcreteObserver(); subject.add(observer); subject.notifyObservers原创 2021-12-08 23:56:52 · 202 阅读 · 0 评论 -
设计模式之享元模式(Flyweight模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { FlyweightFactory factory = new FlyweightFactory(); Flyweight flyweight = factory.getFlyweight("name"); }}FlyweightFactory:public class Fl原创 2021-12-07 23:16:19 · 193 阅读 · 0 评论 -
设计模式之组合模式(Composite模式)
一、UML类图1. 安全方式:2. 透明方式:二、代码示例1. 安全方式:Client:public class Client { public static void main(String[] args) { Composite composite = new Composite(); Composite subComposite = new Composite(); Leaf leaf = new Leaf();原创 2021-12-07 23:05:53 · 280 阅读 · 0 评论 -
设计模式之代理模式(Proxy模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { RealSubject realSubject = new RealSubject(); Proxy proxy = new Proxy(realSubject); proxy.operation(); }}Proxy:public class Proxy原创 2021-12-07 22:42:29 · 117 阅读 · 0 评论 -
设计模式之装饰模式(Decorator模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Component component = new ConcreteComponent(); component = new ConcreteDecorator(component); component.operation(); }}Component:p原创 2021-12-07 22:31:27 · 252 阅读 · 0 评论 -
设计模式之桥接模式(Bridge模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Implementor implementor = new ConcreteImplementor(); Abstraction abstraction = new RefinedAbstraction(); abstraction.setImplementor(impleme原创 2021-12-07 00:26:29 · 263 阅读 · 0 评论 -
设计模式之外观模式(Facade模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Facade facade = new Facade(); facade.method(); }}Facade:public class Facade { private SubsystemClassA classA = new SubsystemClassA();原创 2021-12-06 23:32:11 · 94 阅读 · 0 评论 -
设计模式之适配器模式(Adapter模式)
一、UML类图1. 类适配器2. 对象适配器二、代码示例1. 类适配器Client:public class Client { public static void main(String[] args) { Target target = new Adapter(); target.request(); }}Target:public interface Target { void request();}Ada原创 2021-12-06 23:08:04 · 142 阅读 · 0 评论 -
设计模式之单例模式(Singleton模式)
一、UML类图二、代码实例1、饿汉式Client:public class Client { public static void main(String[] args) { Singleton singleton = Singleton.getInstance(); }}Singleton:public class Singleton { private static Singleton mInstance = new Singleton(原创 2021-12-05 18:39:34 · 130 阅读 · 0 评论 -
设计模式之原型模式(Prototype模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) throws CloneNotSupportedException { Prototype prototype1 = new ConcretePrototype(); Prototype prototype2 = prototype1.createClone(); }}Protot原创 2021-12-05 22:26:49 · 198 阅读 · 0 评论 -
设计模式之建造者模式(Builder模式)
一、UML类图二、代码示例Client:public class Client { public static void main(String[] args) { Builder builder = new ConcreteBuilder(); Director director = new Director(builder); director.construct(); Product product = builder原创 2021-12-05 22:52:29 · 299 阅读 · 0 评论 -
设计模式之抽象工厂模式(Abstract Factory模式)
一、UML类图二、代码示例Client:public class Client { AbstractFactory factory = new ConcreteFactory(); ProductA productA = factory.createProductA(); ProductB productB = factory.createProductB();}ProductA:public interface ProductA {}ProductB原创 2021-12-06 00:07:38 · 317 阅读 · 0 评论 -
设计模式之工厂方法模式(Factory Method模式)
一、UML类图二、代码示例Client:public class Client { Factory factory = new ConcreteFactory(); Product product = factory.createProduct();}Product:public interface Product {}ConcreteProduct:public class ConcreteProduct implements Product{}原创 2021-12-05 23:44:56 · 194 阅读 · 0 评论