Why does design come from?

Source: Internet
Author: User

Why does design come from?

Where does design come from? Yes. Is refactoring.

The design principle is the soul of the system design, and the design mode is the template for system development. Flexible applications are the principle of constant design. For example, to implement a user registration method, first think:

// Initial design

Public void register (string name, int32 age)

{

}

Under certain conditions, this method has been able to withstand the system's test and insert new user information to the database securely and stably. However, when the demand changes, you may have to make adjustments, which we callReconstruction. However, refactoring is far from expansion, but design. For example, if the current registration item has changed, you also need to register the Gender and phone number at the same time. If there is no design adjustment, it will be implemented:

// Demand change

Public void register (string name, int32 age, bool ismale, int32 phone)

{

}

This problem is solved to a certain extent by means of heavy load. However, this adjustment cannot be called restructuring, and there are at least the following drawbacks:

· When new registration information is available, you must constantly reload the Register Method to expand more information.

· The parameter list of the method register is too long, which is not an elegant code implementation.

· You need to modify the method calls in the system to adapt to the new overload method.

Rigid adjustments lose design flexibility. programs without thinking can only make program expansion and maintenance unmanageable. In fact, for the above problems, we only need to perform simple reconstruction, you can easily avoid these three drawbacks and implement a more flexible system. For example, a simple reconstruction is as follows:

Public class userinfo

{

Public string name {Get; set ;}

Public int32age {Get; set ;}

Public bool gender {Get; set ;}

}

By encapsulating user information into a class, you can implement a simpler parameter list. At the same time, the benefits of this function are far from avoiding the preceding three defects, and can also encapsulate user information, more reliable information hiding and exposure:

· You can encapsulate fields and attributes to control the read-only and read-write permissions of members .. Net 3.0's automatic attributes enable more elegant syntax games for Attribute encapsulation, which make C # a more attractive advanced language (see section 13.2 "appreciation of C #3.0 "):

// Define readable and writable attributes

Public String mobile {Get; set ;}

 

// Define read-only attributes

Public String password {Get; private set ;}

· Implement certain logic encapsulation. For example, you can check the validity of an email:

Private string email;

 

Public String email

{

Get {return email ;}

Set

{

String strreg = @ "^ ([\ W-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ W-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ";

 

If (RegEx. ismatch (value, strreg ))

{

Email = value;

}

Else

{

Throw new invalidcastexception ("invalidemail address .");

}

}

}

So how is the design implemented and established? The answer is object-oriented. Just like the above evolution process, the application of the elements of object-oriented Encapsulation has completed a more flexible design. In section 1.3 "encapsulation secrets", we will discuss encapsulation in detail, instance-based applications and pairs. net implements the essential analysis, which can enhance the understanding of the basic elements of object-oriented.

These object-oriented ideas and applications come from practice and are perfect for reconstruction.

This article is excerpted from the book ". Net (version 2nd) You must know ".

Book details: http://blog.csdn.net/broadview2006/article/details/6673353

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.