![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
设计模式
比不揪
这个作者很懒,什么都没留下…
展开
-
设计模式之单例模式
在编程中,有时候只需要生成一个对象实例,比如你手机里的某个APP,如果已经打开,再点击并不会打开一个新的,而是把已经打开的那个窗口显示出来,这就是单例的精神。 好了,上代码/** * 单例模式 * 生成唯一模式 */public class Main { public static void main(String[] args) { //生成第一个实例 ...原创 2018-08-09 19:13:36 · 105 阅读 · 0 评论 -
设计模式-目录
设计模式-目录 创建型设计模式 1、简单工厂 2、工厂方法 3、抽象工厂方法 4、单例 5、建造者结构型设计模式 6、原型 7、适配器 8、组合 9、外观 10、装饰 11、享元 12、代理行为型设计模式 13、策略 14、命令 15、解释器 16、责任链 17、迭代器 18、备忘录 19、中介者 20、状态 21、观察者 22、模板 23、...原创 2018-08-15 13:12:02 · 192 阅读 · 0 评论 -
设计模式之备忘录模式
/** * 中介者模式 * @param <T> */public static void main(String[] args) { Map<String,String> stateA = new HashMap<String,String>(); stateA.put("a","1&quo原创 2018-08-15 10:24:20 · 108 阅读 · 0 评论 -
设计模式之中介者模式
/** * 中介者模式 */public class Main { public static void main(String[] args) { Mediator mediator = new Mediator(); mediator.notice("ObjectA"); mediator.notice("ObjectB"); ...原创 2018-08-15 10:38:07 · 119 阅读 · 0 评论 -
设计模式之观察者模式
public class Main { public static void main(String[] args) { IntegerObjectObservable integerObjectObservable = new IntegerObjectObservable(); IntegerObjectObserver integerObjectO...原创 2018-08-15 10:51:10 · 104 阅读 · 0 评论 -
设计模式之状态模式
/** * 状态模式 */public class Main { public static void main(String[] args) { IState stateA = new StateA("A"); IState stateB = new StateB("B"); IState stateB1 = new StateB(...原创 2018-08-15 11:08:56 · 118 阅读 · 0 评论 -
设计模式之模板模式
/** * 模板模式 */public class Main { public static void main(String[] args) { Template template = new TemplateContent(); template.printN(10); }}public abstract class Templ...原创 2018-08-15 11:19:18 · 143 阅读 · 0 评论 -
设计模式之访问者模式
import java.util.ArrayList;import java.util.List;/** * 访问者模式 */public class Main { public static void main(String[] args) { Visitor visitor = new ConcreteVisitor(); List resu...原创 2018-08-15 11:27:59 · 138 阅读 · 0 评论 -
设计模式之简单工厂
简单工厂设计模式太简单,直接上代码 “ /** * 简单工厂 * 简单工厂就是利用条件判断,生成需要的实例 */ public class Main {public static void main(String[] args) { ICat blackCat = SimpleFactory.create("BlackCat"); blackCat.eat(...原创 2018-08-08 16:09:09 · 121 阅读 · 0 评论 -
设计模式之工厂方法
还是太简单了,上代码/** * 工厂方法 * 在简单工厂上的基础上对工厂本身进行了封装,与简单工厂的区别就在于工厂是多态指定的 */public class Main { public static void main(String[] args) { IAnimalFactory blackAnimalFactory = new BlackAnimal...原创 2018-08-08 16:20:47 · 103 阅读 · 0 评论 -
设计模式之迭代器模式
/** * 迭代器模式 */public class Main { public static void main(String[] args) { Integer[] a = {1,2,3,4,5}; ArrayIterator<Integer> dataIterator = new ArrayIterator<Integer&g...原创 2018-08-15 10:07:05 · 125 阅读 · 0 评论 -
设计模式之代理模式
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;/** * 代理模式 */public class Main { public static void main(String[] args) { S...原创 2018-08-11 11:28:04 · 134 阅读 · 0 评论 -
设计模式之享元模式
/** * 享元模式 */public class Main { public static void main(String[] args) { ICat cat1 = FlyweighFactory.getFlyweight("cat1"); ICat cat2 = FlyweighFactory.getFlyweight("cat2"); ...原创 2018-08-11 10:22:11 · 119 阅读 · 0 评论 -
设计模式之建造者模式
如果我们创建一个实体类的时候,需要设置数据库环境,设置系统变量环境,设置用户自定义变量等多个步骤,可以在构造函数中去完成这些过程,但是如果把这些步骤全部都塞到一个构造函数中,有些不大好,比如有的子类可能只要其中的几个步骤,这样的话最理想的情况是这些设置步骤可选,怎么做呢,上代码了/** * 建造者设计模式 * 如果一个类的构建需要很多步骤,应该将步骤拆分开进行解耦 */public c...原创 2018-08-09 19:45:46 · 127 阅读 · 0 评论 -
设计模式之原型模式
其实这个设计模式就是我们通常说的对象拷贝,直接看代码吧/** * 原型设计模式 * 把一个有状态的对象复制出多个这种状态的对象 */public class Main { public static void main(String[] args) { Prototype prototype1 = new Prototype(); protot...原创 2018-08-09 19:53:40 · 105 阅读 · 0 评论 -
设计模式之适配器模式
这个设计模式我们平时用的太多了,很多时候用到了都不知道,先看代码/** * 适配器 * 一个服务如果现有的方法不适应新的需求,可以写一个类进行适配,利用现有的服务进行扩展 */public class Main { public static void main(String[] args) { Animal animal = new Animal(); ...原创 2018-08-09 19:57:46 · 85 阅读 · 0 评论 -
设计模式之策略模式
/** * 策略模式 */public class Main{ public static void main(String[] args) { Context ctx = new Context(new StrategyA()); ctx.doMethod(); ctx = new Context(new StrategyB())...原创 2018-08-14 09:05:24 · 113 阅读 · 0 评论 -
设计模式之命令模式
/** * 命令模式 */public class Main { public static void main(String[] args) { CatReceiver catReceiver = new CatReceiver(); RunCommand runCommand = new RunCommand(catReceiver); ...原创 2018-08-14 11:00:12 · 450 阅读 · 0 评论 -
设计模式之解释器模式
/** * 解释器 */public class Main { public static void main(String[] args) { String context = "context"; AdvanceExpression advanceExpression = new AdvanceExpression(); Simpl...原创 2018-08-14 11:28:40 · 246 阅读 · 0 评论 -
设计模式之责任链
/** * 责任链 */public class Main { public static void main(String[] args) { Request request = new Request("hello"); RequestHandleA requestHandleA = new RequestHandleA(); R...原创 2018-08-14 11:35:46 · 109 阅读 · 0 评论 -
设计模式之组合模式
/** * 组合模式 */public class Main { public static void main(String[] args) { Cat catParent = new Cat(&amp;amp;quot;cat0&amp;amp;quot;); Cat catChild1 = new Cat(&amp;amp;quot;cat1&amp;amp;quot;);原创 2018-08-11 09:29:52 · 120 阅读 · 0 评论 -
设计模式之外观模式
/** * 装饰模式 */public class Facade { private IServiceA serviceA; private IServiceB serviceB; private IServiceC serviceC; public Facade(){ serviceA = new ServiceA(); ...原创 2018-08-11 09:39:12 · 100 阅读 · 0 评论 -
设计模式之装饰模式
/** * 装饰模式 */public class Main { public static void main(String[] args) { Cat cat = new Cat(); cat.run(); System.out.println("--------------------"); CatDecorat...原创 2018-08-11 10:12:30 · 161 阅读 · 0 评论 -
设计模式之抽象工厂方法
如果看了前两篇文章,这个设计模式一样很简单,上代码/** * 抽象工厂方法 * 在工厂方法的基础上,每个工厂实例里不止一个接口方法,而是多个 * 这样做体现了产品簇的意义,将相关的产品放在同一个工厂里 */public class Main { public static void main(String[] args) { IAnimalFactor...原创 2018-08-08 16:42:53 · 135 阅读 · 0 评论