In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I. data protection
1.
II. The means of OCP (opening and closing principle)
The so-called OCP means that it is open to extensions (when new requirements arise, it can be achieved by extending the existing model) and turned off for modifications (no modifications are allowed for existing binaries).
The key to realize the OCP principle is abstraction and encapsulation. Abstract encapsulation is used to deal with the parts of requirements that may change. The specific processing methods are as follows:
1. Using a polymorphic approach
Make an inheritance tree. This scenario allows entities with extended requirements to inherit existing entities for areas where changes will occur but are not very serious.
two。 Using the combination of inheritance and composition
For example: decorator mode, policy mode, state mode, bridge mode
3. Delayed binding:
Run-time registration:
Use Event style or Observer pattern to implement runtime registration. When an extension is needed, let the extended method listen for an event, and this method will be called (service lookup) when the event occurs.
Configuration file:
Use the configuration file for startup binding, write the information that needs to be modified and extended in the configuration file, and decide what to do by parsing the configuration file (DataDriven)
Inherited polymorphic mode (LSP)
Component replacement:
If you need to modify or extend the module, use the modified / extended module when loading the module to implement the load binding. Generally, the part of the change is written in a .dll file, and the .dll file is updated directly when it changes. (ReflectiveorMeta-LevelDesign)
Predefined protocols:
The protocol is predefined between the two, and then each process can change independently, as long as the communication protocol remains the same.
Protocols such as TCP/IP (Uniform Access)
4. Information hiding
The following is described in detail
5. Generalization module: Interpreter-Driven6. Demeter's rule of limiting interaction paths: an object should know as little as possible about other objects
Third, what are the two basic types of information hiding in a module, and what are their typical processing methods? 1. Each module has a basic secret-- external behavior and internal hiding each module hides the implementation of important design decisions, only the composition of the module can understand the details. All design decisions are independent of each other. That is, the separation of the interface function of a module from the details of the internal program of the module. The function interface is given to hide the details of the function realization program: appearance mode Facade pattern-- information hiding: decoupling between the user and the subsystems of the component to reduce the coupling Controller2. The module may have additional secrets-- changes that change the expected changes: separate the changes from the module and arrange them into new classes, methods, or design units. Then encapsulate and isolate all the secret so that once it changes, the change will not affect other program modules. The part of the program that is about to change needs to make a decision. Give the interface that needs to be modified, hide the implementation program details of the modified part: policy mode: extract the algorithm from the object containing it, encapsulate the algorithm (policy) into an object decoration pattern: decorate a functional adapter pattern that extends an instance of a class without changing the code of the class: convert the interface of the class into another interface expected by client Let classes with incompatible interfaces work together if the context is also mutable: use bridging mode 1. Policy mode: the algorithm to be changed is independent, encapsulated into an object according to OCP's method, and an inheritance tree is established for the algorithm (set an interface for it so that all possible altered versions of the algorithm implement this interface)
two。 State mode: the behavior of the object changes according to the change of the state (the change of the value of the attribute). The changed state is made into an inheritance tree independently, and each subclass of the implementation implements the behavior in a state. The original object contains a reference to the state object that represents the current state, and all behaviors use the behavior of the state object. (Context and State need to handle the state change, not Client)
The change comes from within, that is, you change your state after you have done something.
3. Bridging pattern: in cases where abstraction and implementation need to change independently, abstract and implementation are made into two different inheritance trees. Abstract interface and implementation interface are two completely different interfaces, the implementation interface generally contains very basic and primitive methods, while the abstract interface contains more advanced methods based on the original methods. There is a reference to the implementation object in the abstract interface, and the method of the abstract object needs to call the method of the implementation object (the method of the abstract interface is based on the method of implementing the interface). What are the means of implementing commonness and variability?
Polymorphism (inheritance) and aggregation
The polymorphism (inheritance) is suitable for the case of 1 of N, the common part is encapsulated in the parent class, and the variability part is encapsulated in the subclass.
Aggregation is suitable for M of N. Whole role class encapsulates the common part, and part role class encapsulates the variability part.
1. What if there are more than 2 different behaviors between a set of objects except the commonness?
Make multiple policy trees
2. What if there are differences in some behavior groups of an object set?
Part of the behavior is bound to a policy tree
3. What if there are differences in some attributes of an object set (and the methods that depend on these attributes)?
Make properties and methods into a policy tree
4. If a behavior of an object set needs to be accomplished by cooperative objects, but their cooperative objects are different, such as
What are we going to do?
Put the process of "calling collaboration objects" in Strategy, a variant of Command Pattern
5. What if the behavior of an object set is different because of the value of the attribute?
State Pattern
Fifth, intermediary decoupling mechanism (which Indirection means are often used to solve De-Coupling)
1) avoid repetition-just do it once: repetition often represents coupling, and modifying part of the repetitive code means modifying others
2) DIP:Dependency Inversion Principle, depending on the principle of inversion, that is, details should depend on abstraction, abstractions should not depend on details; defining interfaces in modules to be implement by other modules is a basic way to remove dependencies and reduce coupling
3) inheritance: commonness and difference
4) Design pattern
Intermediary mode
Define objects that encapsulate the way a set of objects interact; mediation promotes loose coupling by avoiding explicit references to each other; allows you to interact independently of differences; centralized control
Bridging mode
Result: decoupling interface and implementation-eliminates compile-time dependencies, improves scalability, and hides implementation details from client
1) dependency inversion: if the abstract entity needs to depend on the concrete entity, then make an interface for the concrete entity, the abstract entity can rely on this interface, and the concrete entity implements this interface
2) appearance mode: Faade is an indirect layer between the user and the subsystem. It encapsulates the internal implementation of the subsystem and provides access interface to the user, decoupling the two.
3) proxy mode: Proxy is an indirect layer between the user and the actual entity, which is responsible for forwarding the user's request to the actual entity and controlling the access to the actual entity to decouple the two.
4) Adapter pattern: an interface Target is used in the application, and an implementation of this Target needs to use another implementation, but that implementation is inconsistent with the current interface. Let the implementation entity of Target aggregate an object of Adaptee to make an object Adapter. When a user has a request for Adapter, Adapter forwards the request to Adaptee. At the same time, Adapter also needs to implement duties that users require but are not implemented in Adaptee, not just forwarding requests. (client- > Adapter- > Adaptee)
5) Event-Style event style: using an event handling mechanism, other modules can provide events to the processing object or register their own methods with an event. When this event occurs, the processing object will call all methods registered to the event to decouple the event source from the registered method.
6. The main mechanism, applicable scenarios, advantages and disadvantages of runtime registration. 1. Observer Pattern
Intent: defines an one-to-many dependency between objects that, when the state of an object changes, depends on
Its objects are notified and updated automatically
Applicable scenarios:
(1) when a change to one object needs to change other objects at the same time, it is not known how many objects need to be changed.
(2) when an object must notify other objects, and he cannot assume who the other objects are. In other words, you don't want this
These objects are tightly coupled.
(3) when a model has two aspects, one aspect depends on the other. Encapsulate the two in a separate pair
In the image so that they can be changed and reused independently.
Advantages:
Flexibility, variability and reusability are guaranteed. Reduce the degree of coupling between the target and the observer, achieve abstract coupling, and allow
By independently changing the target and observer, the target object can be reused without having to reuse its observer at the same time.
Support broadcast communication, and the target does not need to know who the observer is.
Disadvantages:
Increasing the complexity of the system makes it more difficult to understand and test the system.
An unintentional update by one observer may cause unexpected updates by other observers, and it can also lead to update errors, which
The kind of error is hard to catch.
2 、 Event Style
Compared with Observer Pattern, it is possible to monitor multiple events. That is, to achieve many-to-many dependencies between objects.
Other features are the same as Observer Pattern
3 、 Command Pattern
Intention: encapsulate a request as an object so that you can parameterize the customer with different requests; request scheduling
Team or record request logs, and support undoable operations.
Applicable scenarios:
(1) abstract the operation to be performed to parameterize an object.
(2) make, arrange and execute requests at different times.
(3) support undo operations by storing the state before implementing the operation.
(4) support to modify the log so that when the system crashes, these changes can be redone.
Advantages:
Decouple the object that invokes the operation from the object that knows how to implement it.
Commands can be stored in a stack or queue to support queuing of requests, and command processing mode maintains historical information.
Undo and redo operations can be easily supported, but additional state information must be stored to avoid latency issues.
It is easy to extend the Command object.
7. Special type processing mechanism
8. What are the common solutions to the creation of objects? 1. Simple scenarios:
Creator creator: for An and B objects, A creates B objects in the following cases: an aggregates B objects; A contains B objects; A records references to B objects; A uses B objects; A contains data that initializes B objects.
Coupling low coupling: in the case of An aggregating, including, recording, and using B, if the responsibility of creating B objects is delegated to other objects, A needs to produce redundant coupling with other objects
Cohesion high cohesion: when A contains data that initializes B objects, An is the information expert. According to the principle of high cohesion, A should assume the responsibility of creating B objects.
2. Complex scenarios: (1) scenario 1: only one instance is allowed to be created to use SingletonPattern, first privatize Constructor; declare a class instance of static private; create a getInstance method of publicstatic so that external classes can obtain such instances through this method. And if this method is used in multithreading, it should be declared as protected/synchronize to ensure thread safety.
(2) scenario 2: there is a limit on the number of instances
Make improvements based on singleton
(3) scenario 3: a class that does not know the object it must create; a class that wants to have its subclass to determine the object it creates; the class delegates the responsibility of creating the object to one of the multiple help subclasses, and the information about which help subclass should be localized as an agent
Factory Method
(4) scenario 4: control the extension of the subclass. The algorithm framework of the subclass and the parent class is the same, but the local implementation methods are different.
Template MethodPattern
(5) scenario 5: there is a type dependency among multiple class instances to be created.
Abstract FactoryMethod
(6) scenario 6: the creation and initialization of an instance is very complex, for example, the class to be instantiated is defined at run time, and the value of the variable changes during initialization.
Prototype Pattern
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.