Write the code writing for several years, only to find themselves every day with design patterns!

The default file 1583048739345.jpg

Original Statement

Author: Huang oblique

Reprint please indicate the source and author at the beginning of the article.

Series of articles

This series of articles mainly focus on programmers, especially in Java or back-end programmers must master some of the techniques and skills, these articles are combined with my personal experience of learning programming, review and settle down methodology. Author Ali is currently doing Java, sneak in to share some technical articles and I hope that more people learn more easily programmed.

Series of articles will be some technical learning process, learning the essentials and my combined experience, more easy to understand, and I will put the information I used when learning, books and articles out to share with you, to save you and me time. The so-called Give a man a fish, but also giving the fishing, is the goal of this series of articles trying to achieve.

A programmer loves to share, a slash of young love life. Share programmer learning programming dry goods and personal growth experience, look forward to your attention, so that we progress together!

This article Mind Mapping

Here Insert Picture Description

What is a design pattern

Learning programming friends, presumably for design pattern is no stranger to this word, you must have at least heard of, if you are doing Java, then there is a greater need to understand design patterns, why do you say so, because Java as a face object language, much of the code can be simplified by designing patterns, specifications, and to enhance the readability of the coding efficiency.

According to Baidu Encyclopedia saying, software design patterns (Design pattern), also known as design patterns, is a set of repeated use, known to most people, after cataloging, code design experience summary. Use design patterns to reusable code, make the code easier to understand others, to ensure the reusability of code reliability program.

In other words, the code reusability is the core essence of design patterns.

Usually we have heard, what does design patterns, such as the factory pattern, singleton, proxy mode, observer mode, etc., these design patterns not only to frequently asked during the interview, but also in the Java ecosystem has also been widely used , where there are many such single JDK embodiment, the factory mode application, spring framework basically these have used the design model, and that Tomcat web application container, is the culmination of design patterns, application to an observer model particularly high.

Why learn design patterns

那么,我们学习设计模式有什么用呢,一来,是帮助你更好地在日常开发中使用到设计模式,二来,想要理解JDK、spring以及Tomcat的实现原理和源码,你就必须要掌握相关的设计模式,否则你连代码都看不懂,又哪里谈得上开发呢。

这一点我体会很深,在大公司里,很多核心系统的代码都写得非常的高端大气上档次,对于设计模式的使用可以说是用到了极致,像是模板方法、策略模式、工厂模式等等适用于大型应用开发的一些设计模式,都会得到广泛的应用。先看懂代码,再进行开发,这肯定是程序员的自我修养之一。

学习设计模式,短期利于面试,长期则可以应用于工作,看来对设计模式的学习,已经是刻不容缓了。

新手上路

对于新手来说,设计模式完全是陌生的事物,一般常见的20多个设计模式,能够记住它的名字和用法都已经很困难了,更不用说自己能不能懂得如何去使用了。

我刚学设计模式的时候,就是抱着一本书啃半天,看完了所有的设计模式,但是过几天就忘得差不多了,面试的时候问我工厂模式,观察者模式,还是支支吾吾半天答不上来,说白了就是没有理解。

比如这个工厂模式,就分为简单工厂,工厂模式,以及抽象工厂模式,每个模式的用法都不太一样,当时就困扰了我很长一段时间。

如果你理解了它的内涵之后,就可以大概知道,简单工厂就是用来生成单一实例的,而工厂模式是可以根据输入输出不同的实例,抽象工厂则是根据不同的工厂生成不同产品的实例。

我们学习设计模式的时候,往往书本上给的栗子和demo都比较不切合实际,有的例子是动物,有的例子是食物,这个时候,如果我们自己去写一写,替换成工作中的一些场景,或者是自己熟悉的场景,比如说蔡徐坤,奥利给等等,相信更有助于你的理解和记忆。

花一些时间,跟着《head first 设计模式》这本书,把书上的设计模式案例都实现一下,能够跑得起来,要比你一遍一遍地看书来得靠谱多了。

学习源码

很多朋友看到“源码”两个字就望而却步,就好像让你玩游戏你一百个答应,让你拆开机箱看看哪块电路板因为玩游戏而严重发热,你就完全提不起兴趣。

