The Spirit of abstraction

Source: Internet
Author: User

Software design, has been a very abstract, very difficult to understand the field.
However, design, not science, although there are evaluation criteria, but there is no right and wrong. Good software, like art, shines the crystallization of the wisdom of the predecessors.

This article will explore the popular software design, as well as summarize the good design principles and methods.

Basic principles of Design

When it comes to software design, many people will tell you how to design software, how to object-oriented, how to meet the design pattern. Yes, at present, learning software design, the first contact may be the object-oriented design method, 23 design patterns.

But I want to say that software design, not rigid, not only these models. Software design, is the creation of more flexible artwork, and object-oriented, may be just one of the factions .

We are familiar with the object-oriented, just one of thousands of design methods, 23 design patterns, but also the design of the iceberg, we have to jump out of the object-oriented coterie, to see the outside scenery.

But no matter what kind of design methods and ideas, there must be an evaluation standard, or a consensus, to explain what kind of design is a good design, the basic criterion is: cohesion, low coupling .

The so-called high cohesion , refers to a function, or a module, the internal package is better, the same function, as far as possible only one copy of the code, so you change a function, often change is very small. And the high cohesion of the Code, attaches great importance to the stability of the interface, in the case of not modifying the interface, modify the internal implementation, the external impact of minimal or no impact.

low coupling , it is said that the code module coupling between the low, the module can be easier to move and replace, the coupling between the module is low, the biggest advantage is the maintainability is strong, a module problems, easy to replace. And the reuse of strong, the same module, can be referenced in multiple places, reducing the cost of software.

As an example,

We now want to do an online shopping site, there are users and administrators of two roles, need to maintain a product list, the user's shopping cart, what kind of design method?

Here we just give a simple example, the reality is often more complex than this.

Method 1:

We write a piece of code for each page, assuming we use Java EE implementation, then we may have set up a few pages, we can follow the page division function, and then implement, to complete the page is:

    1. Login, registration page
    2. View Product List page
    3. Shopping Cart Page
    4. Background login Page
    5. Manage Product List page in the background

We will use JSP to write a page content, and then through the database operation, the corresponding table data out, and formatted to the front page, in the acceptance of the user submitted form, we query the database for it, and data inspection and filtering, and then manipulate the corresponding data table.

Through our hard-pressed programmer's concerted efforts, and finally realized the full functionality of the site can be used.
But... Customers suddenly found that they want to add a VIP user, so that VIP users can store more personal information, binding their own bank card accounts, so that they can provide VIP users with better services.

However, this is not simple, because all the code and page logic are mixed together, although there is a page division function, but add a new user role, most of the page needs to be changed, the workload is very large.

Method 2:

Let's start by thinking about what makes the change difficult. If you consider a user's model separately, it may be easier to implement, that is to say: we divide the function module according to the page, this idea is too rough .

We design a user model that abstracts individual data operations and allows us to extend the user type. Through the way of inheritance, we can not only achieve VIP users, and even expand enterprise users and so on.

We use object-oriented design here, because Java object support is very good, we design is also very convenient to use Java class to implement, so we can not only easily extend the model, but also easy to reuse the operation of the model.

Similarly, we continue to abstract the structure of the entire project, such as shopping carts, commodities, can be abstracted into a separate model class, and the key process control, but also can be abstracted into a controller.

This popular MVC architecture now plays a key role in software architecture.
This does not want to explain the MVC architecture, but to illustrate a problem, the software structure directly affects the software maintainability, Extensibility.

High cohesion, low coupling, these are good software design in the basic point of view, but more basic design considerations, is the software is to sub-module implementation, of course, this module is a software structure of the abstraction, can be said to be a class, can also be said to be a function, in short, is the software components. If a software has only one function, all functions are completed from top to bottom, there is no software design.

The core of software design--abstract

The core of software design, I think is the abstraction of things.

In software design, abstraction is the most basic way to design high cohesion, low-coupling code. Hopefully the code is designed better, then we need a good abstraction, a reasonable model of the real thing .

