1. JavaScript does not provide class inheritance in the traditional object-oriented language, but rather, it implements the inheritance between objects by means of a prototype delegate.
2, static type language at compile time has determined the type of variable, and dynamic type language variable type to the time of the program run, when the variable is given a value, it will have a certain type.
3. JavaScript is a typical dynamic type language
4, using the duck type of thought, I can implement a principle in the dynamic type language: "Interface-oriented programming, rather than implementation-oriented programming"
5, polymorphic: The same operation on different objects, can produce different interpretations and different execution results.
6. The idea behind polymorphism is to separate "what to do" and "who to do and how to do", that is, to separate "unchanging things" from "things That may change".
7. The most fundamental benefit of polymorphism is that you no longer have to ask the object "What type you are" and then invoke an object's behavior based on the resulting answer--you just call that behavior, and all the other polymorphic mechanisms will be arranged for you.
8, from the point of view of design mode, packaging in the more important aspects of packaging change
9. Creation mode, structural mode, behavioral mode
10. JavaScript chooses the prototype-based object-oriented system
11, prototype mode does not care about the specific type of object, but instead finds an object and then creates an identical object by cloning.
12, the implementation of the prototype mode is the key, whether the language itself provides a clone method, ECMAScript5 provides a object.create method, can be used to clone the object
13. All JavaScript objects are cloned from an object.
14. An important feature of prototype programming: When an object cannot respond to a request, it delegates the request to its own prototype
15. Prototype programming generics include at least the following basic rules:
1. All the data are objects
2. To get an object, instead of instantiating the class, find an object as a prototype and clone it
3. The object will remember its prototype
4. If the object cannot respond to a request, it will delegate the request to its own prototype
16. The root object in JavaScript is the Object.prototype object, which is an empty object
17. The process of creating an object with the new operator is actually just a process of cloning the Object.prototype object first and then doing some additional operations
18. For the phrase "the object entrusts the request to its own prototype", the better argument is that the object delegates the request to its constructor's prototype.
19. __proto__ is the link between the object and the prototype of the object constructor, __proto__ to the prototype of the constructor
20, the end of the prototype chain, Object.prototype, that is, null
21, design patterns in many cases actually reflect the language of the shortcomings, if you want to use design patterns, rather than to find a better language.
22, this is specific to which object is dynamically bound at run time based on the execution environment of the function, not the environment when the function is declared
23, borrowing the construction method
24, the use of the Array.prototype.push method needs to meet two conditions:
1. The object itself can access the properties
2. The length property of an object can be read and written
25. The intent of the command pattern is to encapsulate the request as an object, separating the coupling between the initiator of the request and the receiver (performer) of the request. You can pre-populate the Command object with the recipient of the command before the command is executed.
26. The higher order function refers to a function that satisfies at least one of the following conditions:
1. Functions can be passed as parameters
2. function can be output as return value
27, the application of the callback function is not only in the asynchronous request, when a function is not suitable for some requests, we can also encapsulate these requests into a function, and pass it as a parameter to another function, "delegate" to another function to execute.
28. The main function of AOP (aspect-oriented programming) is to pull out some functions unrelated to the core business logic modules, and these functions which are not related to business logic usually include log statistics, security control, exception handling, etc.
29. JavaScript implements AOP, which refers to a function that "dynamically weaves" into another function.
30. Decorator Mode essence idea: The Decorator object and the object it decorates have a consistent interface, so they are clear to the customer using the object, and the object that is decorated does not need to know that it has been decorated, this transparency allows us to recursively nest arbitrary multiple Decorator Objects , somewhat like the idea of recursion
to implement a standard singleton pattern is not complicated, but to use a variable to flag whether an object is currently created for a class, and, if so, to return directly to the object that was created the next time an instance of the class is obtained.
32. Separating the unchanging part from the changing part is the theme of each design pattern
33, the purpose of the strategy model is to separate the use of the algorithm and the implementation of the algorithm.
34. A policy-based program consists of at least two parts. The first section is a set of policy classes that encapsulate specific algorithms and are responsible for the specific computational process. The second part is the Environment class Context,context accepts the client's request, then delegates the request to a policy class. To do this, indicate that you want to maintain a reference to a policy object in the context.
35. The idea of a strategy model: Define a series of algorithms, encapsulate them one by one, and make them interchangeable. In fact, it is to wrap multiple if into multiple paths, pass in different parameters, execute different paths, each path strategy is different, but the general purpose is consistent.
36, the implementation of the strategy mode is not complicated, the key is how to find the value of package change, delegation and polymorphism from the implementation of the strategy pattern.
37, from the definition, the strategy mode is used to encapsulate the algorithm. But it is a bit overqualified to use the strategy model only for encapsulation algorithms. In real-world development, we usually spread the meaning of the algorithm, which is also used to encapsulate a series of "business Rules". As long as these business rules point to the same goals and can be replaced, we can use the policy pattern to encapsulate them.
37, the proxy mode is to provide a substitute or placeholder for an object in order to control access to it.
38, the agent mode is the key is, when the customer is not convenient to directly access an object or not meet the needs of the time, provide a surrogate object to control access to this object, the customer is actually access to the surrogate object, the surrogate object please make some processing of the request, and then transfer the request to the Ontology object.
39, virtual agent put some expensive objects, delay to really need it before the time to create, virtual agent key idea is to meet certain conditions before the actual implementation of the original object method.
40, the agent is in the proxy object of a method medium to the appropriate time (such as the picture pre-loaded), to execute the target object method, the two methods are the same name, so for the user this process is transparent. The agent is responsible for preloading the picture, and after the pre-loading operation is completed, the request is re-handed to the ontology myimage.
41, to the IMG node set src and picture preload These two functions, are isolated in two objects, they can change without affecting each other. And even if we don't need to preload one day, we just need to change to the request ontology instead of requesting the proxy object.
42. The cache proxy can provide temporary storage for some computationally expensive operations, and in the next operation, if the parameters passed in are consistent with the previous ones, the result of the operation stored previously can be returned directly.
43, such as paging requests, we can use the cache proxy to cache the data we have requested, not every time from the background crawl.
44, the proxy mode includes many small classifications, the most commonly used in JavaScript development is the virtual agent and the cache proxy. Although proxy mode is very useful, when we write business code, we often do not need to anticipate the need to use the proxy mode, when it is really inconvenient to access an object directly, it is not too late to write the agent. Because the real object logic does not change because of the proxy, the purpose of the agent is to execute the original object at the appropriate point in time.
45.
The publish-subscriber pattern can replace the hard-coded notification mechanism between objects, and an object does not have to explicitly invoke an interface from another object. Publish-Subscribe mode lets two objects loosely coupled together, although not quite clear about each other's details, but this does not affect the communication between them
46.
Implement publish-Subscribe ideas:
1. First specify who will act as the publisher (e.g. sales office)
2. Then add a cache list to the publisher to hold the callback function to notify the Subscriber (the Sales Office roster)
3. When the message is last published, the publisher will traverse the cache list, triggering the subscriber callback function (traversing the roster and texting each other).
47.
The advantages of the publish-subscribe model are obvious, one-time decoupling, and two decoupling between objects. The publish-subscribe pattern can also be used to help implement some other design patterns, such as the broker pattern.
48.
Command Pattern Scenarios:
Sometimes you need to send a request to some object, but you do not know who the recipient of the request is, or what the requested action is, and want to design the software in a loosely coupled fashion, so that the request sender and the requesting receiver can eliminate the coupling relationship between them.
49.
The theme of design patterns always separates the unchanging things from the changing things, and the command pattern is no exception. Something happens when you press a button, and something happens that is mutable.
50.
The origin of the command pattern is actually an object-oriented alternative to the callback function, and the callback is a direct function, such as adding. and command mode is an object, there can be many methods, such as adding and undoing, so we have to add and revoke the function, it is best to encapsulate the command object to pass, rather than directly transfer the function.
51.
Command mode in JS The white is, I want to execute a method, but I do not know its name and specific code, then I execute the command object through the closure of the return of the Execute method, this method returns is the function we want. See page 129 for specific examples
52.
Undo is a very useful function in the command mode, imagine the development of a go program, we have each step of the pieces of the changes are encapsulated as a command, you can easily implement the undo function. Also, the Undo command can be used to implement the CTRL + Z function of the text editor.
53.
The combination pattern is to construct larger objects with small sub-objects, and these small sub-objects themselves may be made up of smaller "grandchild" objects.
54.
When we add a command to the universal remote, we don't care whether the command is a macro or a normal subcommand. This is not important to us, we just need to make sure it is a command, and this command has an executable Execute method, then this command can be added to the universal remote control.
55.
The combo Mode command is that each layer of objects has an Execute method, and the root object executes execute, traversing all descendant objects, manifesting polymorphism.
56.
Each time a request is made to the topmost object, it is actually a deep-first search for the entire tree
57.
The biggest thing about the combo pattern is that you can treat the combined object and the base object consistently.
58.
Customers want to unify all the objects in the tree, and the combined mode allows the customer to ignore the difference between the combined object and the leaf object, and when the customer faces the tree, the client does not care whether the object currently being processed is a composite object or a leaf object, that is, not having to write a bunch of if, else statements to handle them separately. Combining objects and leaf objects will do their own right thing, which is an important ability to combine patterns.
59.
Combination Mode disadvantage: Each object in the system looks similar to other objects, and their differences are only highlighted when they run, which makes the code difficult to understand. In addition, if too many objects are created by combining patterns, these objects may not be able to afford the system.
60.
Design pattern based on inheritance--template method pattern
61.
The template method pattern consists of two parts, the first part is the abstract parent class, the second part is the concrete implementation subclass. The algorithm framework for subclasses is usually 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, by inheriting the abstract class, inheriting the entire algorithm structure, and optionally overriding the parent class's methods.
62.
The reason P154,beverage.prototype.init is called a template method is that it encapsulates the algorithm framework of a subclass, which acts as a template for an algorithm that instructs the subclass in what order to execute which methods.
63.
JavaScript does not support abstract classes, and we work with the following two workarounds:
1. Use the duck type to simulate the interface check to ensure that the parent class is actually overridden by a method in the subclass.
2, let Beverage.prototype.brew and other methods throw exception, if because careless forgot to write Coffee.prototype.brew method, then at least we will get a bug when the program runs.
64.
Placing hooks is a common means of isolating changes. We place hooks in a place where the parent class is prone to change, and the hooks can have a default implementation, and it is up to the subclass to decide whether to "hook up". The results of the hook method return determine the following steps of the template method, that is, the next direction of the program, so that the program has the possibility of change.
65.
When a program is written in a template-method pattern, it means that the subclass discards control of itself, instead notifies the subclass of the parent class, and which methods should be called. As a subclass, only responsible for providing some design details.
66.
In JavaScript, we often do not need to follow the sample to implement a template method pattern, higher-order function is a better choice.
67.
The core of the enjoy meta model is the use of shared technologies to effectively support a large number of fine-grained objects.
68.
The enjoy meta mode requires that the attributes of an object be divided into internal states and external States (where States usually refer to attributes). The goal of the enjoy meta-mode is to minimize the number of shared objects, and the following experiences provide some guidance on how to divide the internal and external states.
1. Internal state is stored inside the object.
2. Internal status can be shared by some objects
3, the internal state is independent of the specific scene, usually does not change
4, the external state depends on the specific scene, and changes according to the scene, the external state can not be shared
69.
The object that stripped the external state is called a shared object, and the external state is passed into the shared object when necessary to assemble it into a complete object.
70.
Enjoy meta-mode is a time-space-changing optimization mode
71.
The key to the enjoy meta-mode is how to differentiate between internal and external states
72.
Enjoy meta mode for scenarios:
1. A large number of similar objects are used in a program
2, due to the use of a large number of objects, resulting in a large memory overhead
3. Most state of an object can be changed to an external state
4. After stripping out the external state of the object, you can replace a large number of objects with relatively few shared objects
73.
When an object has no internal state, the factory that produces the shared object actually becomes a singleton factory.
74.
The object pool has some similarities to the Cheung Yuan pattern, but it does not separate the internal and external states from the process.
75.
The enjoy meta mode is a model for solving performance problems, which is different from the birth of most models
76.
The definition of a responsibility chain pattern is to allow multiple objects to have a chance to process the request, thereby avoiding the coupling between the sender and receiver of the request, linking the objects into a chain, and passing the request along the chain until an object has processed it.
77.
The biggest advantage of the responsibility chain mode is that the request sender only needs to know the first node in the chain, thus weakening the strong connection between the sender and a group of receivers.
78.
The use of AOP to test the chain of responsibility is simple and ingenious, but the way the function is stacked together, but also superimposed the scope of the function, if the chain is too long, it will have a greater impact on performance.
79.
The role of the mediator pattern is to remove the tight coupling between objects and objects
80.
If complex coupling between objects does cause difficulty in invocation and maintenance, and the coupling increases exponentially as the project changes, we can consider refactoring the code with the mediator pattern
81.
The key of state mode is to distinguish the internal state of things, and the change of the internal state of things often leads to the behavior change of things.
82.
The benefits of the state pattern are obvious, and it can localize the relationships between each state and its corresponding behavior, which are dispersed and encapsulated in their respective state classes, making it easy to read and manage code
83.
Status Mode definition:
Allows an object to change his behavior when its internal state changes, and the object seems to modify its class
84.
The same point of the policy mode and the state pattern is that they all have a context, some policy or state class, and the context delegates the request to these classes to execute
85.
The difference between the policy mode and the state mode is that the policy classes are equal and parallel to each other and there is no connection between them, so the customer must be familiar with the role of these policy classes so that customers can switch the algorithm at any time ; In the state mode, the behavior of the state and state is already encapsulated, and the transition between States is long overdue, andthe " Change behavior " thing happens inside the state pattern. For the customer, there is no need to know these details. This is where the state pattern works.
,
The role of adapter mode is to resolve the interface incompatibility between the two software entities, after using the adapter mode, the original interface is not compatible with the two software entities can work together
87.
If the existing interface is already working, then we will never use the adaptor mode, adapter mode is a "mend" mode, no one will use it at the beginning of the program design
88.
Common design principles: Single duty principle, Richter replacement principle, dependency inversion principle, interface isolation principle, synthetic reuse principle, least knowledge principle
89.
Open-closed principle: Software entities should be scalable, but not modifiable
90.
Eliminate conditional branching with the polymorphism of the object
Placing hooks is also a way of separating changes
91.
In JS, both the policy mode and the command mode can be easily implemented with the callback function.
92.
Publish-the subscriber pattern is used to reduce the dependency between multiple objects, and he can replace the hard-coded notification mechanism between objects, an object without having to explicitly invoke the interface of another object.
JavaScript Design patterns and development practices notes on knowledge points