157 recommendations for writing high-quality code to improve C # programs--Recommendation 112: Abstract objects from the real world into classes, circle reusable objects, namespaces.

Source: Internet
Author: User

Recommendation 112: Abstract a real-world object into a class and circle reusable objects as namespaces

What is the object in the world around us? Objects are things, commonly known as "things". What, then, is an object? Objects have properties and behaviors. Take animals, for example, cats (cat). Cat can have name, which is the attribute; cat has a bad habit scratchsofa (scratching the couch), which is behavior. The combination of these attributes and behaviors is called a type:

    class Cat    {        publicstringgetset;}          Public void Scratchsofa ()        {            Console.WriteLine ("{0} in Flex sofa ", Name);        }    }

Of course, a type is just a certain kind of object, it refines the interface, standardize the protocol, and the real object is to be produced. For example, Tom Cat, must produce (that is, new) out, in the real world (can be understood as the CLR runtime) really exist (that is, allocating memory). The code is as follows:

Cat Tom =new"Tom"; Tom. Scratchsofa ();

Any "thing" in the software can be understood with this idea.

When we encode the CLR as a real world, and always consider abstracting some objects with common properties and behaviors into one type, we take the first object-oriented step.

Real-life objects can be categorized, which is often a tree-shaped structure. We generalize biology to animals and plants.

The same is true of objects in the CLR, in the case of forms in WinForm, we divide the forms in the system into the background management form and the report foreground form.

In the coding process, we manage the types with packages (folders), and we have the concept of "namespaces". Each node in the namespace, such as the "Manage Forms", represents a taxonomy. Under this node, there is a set of types with properties and behaviors that are relatively close. If the types are not grouped, the chaos will appear immediately. Of course, in agile development, all types or namespaces are not required to be defined in the first place, and as each iteration progresses, the organizational structure of the type can be more logically integrated by refactoring.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--Recommendation 112: Abstract objects from the real world into classes, circle reusable objects, namespaces.

Related Article

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.