没有合适的资源?快使用搜索试试~ 我知道了~
Essential JavaScript Design Patterns
需积分: 10 7 下载量 192 浏览量
2012-04-10
22:08:03
上传
评论
收藏 582KB PDF 举报
温馨提示
试读
43页
Design patterns are reusable solutions to commonly occurring problems in software design. They are both exciting and a fascinating topic to explore in any programming language.
资源推荐
资源详情
资源评论
Essential JavaScript Design Patterns For
Beginners
Authored By Addy Osmani
Copyright 2011 © Addy Osmani. Last edited: May 23rd 2011.
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 unported license. You
are free to remix, tweak, and build upon this work non-commercially, as long as you
credit Addy Osmani (the copyright holder) and license your new creations under the
identical terms. Any of the above conditions can be waived if you get permission from
the copyright holder. For any reuse or distribution, you must make clear to others the
license terms of this work. The best way to do this is with a link to the license.
Foreword
I would like to thank Rebecca Murphey for inspiring me to open-source this mini-book
and release it for free download and distribution - making knowledge both open and
easily available is something we should all strive for where possible. I would also like to
extend my thanks to the very talented Alex Sexton who was kind enough to be the
technical reviewer for this publication. I hope that it helps you learn more about design
patterns and the usefulness of their application to JavaScript code.
Introduction
At the beginning of this book I will be focusing on a discussion about the importance
and history of design patterns in any programming language. If you're already sold on
or are familiar with this history, feel free to skip to the chapter 'What is a Pattern?' to
continue reading.
One of the most important aspects of writing maintainable code is being able to notice
the recurring themes in that code and optimize them. This is an area where knowledge
Essential JavaScript Design Patterns For Beginners http://addyosmani.com/resources/essentialjsdesignpatter...
1 of 43 2011/7/15 12:55
of design patterns can prove invaluable.
Design patterns can be traced back to the early work of a civil engineer named
Christopher Alexander. He would often write publications about his experience in
solving design issues and how they related to buildings and towns. One day, it
occurred to Alexander that when used time and time again, certain design constructs
lead to a desired optimal effect.
In collaboration with Sarah Ishikawra and Murray Silverstein, Alexander produced a
pattern language that would help empower anyone wishing to design and build at any
scale. This was published back in 1977 in a paper titled 'A Pattern Language'.
Some 30 years ago, software engineers began to incorporate the principles Alexander
had written about into the first documentation about design patterns, which was to be
a guide for novice developers looking to improve their coding skills. It's important to
note that the concepts behind design patterns have actually been around in the
programming industry more than likely since its inception, albeit in a less formalized
form.
One of the first and arguably most iconic formal works published on design patterns in
software engineering was a book in 1995 called
'Design Patterns: Elements Of Reusable
Object-Oriented Software
'. This was written by Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides - a group that became known as the Gang of Four (or GoF
for short).
The GoF's publication is considered quite instrumental to pushing the concept of
design patterns further in our field as it describes a number of development
techniques and pitfalls as well as providing twenty-three core Object-Oriented design
patterns frequently used around the world today. We will be covering these patterns in
more detail in the section ‘Categories of Design Patterns’.
In this book, we will take a look at a number of popular JavaScript design patterns and
explore why certain patterns may be more suitable for your projects than others.
Remember that patterns can be applied not just to vanilla JavaScript, but also to
abstracted libraries such as jQuery or Dojo as well. Before we begin, let’s look at the
exact definition of a ‘pattern’ in software design.
Contents
What is a Pattern?
'Pattern'-ity Testing, Proto-Patterns & The Rule Of Three
The Structure Of A Design Pattern
Writing Design Patterns
Anti-Patterns
Essential JavaScript Design Patterns For Beginners http://addyosmani.com/resources/essentialjsdesignpatter...
2 of 43 2011/7/15 12:55
Categories Of Design Pattern
Design Patterns in JavaScript
Creational Pattern
Constructor Pattern
Singleton Pattern
Module Pattern
Revealing Module Pattern
Observer Pattern
Prototype Pattern
Command Pattern
DRY Pattern
Facade Pattern
Factory Pattern
Mixin Pattern
Decorator Pattern
A High-level View Of Design Patterns in jQuery
Lazy Initialisation
Composite Pattern
Wrapper Pattern
Facade Pattern
Observer Pattern
Iterator Pattern
Strategy Pattern
Proxy Pattern
Builder Pattern
Prototype Pattern
Flyweight Pattern
Conclusions
References
What is a Pattern?
A pattern is a reusable solution that can be applied to commonly occurring problems
in software design - in our case - in writing JavaScript applications. Another way of
looking at patterns are as templates for how you solve problems - ones which can be
used in quite a few different situations.
To consider how useful a pattern may be, let us consider that if you were to write a
script where you said ‘for each item, sound an alert’, if sounding an alert was
complex in nature, it would always result in more maintainable code doing the above
over saying ‘do this for item 1’, ‘do this for item 2’, ‘do the same again for item
3’, i.e. If the code performing the bulk of the work exists in fewer places it becomes
Essential JavaScript Design Patterns For Beginners http://addyosmani.com/resources/essentialjsdesignpatter...
3 of 43 2011/7/15 12:55
significantly easier to maintain.
You may ask why it’s important to understand patterns and be familiar with them.
Design patterns have three main benefits:
Patterns are proven solutions: They provide solid approaches to solving
issues in software development using proven solutions that reflect the
experience and insights the developers that helped define and improve them
bring to the pattern.
1.
Patterns can be easily re-used: A pattern usually reflects an out of the box
solution that can be adapted to suit your own needs. This feature makes them
quite robust.
2.
Patterns can be expressive: When you look at a pattern there’s generally a
set structure and ‘vocabulary’ to the solution presented that can help
express rather large solutions quite elegantly.
3.
Patterns are not an exact solution. It’s important that we remember the role of a
pattern is merely to provide us with a solution scheme. Patterns don’t solve all design
problems nor do they replace good software designers, however, they do support
them. Next we’ll take a look at some of the other advantages patterns can offer us.
Reusing patterns assists in preventing minor issues that can cause major problems
in the application development process. What this means is that when your code
relies more on proven patterns, you can afford to spend less time worrying about your
code architecture and more time focusing on the quality of your overall solution. This
is because patterns can encourage you to code in a more structured and organized
fashion so the need to refactor it for cleanliness purposes in the future can be
significantly decreased.
Patterns can provide generalized solutions which are documented in a fashion that
doesn't require them to be tied to a specific problem. This generalized approach
means that regardless of the application (and in many cases the programming
language) you are working with, design patterns can be applied to improve the
structure of your code.
Certain patterns can actually decrease the overall file-size footprint of your code
by avoiding repetition. By encouraging developers to look more closely at their
solutions for areas where instant reductions in repetition can be made, e.g. reducing
the number of functions performing similar processes in favor of a single generalized
function, the overall size of your codebase can be decreased.
Patterns that are frequently used can be improved over time by harnessing the
collective experiences other developers using those patterns contribute back to the
design pattern community. In some cases this leads to the creation of entirely new
design patterns whilst in others it can lead to the provision of improved guidelines on
how specific patterns can be best used. This can ensure that pattern-based solutions
continue to become more robust than ad-hoc solutions may be.
Essential JavaScript Design Patterns For Beginners http://addyosmani.com/resources/essentialjsdesignpatter...
4 of 43 2011/7/15 12:55
'Pattern'-ity Testing, Proto-Patterns &
The Rule Of Three
Remember that not every algorithm, best practice or solution represents what might
be considered a complete pattern. There may be a few key ingredients here that are
missing and the pattern community is generally weary of something claiming to be
one unless it has been heavily vetted. Even if something is presented to us which
*appears* to meet the criteria for a pattern, it should not be considered one until it has
undergone suitable periods of scrutiny and testing by others.
Looking back upon the work by Alexander once more, he claims that a pattern should
both be a process and a ‘thing’. This definition is obtuse on purpose as he follows
by saying that the process should create the ‘thing’. This is a reason why patterns
generally focus on addressing a visually identifiable structure i.e you should be able to
visually depict (or draw) a picture representing the structure that placing the pattern
into practice results in.
In studying design patterns, you may come across the term ‘proto-pattern’ quite
frequently. What is this? Well, a pattern that has not yet been known to pass the
‘pattern’-ity tests is usually referred to as a proto-pattern. Proto-patterns may result
from the work of someone that has established a particular solution that is worthy of
sharing with the community, but may not have yet had the opportunity to have been
vetted heavily due to it’s very young age.
Alternatively, the individual(s) sharing the pattern may not have the time or interest of
going through the ‘pattern’-ity process and might release a short description of
their proto-pattern instead. Brief descriptions of this type of pattern are known as
patlets.
The work involved in fully documenting a qualified pattern can be quite daunting.
Looking back at some of the earliest work in the field of design patterns, a pattern may
be considered ‘good’ if it does the following:
Solves a particular problem - patterns are not supposed to just capture principles or
strategies. They need to capture solutions. This is one of the most essential ingredients
for a good pattern.
The solution to this problem cannot be obvious - you can often find that problem-
solving techniques attempt to derive from well-known first principles. The best design
patterns usually provide solutions to problems indirectly - this is considered a
necessary approach for the most challenging problems related to design.
Essential JavaScript Design Patterns For Beginners http://addyosmani.com/resources/essentialjsdesignpatter...
5 of 43 2011/7/15 12:55
剩余42页未读,继续阅读
资源评论
魔豆爸
- 粉丝: 89
- 资源: 106
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功