Download Complete Learning Java Script Design Patterns 2nd Edition Addi Osmani PDF for All Chapters
Download Complete Learning Java Script Design Patterns 2nd Edition Addi Osmani PDF for All Chapters
com
https://ebookmeta.com/product/learning-java-script-design-
patterns-2nd-edition-addi-osmani/
OR CLICK HERE
DOWLOAD NOW
With Early Release ebooks, you get books in their earliest form—the author’s
raw and unedited content as they write—so you can take advantage of these
technologies long before the official release of these titles.
Addy Osmani
Learning JavaScript Design Patterns
by Addy Osmani
Copyright © 2023 Adnan Osmani. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
95472.
O’Reilly books may be purchased for educational, business, or sales promotional
use. Online editions are also available for most titles (http://oreilly.com). For more
information, contact our corporate/institutional sales department: 800-998-9938 or
[email protected].
Good code is like a love letter to the next developer who will maintain it!
Design patterns provide a common vocabulary to structure code, making it easier to
understand. They help enhance the quality of this connection to other developers.
Knowledge of design patterns helps us identify recurring themes in requirements
and map them to definitive solutions. We can rely on the experience of others who
have encountered a similar problem and devised an optimized method to address it.
This knowledge is invaluable as it paves the way for writing or refactoring code to
make it maintainable.
Whether on the server or client, JavaScript is a cornerstone of modern web
application development. The previous edition of this book focused on several
popular design patterns in the JavaScript context. Over the years, JavaScript has
significantly evolved as a language in terms of features and syntax. It now supports
modules, classes, arrow functions, and template literals that were not there earlier.
We also have advanced JavaScript libraries and frameworks that have made life easy
for many web developers. How relevant, then, are Design Patterns in the modern
JavaScript context?
It’s important to note that traditionally design patterns are neither prescriptive nor
language specific. You can apply them when you think they fit, but you don’t have
to. Like data structures or algorithms, you can still apply classic design patterns
using modern programming languages, including JavaScript. You may not need
some of these design patterns in modern frameworks or libraries where they are
already abstracted. Conversely, the use of specific patterns may even be encouraged
by some frameworks.
In this edition, we are taking a pragmatic approach to patterns. We will explore why
specific patterns may be the right fit for implementing certain features and if a
pattern it is still recommended in the modern JavaScript context.
As applications got more interactive, requiring a large amount of JavaScript, the
language came under constant criticism for its negative impact on performance.
Developers are continuously looking for new patterns that can optimize JavaScript
performance. This edition highlights such improvements wherever relevant. We will
also discuss framework-specific patterns such as React Hooks and Higher Order
Components that have become increasingly popular in the age of React.js.
Going back a step, let us start by exploring the history and importance of design
patterns. If you are already familiar with this history, feel free to skip to “What Is a
Pattern?” to continue reading.
What Is a Pattern?
A pattern is a reusable solution template that you can apply to recurring problems
and themes in software design. Similar to other programming languages, when
building a JavaScript web application, you can use the template to structure your
JavaScript code in different situations where you think it will help.
Learning and using Design patterns is mainly advantageous for developers because:
Patterns are proven solutions.
They are the result of the combined experience and insights of developers
who helped define them. They are time-tested approaches known to work
when solving specific issues in software development.
Some patterns can decrease the overall code file-size footprint by avoiding
repetition.
Design patterns encourage developers to look more closely at their
solutions for areas where they can achieve instant reductions in
duplication. For example, you can reduce the number of functions
performing similar processes in favor of a single generalized function to
decrease the size of your codebase. This is also known as making code
more dry.
Patterns add to a developer’s vocabulary, which makes communication faster.
Developers can reference the pattern when communicating with their
team, discussing it in the design patterns community, or indirectly when
another developer later maintains the code.
NOTE
Patterns are not exact solutions. The role of a pattern is merely to provide us with a solution
scheme. Patterns don’t solve all design problems nor replace good software designers. You still need
sound designers to choose the correct patterns that can enhance the overall design.
From the moment a new pattern is proposed to its potential widespread adoption, a
pattern may have to go through multiple rounds of deep inspection by the design
community and software developers. This chapter talks about this journey of a
newly introduced “Proto-Pattern” through a “pattern”-ity test till it is eventually
recognized as a pattern if it meets the Rule of Three.
This and the next chapter explore the approach to structuring, writing, presenting,
and reviewing nascent design patterns. If you’d prefer to learn established design
patterns first, you can skip these two chapters for the time being.
Describes a relationship.
In some cases, it may appear that a pattern describes a type of module.
Despite what the implementation looks like, the official description of the
pattern must describe much deeper system structures and mechanisms
that explain its relationship to code.
We would be forgiven for thinking that a proto-pattern that fails to meet guidelines
isn’t worth learning from; however, this is far from the truth. Many proto-patterns
are actually quite good. I am not saying that all proto-patterns are worth looking at,
but there are quite a few useful ones in the wild that could assist us with future
projects. Use your best judgment with the above list in mind, and you’ll be fine in
your selection process.
Rule of Three
One of the additional requirements for a pattern to be valid is that they display
some recurring phenomenon. You can often qualify this in at least three key areas,
referred to as the rule of three. To show recurrence using this rule, one must
demonstrate:
Fitness of purpose
How is the pattern considered successful?
Usefulness
Why is the pattern considered successful?
Applicability
Is the design worthy of being a pattern because it has broader
applicability? If so, this needs to be explained. When reviewing or defining
a pattern, it is vital to keep the above in mind.
Summary
This chapter has shown how every proposed proto-pattern may not always be
accepted as a pattern. The next chapter shares the essential elements and best
practices for structuring and documenting patterns so the community can easily
understand and consume them.
Chapter 3. Structuring and Writing
Patterns
The success of a new idea depends on its utility and also on how you present it to
those it is trying to help. For developers to understand and adopt a design pattern,
it should be presented with relevant information about the context, circumstances,
prerequisites, and significant examples. This chapter applies to those trying to
understand a specific pattern and those trying to introduce a new one as it provides
essential information on how patterns are structured and written.
A context
A system of forces that arises in that context
A configuration that allows these forces to resolve themselves in
context
With this in mind, let‘s now summarize the component elements for a design
pattern. A design pattern should have the following, with the first five elements
being the most important:
Pattern name
A unique name representative of the purpose of the pattern
Description
A brief description of what the pattern helps achieve.
Context outline
The contexts in which the pattern effectively responds to its users’ needs.
Problem statement
A statement of the problem addressed so that we understand the pattern’s
intent.
Solution
A description of how the user’s problem is solved in an understandable list
of steps and perceptions.
Design
A description of the pattern’s design and, in particular, the user’s behavior
in interacting with it.
Implementation
A guide to how developers would implement the pattern.
Illustrations
Visual representations of classes in the pattern (e.g., a diagram).
Examples
Implementations of the pattern in a minimal form.
Corequisites
What other patterns may be needed to support the use of the pattern
being described?
Relations
What patterns does this pattern resemble? Does it closely mimic any
others?
Known usage
Is the pattern being used in the wild? If so, where and how?
Discussions
The team or author’s thoughts on the exciting benefits of the pattern.
TIP
Solutions in which neither interactions nor defined rules appear are not patterns.
Although patterns may have a high initial cost in the planning and write-up phases,
the value returned from that investment can be worth it. Patterns are valuable
because they help to get all the developers in an organization or team on the same
page when creating or maintaining solutions. If you are considering working on a
pattern of your own, research beforehand, as you may find it more beneficial to use
or extend existing, proven patterns rather than starting afresh.
Writing a pattern
If you are trying to develop a design pattern yourself, I recommend learning from
others who have already been through the process and done it well. Spend time
absorbing the information from several different design pattern descriptions and
take in what’s meaningful to you. Explore structure and semantics—you can do this
Random documents with unrelated
content Scribd suggests to you:
Dense Stars
The agreement of the observational points with the curve is
remarkably close, considering the rough nature of the observational
measurements; and it seems to afford a rather strong confirmation of
the theory. But there is one awful confession to make—we have
compared the theory with the wrong stars. At least when the
comparison was first made at the beginning of 1924 no one
entertained any doubt that they were the wrong stars.
We must recall that the theory was developed for stars in the
condition of a perfect gas. In the right half of Fig. 7 the stars
represented are all diffuse stars; Capella with a mean density about
equal to that of the air in this room may be taken as typical. Material
of this tenuity is evidently a true gas, and in so far as these stars
agree with the curve the theory is confirmed. But in the left half of the
diagram we have the Sun whose material is denser than water,
Krueger 60 denser than iron, and many other stars of the density
usually associated with solid or liquid matter. What business have
they on the curve reserved for a perfect gas? When these stars were
put into the diagram it was not with any expectation that they would
agree with the curve; in fact, the agreement was most annoying.
Something very different was being sought for. The idea was that the
theory might perhaps be trusted on its own merits with such
confirmation as the diffuse stars had already afforded; then by
measuring how far these dense stars fell below the curve we should
have definite information as to how great a deviation from a perfect
gas occurred at any given density. According to current ideas it was
expected that the sun would fall three or four magnitudes below the
curve, and the still denser Krueger 60 should be nearly ten
magnitudes below.[8] You see that the expectation was entirely
unfulfilled.
The shock was even greater than I can well indicate to you,
because the great drop in brightness when the star is too dense to
behave as a true gas was a fundamental tenet in our conception of
stellar evolution. On the strength of it the stars had been divided into
two groups known as giants and dwarfs, the former being the
gaseous stars and the latter the dense stars.
Two alternatives now lie before us. The first is to assume that
something must have gone wrong with our theory; that the true curve
for gaseous stars is not as we have drawn it, but runs high up on the
left of the diagram so that the Sun, Krueger 60, &c., are at the
appropriate distances below it. In short, our imaginary critic was
right; Nature had hidden something unexpected inside the star and
so frustrated our calculations. Well, if this were so, it would be
something to have found it out by our investigations.
The other alternative is to consider this question—Is it impossible
that a perfect gas should have the density of iron? The answer is
rather surprising. There is no earthly reason why a perfect gas
should not have a density far exceeding iron. Or it would be more
accurate to say, the reason why it should not is earthly and does not
apply to the stars.
The sun’s material, in spite of being denser than water, really is a
perfect gas. It sounds incredible, but it must be so. The feature of a
true gas is that there is plenty of room between the separate
particles—a gas contains very little substance and lots of emptiness.
Consequently when you squeeze it you do not have to squeeze the
substance; you just squeeze out some of the waste space. But if you
go on squeezing, there comes a time when you have squeezed out
all the empty space; the atoms are then jammed in contact and any
further compression means squeezing the substance itself, which is
quite a different proposition. So as you approach that density the
compressibility characteristic of a gas is lost and the matter is no
longer a proper gas. In a liquid the atoms are nearly in contact; that
will give you an idea of the density at which the gas loses its
characteristic compressibility.
The big terrestrial atoms which begin to jam at a density near that
of the liquid state do not exist in the stars. The stellar atoms have
been trimmed down by the breaking off of all their outer electrons.
The lighter atoms are stripped to the bare nucleus—of quite
insignificant size. The heavier atoms retain a few of the closer
electrons, but have not much more than a hundredth of the diameter
of a fully arrayed atom. Consequently we can go on squeezing ever
so much more before these tiny atoms or ions jam in contact. At the
density of water or even of platinum there is still any amount of room
between the trimmed atoms; and waste space remains to be
squeezed out as in a perfect gas.
Our mistake was that in estimating the congestion in the stellar
ball-room we had forgotten that crinolines are no longer in fashion.
It was, I suppose, very blind of us not to have foreseen this result,
considering how much attention we had been paying to the
mutilation of the atoms in other branches of the investigation. By a
roundabout route we have reached a conclusion which is really very
obvious. And so we conclude that the stars on the left of the diagram
are after all not the ‘wrong’ stars. The sun and other dense stars are
on the perfect gas curve because their material is perfect gas.
Careful investigation has shown that in the small stars on the
extreme left of Fig. 7 the electric charges of the atoms and electrons
bring about a slight deviation from the ordinary laws of a gas; it has
been shown by R. H. Fowler that the effect is to make the gas not
imperfect but superperfect—it is more easily compressed than an
ordinary gas. You will notice that on the average the stars run a little
above the curve on the left of Fig. 7. It is probable that the deviation
is genuine and is partly due to superperfection of the gas; we have
already seen that imperfection would have brought them below the
curve.
Even at the density of platinum there is plenty of waste space, so
that in the stars we might go on squeezing stellar matter to a density
transcending anything known on the earth. But that’s another story—
I will tell it later on.
The general agreement between the observed and predicted
brightness of the stars of various masses is the main test of the
correctness of our theories of their internal constitution. The
incidence of their masses in a range which is especially critical for
radiation pressure is also valuable confirmation. It would be an
exaggeration to claim that this limited success is a proof that we
have reached the truth about the stellar interior. It is not a proof, but
it is an encouragement to work farther along the line of thought
which we have been pursuing. The tangle is beginning to loosen.
The more optimistic may assume that it is now straightened out; the
more cautious will make ready for the next knot. The one reason for
thinking that the real truth cannot be so very far away is that in the
interior of a star, if anywhere, the problem of matter is reduced to its
utmost simplicity; and the astronomer is engaged on what is
essentially a less ambitious problem than that of the terrestrial
physicist to whom matter always appears in the guise of electron
systems of the most complex organization.
We have taken the present-day theories of physics and pressed
them to their remotest conclusions. There is no dogmatic intention in
this; it is the best means we have of testing them and revealing their
weaknesses if any.
In ancient days two aviators procured to themselves wings.
Daedalus flew safely through the middle air and was duly honoured
on his landing. Icarus soared upwards to the sun till the wax melted
which bound his wings and his flight ended in fiasco. In weighing
their achievements, there is something to be said for Icarus. The
classical authorities tell us that he was only ‘doing a stunt’, but I
prefer to think of him as the man who brought to light a serious
constructional defect in the flying-machines of his day. So, too, in
Science. Cautious Daedalus will apply his theories where he feels
confident they will safely go; but by his excess of caution their hidden
weaknesses remain undiscovered. Icarus will strain his theories to
the breaking-point till the weak joints gape. For the mere adventure?
Perhaps partly; that is human nature. But if he is destined not yet to
reach the sun and solve finally the riddle of its constitution, we may
at least hope to learn from his journey some hints to build a better
machine.
LECTURE II
SOME RECENT INVESTIGATIONS