Patterns in Java

Source: Internet
Author: User

There is always a myth: design can and should be independent of implementation details, design is often seen as an abstract concept and implementation is a concrete example of code. If we are convinced that "design is a creative and purposeful activity: the concept of a structure that is crafted for a purpose", a structure that is not appropriate for its environment, or which cannot collaborate with the environment, is not suitable for this purpose. The environment includes target platform-language, tools, libraries, middleware (middleware), etc. There are also its functional and non-functional units.

We would think it would be unreasonable to design a house without knowing the layout of the terrain, or to build a skyscraper when it is not clear which material to use. The idea that we see threads, distributions, and so on as small coding details is undoubtedly the trigger for wasting energy (time and money) in design, and finally we find that the gap between theory and practice is greater in practice than in theory. While in some cases a high-level design can remain unchanged in a number of technologies, more often than not, we need to make up the circle in person, allowing (and even encouraging) details and actual information to influence and inform the structure of the system.

The role of patterns (Patterns) is to obtain information on these structures. They can describe-predictive or retrospective-the principles of design and design, telling the problem to the solution, the environment, the motivation to get the job, and the results to be generated. Here, I'll focus on two patterns--command-query separation and Command method--assigning tasks to methods in a class interface, examining how they interact and affect concurrent, distributed, and ordered environments and local execution.

Interface design. As the name suggests, interfaces provide a definition between different systems or between different components of the system. In software, interfaces provide a barrier that separates the target from the implementation, separates the concept from the concrete, and separates the user from the author. In Java, there are many concepts of interfaces: The public part provides the interface of classes and methods for potential users, and the protected section provides an interface for its subclasses (subclass) and the surrounding packages; A package has a common part ; Reflection (Reflection) is another mechanism that provides and uses object method interfaces.

Restraint and supply. From the point of view of the user to the author, an interface establishes and names the use method of a purpose model. The methods in the class interface provide a special way to use them. These constraints--the type system at compile time, the exception mechanism that runs and the return value--make the purpose of the class author manifest and strengthened. The simplest example in this context is the understanding of the meaning of encapsulation: privatization ensures that class users can manipulate information and behavior only through the common method interface of the class.

However, for encapsulation, it's much more than just data private. In design, encapsulation often involves self inclusion (self-containment). A need you know how to call a method (e.g.) In a thread's environment, the encapsulation of a class that calls another method after one method call and you must explicitly synchronize the object "is not as good as putting all of these contained and hidden classes (e.g.). This class is Thread-safe ") good. There is a design flaw in the previous design, and many of its qualifications are vague rather than reinforced. This puts responsibility on the user rather than letting the class provider do the work to complete the class design, and this is inevitably flawed.

In this case, the supply (affordances) describes the feasibility and viability of the use.

The term supply (affordances) refers to the perceived real attributes of things, and, first of all, these attributes determine the possible ways in which things can be used. A chair can be used to support other things, so you can sit on people. A chair can still be transported (carried). Glass can be translucent or broken ...

Supply provides clues to the operation of things, the plate can be pressed, the handle can rotate, and the groove can be inserted into things. A spherical object can be tossed or bounced. When the supply advantage is used, the user can determine what to do only by looking at it: no graphs, no labels, no instructions. Complex things may need some explanation, but simple things should not. When the simple thing also needs to use the picture, the label to explain, the design is the failure.

One of the duties of a class designer is to reduce the gap between constraint and supply in the interface (GAP), match the target and some degree of freedom, and minimize the possibility of error usage.

Environmentally sensitive design. Separation of methods in space or time-for example, threading, remote method invocation, Message Queuing-can have far-reaching implications for the correctness and efficiency of the design. The result of this separation is not negligible: concurrency introduces the cost of uncertainty and environmental choice; The distribution introduces the wrong and increasing call overhead of the backhaul. These are the design issues, not the simplicity of modifying bugs.

In either case, the result is a design that will hinder ownership style (Property-style programming)--When an interface is composed primarily of set and get methods, each method points directly to the private area. The encapsulation of such a class is poor (meaning there is no cover). The domain accessors in an interface (field accessors) usually do not provide information: they cannot communicate, simplify, and abstract in the use of objects, which often results in lengthy and error-prone code. The design of ownership style is not a big activity in a short time. Distribution and parallelism magnify the problems of practice in these formats by introducing correctness and severe performance overhead.

Transparency and bug disasters. Abstractions allow us to ignore the details when necessary, so our design ideas can balance environmental factors rather than be subject to them. Deciding what kind of detail to ignore can be a challenge. The seriousness of the problem goes up when important details are not overlooked.

Design tends to make environmental factors as transparent as possible. Transparency can be a tempting idea: it makes it possible for threads and remote object communications to be completely transparent so that users do not perceive anything when they communicate with objects. The proxy pattern supports some degree of transparency. This reinforces the basis of RMI and Cobra. The object of the local proxy and the object using the remote have the same interface in use, and the details on the marshalling allow for invoking the model using familiar methods. However, this distribution is not entirely transparent: errors and potential effects cannot be completely hidden and need to be considered. Transparency, after all, is not a towel.

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.