学习设计模式,除了实战以外,最好的方式就是去看一些源码,比如JDK的源码,Spring的源码,甚至是Tomcat的源码。

如果你自己啃不动,那也可以跟着一些书籍和博客去啃,网上随便一搜就是一大把,spring里的xx设计模式、Tomcat里常见的n种设计模式,JDK里的20种设计模式等等,可能你平时都没有注意,一看自己平时用的API里居然有这么多设计模式,你就会觉得很有意思了。

举个栗子,JDK里的IO流,就使用了装饰者模式,比如对于一个IO输出流,它可能是字节流,也可能是字符流,它还可以是带缓冲的输出流,而这些特性都是通过装饰者模式实现的,IO流的实例可以不断的被转化成另一种流,只需要通过 “(装饰物)IO流”这种写法就可以不断地进行包装,就好像你买了一杯奶茶,可以往里面加椰果,加波霸,加奶加糖一样。

spring里最常用的几个设计模式就是单例模式,代理模式。大家都知道spring的IOC和AOP,spring本身提供一个bean容器,而每个bean其实都是单例的(同一个堆里只有一个实例)这其实就是用了单例模式来实现的。

那么,AOP用的是什么设计模式呢,其实就是代理模式,AOP是通过动态代理来实现的,首先,AOP是作用于某些方法或者是某些类的,你可以把这些方法或者类当成一个切面,也就是被代理的对象,而我们希望在这个切面上添加的功能,就是代理对象,比如统一的登录管理,请求拦截,安全检查等功能。如果你了解过动态代理,应该就会理解我的这个描述。

而对于Tomcat来说,设计模式就更多了,我们这里只讲一个观察者模式,Tomcat的启动是有一个生命周期(pipeline)的,你可以把这个生命周期当成一系列要执行的方法,而Tomcat的实现允许你监听这些方法的调用,你可以在pipeline上面注册自己的监听器,每当pipeline执行到你监听的方法时,它们就会通知你,然后你去执行相应的动作。

不得不说,设计模式在Java生态中的应用实在是太多了,当然,理解设计模式这件事于是需要你花一定时间的。

结合工作

Understand abstract things are often challenging. Study design patterns, we usually read a book or watch tutorials, generally there will be an introduction to a model, and the corresponding code, since there is code to achieve, it can not be regarded as something abstract, but these tend to sample code and we development work to achieve a far cry, so, when we learn design patterns also tend to learn to forget, but can not be applied to development work.

At work, there is a mysterious skill, you can make your code capabilities by leaps and bounds, so that the quality of your code specifications, and can greatly enhance the degree of reuse, this mysterious skill, in fact, CV, yes, that is copy and paste .

what? Not that you copy the code, I will do my ah. No, no matter how programmers can call copy it, and what to learn! reference! Multiplexing!

However, in a big company, CV shameful but although it is useful, after all, a large factory Daniel multi-code written by good people more, each team has always such immortal codes, we should follow the example of learning, I often see some code, several years ago, Daniel wrote, was been passed to the present, and sometimes even do remodeling or doing migration, have to put these big cattle continue to move over the core code, showing that this great influence like high-quality code.

We first whether these cattle code is how to write, but these high-quality model is indeed worthy of our study, for example, I developed a system and found inside the core business of the code is a template method + comment Disposition way development, so read through, very much appreciated, I feel I must write to, then this code style you can learn another system.

For example, recently I was responsible for the full development of another system, I can define specifications and style, so of course I want to show its mettle, so that the quality of code templates and design were all brought me a reference, after all, in large companies try not to repeat create the wheel, learn more learning and more must be no harm, then wait until you have mastered the core of these things, go and create their own set of standards and style are also not a bad idea.

In fact, not only the design mode, there are a lot of things so you can learn through imitation, such as analysis of architecture design, systems, technology stack selection, etc., so-called read a hundred times its meaning from the see, familiar with the Tang three hundred, poetry will not Yin, stress is a "practice makes perfect" it.

Recommended Resources

books

"Head first design patterns"
"Westward Design Patterns"

Recommended Resources

books

"Head first design patterns"
"Westward Design Patterns"

Guess you like

Origin blog.51cto.com/14006572/2475834