Mode in Java

xiaoxiao2021-03-06  44

There is always a myth in the world: design can and should be independent of the details of the implementation, and design is usually seen as an abstract concept and implements a specific example of a code. If we firmly believe that the design is a creative and purposeful activity: the concept of structures carefully developed for a target, ", if it is unable to explain its environment, or cannot work with the environment, then this structure is not Suitable for this goal. Environment includes target platforms - language, tools, libraries, middleware (MiddleWare), and so on. There is also its functionality and non-functional units.

We will think that designing a house when you don't know the terrain layout, or build a skyscrapers when you don't know the use of the materials that are not clear. We will thread the thread, and the concept of small coding is undoubtedly a guide to waste energy (time and money) in the design, and ultimately we find that the gap between theory and practice is better in practice. It is still large in theory. Although a certain part of a high-level design can remain unchanged in many technologies, more cases, we need to personally make this circle, allow (or even encourage) details and actual information to affect and inform the system. Structure.

The role of pattern is to get information on these structures. They can describe-anticipate or retrospective-designed and designed principles, which describes the problems from issues to resolution, description of the environment, acquisition work, and results that should be produced. Here, I will focus on the two modes - Command-query separation and command method - to allocate tasks for the method in a class interface, examine how they do each other and affect concurrency, distributed, and orderly environments and local implementation .

Interface design. As the name implies, the interface provides definition between different systems or between different components of the system. In the software, the interface provides a barrier to separate the goals from the implementation, separating the concept from the author from the author. In Java, there are many interfaces: the public part provides potential users with interfaces, the Protected part provides an interface for its subclass (Subclass) and the surrounding package; a package has a public part Reflection is another mechanism for providing, using an object method interface.

Constraint and supply. Standing in the user's angle, an interface is established and named a method of use of a destination model. The method in the class interface provides a special method of use. These constraints - type system, running is the abnormal mechanism and return value - making the objectives of class authors to reflect and strengthen. The easiest example in this regard is a understanding of the meaning of the package: Privatization ensures that information and behavior can only be operated by the user. However, for the package, far more than data is private. In the design, the package tend to involve self-containing. One requires you know how to call a method (EG "in a thread environment, call another method after a method call, you must clearly synchronize the object") The package is worse than that all of these are all included and hidden. Class (EG "This class is Thread-Safe"). The previous design has a designed vulnerability, and many of its limited conditions are blurred rather than strengthened. This pushes the responsibility to the user rather than letting the class provider to do these work to complete the design, and this is inevitable vulnerability.

In this case, the supply (AFFORDANCES) describes the feasibility and inconsistency of use.

Affordances refer to the perceived real properties of the refers, first, these attributes can determine the possible methods of use of things. A chair can be used to support other things, so you can sit. A chair can be transported. The glass can be translucent, or it can be broken ... supply provides a clue operated on a matter, the plate can be pressed, the handle can be rotated, and the groove can be inserted. The ball can throw or rebound. When the advantage of the supplied is used, the user can only determine what to do, without a picture, no tags have not been described. Complex things may need some explanations, but simple things should not be like this. When a simple thing needs to use pictures, labels, design is failed. One responsibility for class designers is to reduce the separation (GAP), matching targets, and a certain degree of freedom between the interfaces in the interface, and minimize the possibility of errors.

Design of environmentally sensitive environment. Execution of the method of space or time - for example, thread, remote method call, message queue - can have a profound impact on the correctness and efficiency of the design. The result of this separation is not ignored: concurrency introduces an unchecked and environmental selection overhead; distribution introduces a wrong and increasing return of the call overhead. These are the problems of design, rather than modifying BUG. In any case, the result is a Property-style programming that will hinder the property of ownership - When an interface is mainly composed of SET and GET methods, each method directly points directly to the private area. . Such classes have a poor package (meaning without cover). The domain accessor in the interface is usually not information: they cannot communicate, simploid and abstract in the use of objects, which usually causes lengthy and easier code. Programming for ownership style is not a big activity in a short time. Distribution and parallelism enlarges these format practices by introducing correctness and severe performance overhead.

Transparency and bug disaster. Abstract allows us to ignore detail when we need it, so our design ideas can balance the environmental factors rather than subject to them. Decide what kind of details can be ignored to become a challenge. The severity of the problem rose in the case of negligible in important details. Design tends to try to make environmental factors transparent as possible. Transparency can become a seductive idea: maybe it allows threads and remote objects to communicate completely transparent, so that users will not be aware when they communicate. The Proxy mode supports a certain extent of transparency. This enhances the basis of RMI and Cobra. The object of the local agent and the use of remote objects have the same interface in use, and the details on the group allow to call the model with familiar ways. However, this distribution is transparent and incomplete: mistakes and potential effects cannot be completely hidden and need to be considered. After all, it is not a towel.

Command-Query Separation guarantees a method is not a command (command) is query (query)

