"My colon Class" learning note "Design principle (1) Indirect principle

Source: Internet
Author: User

Indirect principle

All problems in computer science can is solved by another level of indirection, except of course for the problem of too ma NY indirections.

--david Wheeler

This is a famous word in the computer world. In the field of computers, such examples are numerous, such as the URI in the path path,http in the file system, the foreign key foreign key in the database, and the variables in the program have indirect reference assignments.

First of all, the variables, in the command paradigm programming, variables are abstract memory, is an indispensable mechanism. Even if it is not necessary, choosing the right variable is still worth recommending. For example, there are often puzzling constant values in the beginner's code, commonly known as magic numbers or magic constant. While it is theoretically not wrong, most of the time it is more appropriate to use variables. The variable is more obvious to the programmer than the magic number, which enhances the readability of the code. When the magic number appears in many places and needs to be modified, replace with the variable can be more than once. Again, the function, function and variable, is one of the most basic elements in procedural programming, and the mechanism of the two is expatiating-both provide an abstract mechanism and indirect means to abstract and indirection the code and expression respectively. The meaning of abstraction lies in the fact that, on the one hand, the specific details are concealed, the code is improved, and the clarity of the code is improved by giving explicit semantics. The Concise meaning lies in: On the one hand, the establishment of the name and real mapping, improve the consistency and maintainability of the Code, on the other hand, the realization of the separation of the name and the real, improve the flexibility and scalability of the code. Abstract and indirect are literally far apart, but in software design there are intrinsic logical associations, especially when they are the same as the ' layer ' modifier. In fact, the indirection in that famous saying is often replaced intentionally or unconsciously by abstraction. The indirect layer and the abstraction layer can be regarded as the two aspects of the same thing: an appropriate middle layer (not limited to the middle layer of the Application service layer in the narrow sense), manifests as the indirect level in the form, in essence manifests as the abstraction layer. In the software design, it embodies the paradigm abound of this unification. For security reasons, for example, the application is generally not authorized to deal directly with the hardware, and can only access the underlying resources indirectly through the core part of the operating system-the kernel (kernel). As a mediator between hardware and applications, the kernel provides an abstract API interface that enables applications to request services from system calls. Virtual machines, such as the JVM and the CLR, also belong to this class, which provides a new layer of indirection on top of the operating system. But the kernel focuses on security and performance, while the JVM and the CLR have other considerations such as portability, development efficiency, industry standards, and so on. In addition, the multilayer architecture (multi-layered architecture) is also an example of the use of indirect principles, the most famous OSI7 layer model including network protocols (OSI Seven layer models), tcp/ The IP4 layer model and the 3-layer or N-tier architecture of the network Application System (N-tier architecture). This type of architecture is characterized by the specific functions of each layer, the provision of services to the upper layers, and the downward layer of service; the lower level is not dependent on the upper layer, the upper layer relies on the adjacentLower layer (This type of architecture is called a closed architecture (closed architecture) or an opaque hierarchy (opaque layering), If the upper layer can request services directly from the non-adjacent lower layer, it is called Open architecture (openarchitecture) or transparent layering (transparent layering). )。 In particular, if a layer is a relatively independent subsystem, it is conceptually equivalent to a virtual machine with a special mission.

MVC represents 3 Modules: model, view, and controller, while the most common 3-tier architectures are: the presentation layer (presentation Tier/layer), the business layer tier/ Layer) and data tier/layer. At first glance, the two seem to be one by one corresponding relationships: both the model and the data layer are all about the data access, the view and the presentation layer are all about the user interface, the controller and the business layer are all related to the logical control, but the relationship between them is very different.

The MVC architecture and the 3-tier architecture, in addition to the interaction and process of the modules, are different in their application areas and scopes. MVC is primarily used as a schema or design pattern for interactive systems, such as a GUI-based or web front-office (front-end) application. Java, for example, includes both swing and JFace GUI toolkits, struts, JSP, Spring MVC, webwork, Tapestry and other Web application frameworks. The 3-tier architecture is typically the highest-level architectural design, which is often present in enterprise applications.

There are many variants of the MVC architecture, and figure 12.2 represents a simplified form for GUI applications, and figure 12-3 represents a simplified form for Web applications.

Compared with the general application, enterprise application usually contains the business logic of the specialized domain, and the business requirements and application environment of the system are more complex and changeable, which is often characterized by multi-user, large data volume, distributed, high concurrency, high integration and cross-platform. The requirements for software quality are relatively higher. People need to ensure not only the design quality including extensibility (extensibility), maintainability (maintainability), reusability (reusability), but also the safety (security), Reliability (reliability), scalability (scalability), performance (performance), availability (availability), interoperability (interoperability), etc., running on different servers, Helps provide the quality of the software's operation. The 3-tier architecture is not antagonistic to the MVC architecture, which often embeds the former's presentation layer. Is a Java EE-based application architecture.

The following discusses the application of the indirect principle in design mode. But before you do that, revisit oop from another perspective. First, it examines the encapsulation, which wraps the relevant data and operations and then sets the access control. The hidden data cannot be manipulated directly by the outside world and can only be read or rewritten indirectly through the public interface.

The public interface set of the class, the API, serves as the role of the indirect layer, and becomes the bridge between external and internal implementations. In the view of the user, the API is the only intermediary to access the service class, and in the view of the maintainer, the service class is free to replace the implementation without worrying about affecting the customer class. Further, it is possible to insert a new indirect Layer-interface (interface) between the customer class and the service class. This interface is inherited by the compiler for the implementation class, and implemented by a polymorphic mechanism binding at run time. Let's compare the graphs (shown in 12-5).

The 3 basic features of OOP bring 3 identities to the object, each of which is the result of normative abstraction. In fact, the principle of separation or indirect principle, the core is still the abstract principle. The separation of interfaces and implementations from encapsulation makes the API of the class an indirect layer, which completes the abstraction of the data, and the separation of the interfaces and implementations brought by inheritance and polymorphism makes the class's hyper-type an indirect layer, thus upgrading the data abstraction into a polymorphic abstraction. In connection with the relationship between the indirect layer and the abstraction layer, and between the normative and the abstract, we can draw the conclusion that the indirect, the separation, the abstraction, the norm, the concepts that do not seem to be necessarily connected in the daily semantics, but in the computer field is a wonderful agreement. In particular, since they fully embody the essence of OOP3 's basic features, they naturally become the only way of Ood (object-oriented design).

The introduction of abstract modules with indirect functions can not only reduce the interdependence and influence between modules, but also make the responsibilities of each module clearer and more exclusive, thus making the whole system more conform to the requirements of low coupling and high cohesion. However, the indirect principle can not be abused, excessive indirect layer will sacrifice the simplicity of the system or runaway performance for standby, in practice must weigh the pros and cons. For example, swing is actually using a simplified version of MVC, which combines a tighter view and a controller into a UI delegate (UI delegate). Similarly, the 3-tier architecture can degenerate into a 2-tier architecture in a simple system and evolve into a 4-or 5-layer or even more multi-layered architecture in a complex system.

Summarize:

"My colon Class" learning note "Design principle (1) Indirect principle

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.