The design pattern and development practice of "reading" JavaScript

Source: Internet
Author: User

2016.08.30

the JavaScript Design patterns and development practices, the people's post and telecommunications press , 5 months , version 1

P13

Find the parts of the change and encapsulate it to make it easy to replace, and the rest is the unchanging part.

P49

function currying (currying) is to collect parameters several times and then pass them as an array to the handler function and One time execution.

the implication is that preprocessing --putting the pre-processing process into a function is clearer and more manageable.

P57

Lazy Load function

The reference to the external variable referencing the function is overridden inside the function, so that the variable points to the new correct function after the first call to the variable.

P84

the errormsg here should not be included in the strategies item. These items as long as return to True/false is good,msg shut them what matter!

Maybe it's msg to change according to Strategie .

P86

Policy Mode The goal is to separate the algorithm from the execution part of the Code (content) , making the calculated code reusable and flexible.

The key point is to see what is the minimum dependency description of the strategy, and how to enter the algorithm from the content .

P89

about the Proxy Mode , the so-called agent is the agent between the visitor-the one who is the visitor. Some interfaces are provided by the visitor for any person to access. These interfaces should not be called in some cases or should be called in another form, in order to ensure that the responsibilities within the interface, the interface should be called every other layer, this layer is used to handle those specific situations, decide how to invoke the interface, this layer is the proxy.

The purpose of the proxy mode is to maintain the responsibility of the interface as a single.

The main feature of proxy mode is that the agent and the agent ( that is, the visitor ) have a consistent interface, the general interface name is also the same.

Because there are a lot of situations to deal with, there are various modes of proxy:

Protection agent: situations where the interface has different access rights

Virtual Agent: The purpose is to delay access to the interface, and then to access it if necessary

Cache Proxy: stores the interface result, the parameters are consistent when the interface is not called directly to return the saved results.

Wait a minute. It is not that the use of virtual agents can not use the protection agent, the above list only shows that these scenarios require a proxy.

P93

single principle of responsibility refers to: just one class ( also includes objects and functions ) , there should be only one cause for it to change. If a class takes on a number of responsibilities, it means that the class will become huge and there may be multiple reasons for it to change.

Responsibilities is defined as "the cause of the change ". When a class assumes multiple responsibilities, due to its high coupling, the handling of one responsibility may affect the handling of another.

The agent layer can take part of the responsibility and make the interface function single.

P124

The problem with the push model of the publish - Subscribe model is that the Data The Subscriber receives is determined by the publisher.

and Pull Model the problem is that the publisher may become a public object.

First, choose the pull model.

1. publish an object that has a series of interfaces for subscribers to obtain data. You may need to use proxy mode.

2. a number of functions are passed in at the time of subscription, and the functions are called on the result and return the result of the call (too complex).

The policy model is also used in the publish-subscribe mode.

P131

Command mode the meaning is to divide the responsibility into three parts: the command - command - performer . Another more important point is that commands become data that can be stored and invoked.

p132

the point is that the demo here, the command Movecommand is bound to the performer animate , which means that movecommand cannot be used for command animate2. If there are multiple performers, multiple corresponding commands must be generated.

can actually achieve into movecommand.execute (animate2).

but think carefully if the executor is more than animate2 and xxx2 ? Multi-Performer is this the case?

This may be achieved by:

var movecommand = function (receiver, POS) {

......

This.receivers = [receiver]

}

MoveCommand.prototype.execute = function () {

if (!arguments.length) {

This._execute.apply (this, this.receivers)

} else {

This._execute.apply (this, arguments)

}

}

Movecommand.prototype._execute = function (receiver) {

Receiver.start ...

}

It is also beneficial to include the performer in the command, which stores the command to store the performer.

p132

Combination Mode The key is interface consistency.

p151

Template Method Mode Consists of two parts of the structure, the first part is the abstract parent class, the second part is the specific implementation subclass. The algorithm framework for subclasses is typically encapsulated in an abstract parent class. , including implementation of some public methods and the order in which all methods in the wrapper subclass are executed. Subclasses through this abstract class, also inherit the entire algorithm structure, and can choose to override the parent class method.

p160

Hook Method Why must it be a method?

Because the results can be dynamically obtained, it is only a matter of how to make the external can change to the hook.

    1. Accessed through this . That is the realization here.

2. expose the hooks as closures.

p173

If there are more than one file,uploaddatabase will be stored in the object. It's just not a upload object.

so I think enjoy meta mode the meaning is to separate the data from the template, thus sharing a structure, some methods.

When you need to use an object, fill in the template with the data to get the object of the specified structure and the available methods.