problem. Method, when they return a value to respond to a problem, with the nature of the query, when they take the change of the change of the object to change the object, with the properties of the command. So a method can be a pure Command mode or a pure query mode, or a mixture of both.

For example, in Java.util.ITeratorator, HasNext can be viewed as a query, Remove is a command, next and awkward merge commands and queries:

Public interface itrator {boolean hasnext (); object next (); void remove ();

If you don't go forward to the next, you cannot query an Iterator object. This results in an error in initialization, incremental, access (Access), and Advance, and defined loop structure: for (Initialization; Contission Condition; Advance) {... Access for USE. ..}

The result of incorporating the Command and Query function into a method is to reduce clarity. This may hinder the assertion-based programming and require a variable to save the query results:

Iterator itrator = collection.iterator (); iterator.hasnext ();) {Object current = iterator.next (); ... use current ...... again use current ...}

solution. Ensure that the behavior of the method is that the command or query can be queried, which can return the value of the value is a pure function and there is no complex effect (Side Effects), and the method of negative effects cannot be returned. "Another way to express this is to ask a question without affecting the answer."

Combined method combination methods often use in threads and distribution environments to ensure correctness and improve efficiency.

problem. Some interfaces that mainly provide intensive methods. At first, it seems to be minimized and adherent - all characteristics of attractive. However, some interfaces appear exactly in the process of use. They are too simplified to force class users to use more work to achieve conventional ending and manipulation of dependence (temporary coupling). This is very troublesome and is easily erroneous, leading to code repeat - code should be avoided - and provide a good breeding condition for BUG. Some ways to perform success simultaneously, encounter trouble during multithreading, abnormal, and distribution where you are executed. If two actions need to be implemented simultaneously, they must comply with collaborative or reverse Semantors - they must completely successfully execute or fail to reverse another action execution - they are Two independent methods are described. The introduction of thread increases the degree of uncertainty. A series of methods call a Mutable object does not ensure that the result is expected, if this object is shared between the thread, even if we assume a separate method is thread safe. Look at the interface to the Event Source, it allows the handle and the query of the event: Interface EventSource {handler getHandler (Event Event); void installhandler (Event Event, Handler NewHandler); ...} Threads between cross calls It will cause unexpected results. Suppose the Source domain references a thread shared object, which is likely to have a handle between 1, 2 between 1, 2: Class EventSourceexample {... public void example (Event Event, handler new, {oldhandler = eventsource.getrandler (Event); // 1eventsource.installHandler (Event, NewHandler); // 2} Private EventSource EventSource; Private Handler Oldrandler;} The same, this time is also a class user rather than designers to pay attention to this, formulate constraints:

class EventSourceExample {... public void example (Event event, Handler newHandler) {synchronized (eventSource) {oldHandler = eventSource.getHandler (event); eventSource.installHandler (event, newHandler);}} private EventSource eventSource; private Handler oldHandler; } If the target object is a remote, the overhead of the backhaul increases, and the migration failure is part of the environment to become part of the environment. In the previous example, we can assume that it is very short compared to the time of performing each method body and the delay of communication. In this example, the overhead is repeated twice and may repeat multiple times in other instances. There is also a problem with the use of the Synchronized sync block of external (Extern). Synchronized blocks are significantly used in distributed environments but can also be applied in a local thread environment: the use of proxy objects between the caller and targets. In short, the use of the SYNCHRONIZED block failed because of the synchronization of the agent object rather than the target object. The conservative statement is that this can have a basic impact on the system's trueity. Because agent use is transparent after the interface, the caller cannot do too much guarantee for behavior. solution. Combined Method must be executed simultaneously in distribution and thread environments. The combination should reflect ordinary use methods. In this way, a Combined Method may be clear than the original method because it reflects the direct application. Recovery strategies and some clumsy methods are encapsulated in Combined Method and simplify interfaces of user-wide perspectives. This improved package has lowered the cumbersome you don't need. All of Combined Method is to support a design of a transaction style. Providing a separate Query method in a joint Command-Query is usually reasonable. However, this requires the need to be developed, not forced execution. It is not common because combined method can complete this work because Comibined Method can complete this: The caller is simple to ignore the result. If a result is returned to incur an overhead, it may be uniform to unify a separate Command method. Back to the previous example, if the installHandler method returns a handle designed to become more simple and independent: class EventSourceExample {... public void example (Event event, Handler newHandler) {oldHandler = eventSource.installHandler (event, newHandler); The Private EventSource EventSource; Private Handler OldLer;} The caller provides a more secure interface and no longer needs to resolve the problem. This reduces the size of risks and code, and all the responsibilities of the class design give the class designer rather than the user. The emergence of a proxy object does not affect the correctness. A Combined Method can be a collection of query, a collection of Command, or both. In this way, it may supplement or conflict with the COMMAND-Query separation method. When the conflict occurs, the priority choice of Combined Method produces a different correctness and applicability. In another example, consider the case where resources are obtained.

转载请注明原文地址:https://www.9cbs.com/read-53688.html

New Post(0)