The reason is also very simple, the software is for the real World Service, only good description of the real thing characteristics of the model, in order to better meet the function we want to complete.

The previous example illustrates what is good abstraction, such as abstracting entities from a page into a data model, and abstracting entity-related operations into a model, which directly applies the object-oriented design pattern and forms a good abstraction.

Of course, there are many kinds of abstract methods, for example, in the famous Lisp language, the function is the first class citizen, the abstraction of various methods into functions is his practice, even the definition of data, can also be abstracted into the model constructor. This is another angle, from another aspect to examine the whole project, the resulting software structure, is also very different.

In Lisp language, the abstract way changed, no longer focus on the data model, but focus on the input and output of each function, combine different functions together, form a new function, is the basic idea of functional programming.

Good abstraction, often unrelated to software, is the effective concept of categorizing the characteristics of things in the brain. A good abstraction often affects the design style of an era. In the UNIX system, there is a very good design, the abstraction of everything as a file, device driver is also a file, IO operation is also a file, system monitoring, but also by the file. This design has affected the entire industry, people are surprised to find that the original operating system to abstract everything as a file, all operations have become unified and convenient. We add a new hardware driver, do not need to modify the system interface, with the device files can be accessed, including interprocess communication, also use memory shared files, monitoring system parameters, only need to open the file can be.

The influence of programming paradigm on people's thinking

I've always said, don't be confined to design patterns. The reason is simple, fixed design patterns often hinder your pursuit of better design. Sometimes there is a kind of blinders feeling.

Determine whether a software design is better, the simplest way is to see if there is no redundant code, eliminate redundancy is the first step in code optimization.
In general, there is always a way to merge redundant code together. But not without redundancy is a good design? This is not necessarily. Because it is possible that you eliminate redundant code, but introduce a complex structure, which is not worth the candle. Also, is to see the readability, if the elimination of redundant code, reduce the readability of the code, the function is clearly a function, after redundancy to eliminate, but do not understand what is doing, at this time, it is necessary to avoid code integration.
Or, another way to do this is to build new abstractions and abstract new concepts so that you can transform your thinking and build code that is neither redundant nor easy to understand.

I like the multi-paradigm programming language, this kind of language does not stipulate you to use exactly what kind of programming model, your free play space is wide, the Representative is C + +, LISP, Scala and so on, are very excellent language.

If you only attribute object-oriented design, then I strongly recommend that you take a look at functional design and aspect-oriented AOP programming, which is often instructive to your design.

Layering is a powerful way to solve software design problems

In fact, layered thinking and abstraction is same strain, a group of software functions of the overall abstraction, often can form an intermediate layer, the vast majority of complex problems, can be layered through the idea, divide and conquer, the task is constantly simplified, the final problem to be solved.

For example, our Internet, the Internet is a model of layered problem solving, under the TCP/IP protocol, there is the concept of peer layer. We know that the TCP/IP protocol family employs a hierarchy of 4 layers, each of which calls the protocols provided by its next layer to fulfill its own needs.
Protocol is divided into: network interface layer, network layer, Transport layer, application layer. The network interface layer solves the problem of the communication of two hardware devices directly, through the error retransmission mechanism, guaranteeing the reliable transmission of the direct connection information; The network layer is responsible for locating the target address, packet forwarding and routing, filling the data with the IP packet, sending to the target machine, the transport layer is complex format information flow, and provides stable and reliable transmission, The transmission must be started with three handshakes, and each packet is sent back to the receiving party for confirmation before it is safe to transmit.

Such a complex internet, through layered solutions, has been a reliable and effective implementation. Nowadays, the development of the Internet has proved that the design of the year is indeed flexible and reasonable.

In the field of software design, there is such a famous proposition:
Any software design challenge can be achieved by adding an intermediate layer.

We are not here to argue whether this proposition is reasonable, but layering is really the essence of human design experience for so many years. Stratification embodies two important ideas, division and abstraction, which is the key to solving the problem.

This article belongs to the West Wind getaway original, reproduced please indicate the source: West Wind World http://blog.csdn.net/xfxyy_sxfancy

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Spirit of abstraction

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.