and here so-called " how many internal states, there are many shared objects ", the internal state refers to the template, the shared object refers to the corresponding data of the template (the code here is Flyweightobj, Instead of the Upload constructor. the Upload constructor, in the final analysis , has nothing to do with the meta-schema, but an abstract class of " Meta " , an abstraction of the internal state. 12.6.1 There are examples).

because the data is separated, in the implementation of this book, the data needs a UploadManager to manage and provide a uploadmanager.setexternalstate for the methods in the template to read the data to populate the specified object ( is the template itself ).

about this implementation, I think here flyweightobj.delfile (ID) and uploadmanager.setexternalstate (ID, this) At the same time, it is not good to call each other feel bad. (There should be no reciprocal references)

is not changed to Flyweightobj.delfile (ID); This.setexternalstate (ID, flyweightobj); would it be better?

It seems that he has done nothing wrong, in the flyweightobj.delfile data is first filled to itself and then delete data, is a reasonable way. After all , delfile may be called multiple times, you can't always write a sentence before each call setexternalstate it?

If you do not need to have a " meta ", then there is no need to use the enjoy meta mode.

p185

Responsibility Chain Model The biggest advantage is decoupling the request sender and N The complex relationship between the recipients, because you do not know which node in the chain can handle the request you made, so you simply pass the request to the first node.

The problem with this book's responsibility chain is that programmers don't know the exact shape of the chain. So a-b-d, I'm going to put a C behind b and I have to know the references to b and D , and modify their nextnode properties.

can var Chainctrl = new Chainctrl (); var A = Chainctrl.cteateandappendnode (FnA), B = ...; var C = Chainctrl.createnode (FnC); Chainctrl.insertafter (C, B);

the above implementation, not only to maintain a presence Chainctrl The internal array, you still need to implement the node.nextnode Property so that you can call b.next () to skip a from B start to execute the responsibility chain.

p187

mentioned here AOP ( plane-oriented programming ) .

the problem with AOP is that you want to insert code before or after the function executes, and you don't want to change the original function. ( If you want to insert code somewhere in the execution of a function, I'm afraid you can only use hooks )

the key to AOP is to call the original function with a function and use it instead of the original function.

by the way, LISP Naturally implements AOP.

p189

Broker Mode solve the problem that there are too many links between code blocks and strong coupling. ( like tearing down a capillary near the heart, even a small change must be cautious )

p195

here in the construction player teamcolor I think it's wrong. team player playerfactory Span style= "Font-family:calibri" >setteam (player) player set properties. Otherwise the demand will be changed to " 2 "

    1. in the The Player constructor adds judgment logic. Of course not.
    2. The Player constructor does not directly add judgment logic but uses hooks. I think this is Dora, after all, to invoke the external interface associated with the team.
    3. Use AOP, replacing the Player. It seems inappropriate to replace the constructor.

or remove it from the The teamcolor property is set better in Player. At this point you can:

1. use hooks in playerfactory.

2. Call setteam (player) in the Playerfactory function

p195

here Player.prototype.die and other methods called the playerdirector.receivemessage, which relies on the mediator mode Playerdirector, in the future if you want to change a mode is bound to change the code here. It is recommended to have a separate floor.

To say so, all calls to the external interface should be separated from one another.

p198

There is a problem with the handling of the drop line, if the first die and then remove, because the remove is not checked, the other side can not win.

you can consider using Aop

p199

when it comes to buying goods, can you create a " process model "? Put the process into the array, call next () and go to the next process, and call jump (FN) jumps to the process of FN.

There is also the idea that you can use MVC here . On the page, and then into the mediator, then the mediator processes a data structure, and the mediator invokes a render function to render the page based on the data structure.

The question is how to render only what needs to be rendered.

p206

the changed here are too bloated and have a high degree of coupling, and the if-else should be split into a function in an obj and then Changed iterates through The members of obj and joins them in a chain of responsibility pattern.

the remainder may also be split into functions and used AOP connections.

p208

Excessive abstraction makes no shape and is not intuitive.

p211

Decorator Mode the intent is to reduce the number of subclasses and instances.

Calling the same name sounds a bit like proxy mode.

The difference between the two in the logical business, the agent's business is related to the business of the agent, the decorator and the decorator's business can be irrelevant.

in terms of implementation, a surrogate is an object that has a method with the same name as the agent ( or function ) ; The decorator is in the class A Another class is called in the prototype method B creates an instance of the method, and finally calls the new A () get an instance, or use the AOP replace the original method.

If the decorator is a singleton, the decorator pattern is changed by the decorator, and the proxy mode can not change it.

The design pattern and development practice of "reading" JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.