Object-oriented design and object-oriented programming

Source: Internet
Author: User

I have found that object-oriented design is too serious to be overlooked. Use the OOP language, but not object-oriented design. It is nothing more than writing C programs with classes, and this is a common situation. The other is to over-emphasize the class and ignore the object, but the object is the root. However, this article will introduce the object-oriented design as the main type.


Objective

Object-oriented is a completely different from the traditional design of the concept of software, traditional software design is to function as the main body, function as a module, to function as the goal. and object-oriented, is the software running components as the main body, to achieve a function, just happen that the interaction of these components can be indirectly done. These components are the objects. Designing software with object-oriented design is actually designing a series of objects so that these objects can do exactly what the software needs to do directly in the run.


One type

Why, when object-oriented occurs, there will also be classes of this kind of thing. Because classes are a way of designing objects, dividing objects into different types and using classes to describe objects of various types. Like a blueprint, a person designs a building and paints a blueprint, so that he can build the building according to this blueprint at any time.

This is a reverse application.

We begin to have the concept of type, because we recognize the object first. Four legs, a long head, and a tail, very able to run, we found a lot of such objects, so established a type: horse. Later found dogs, pigs, cows, sheep and so on, and we found that they all have a common feature: breastfeeding births. So we set up a type: breastfeeding, this type contains all the objects that are breastfeeding. Then we find that there are other things: chickens, ducks, birds, fish, and so on, and they are different from breastfeeding, they are laying eggs, laying eggs and producing children. They have a common feature with breastfeeding children: they move. So we set up a type: An animal that contains all the objects that will move on its own. After the gradual discovery, finally has the present system.

We also found that any object, there are two things: behavior, attributes. Birds often sing, this is the behavior of birds, birds have various sizes, colors, look, this is the bird's properties. So our OOP language, when building type description objects, provides methods and member variables that correspond to behaviors and properties.

The language you are using may have a root type: object, which is actually the type itself, so any type of object can be treated as an object of type objects. We have known a lot of many objects, built a lot of many types, but also began to reverse cognition, is the class. The type from the object is constructed from the bottom up, and the reconstructed type divides the object from top to bottom.

We use the language, for the establishment of the type, there are three main elements: encapsulation, refinement, polymorphism. In C + +, for example, we define types by class, which is encapsulation, and the behavior and properties of objects of this type are in the class definition. You build a type: A book, and then build several types: Pinterest, silk Books, Thread, ebook, and so on, you will certainly classify these types as sub-types of books, which is refinement. You need to draw a shape that can be a rectangular, round, messy shape, so you just need to manipulate the shape type, such as adding a behavior to the shape type: painting. The type of refinement: rectangular round-shaped shapes each define their own picture behavior. You just need to get a shape-thinning object on the line, calling its behavior: Draw, that's polymorphic.

In software design, the use of object-oriented, in fact, is to establish a type system, each software has a own type system. We use types to design software that, when run, interacts with various types of objects to do exactly what is done.


Two variable non-changeable

When we build the type, we need to carefully consider the refinement of the problem. For example a type: Human, then we refine the two types: man, woman.

Class Human {};class man:public human {};class woman:public human {};

However, such refinement, there is a great problem. Because a man object, it becomes a woman object, and vice versa. Like the Poy in Thailand, would you think of her as a man? Why? Because she is a woman now. This is going to change. Instead of the type, the property is changed.

What is a property, the coordinates of an object, the name of a person, the fullness of your purse. These are attributes, which are mutable. But the type does not change, the horse belongs to the mammals, the steel belongs to the metal, these are invariable.

And sex, in fact, is a property, such as in the animal kingdom, many types of animals, will change gender, some have no sex. We should, rather, use sex as a property, not as a type.

Class Human{public:const string& Getsex () {return this.sex;} void Setsex (const string& newsex) {return this.sex;} Private:string sex;};

In this way, your type system is normal. Why, for example, you design a directory Ah, interpersonal management Ah, such as software, you refine the man, woman words, type system bloated, do not say, you just create a new person when you have to analyze whether to build a man object or woman object, as a property is not required, you only need human a (" Man "); Yes, he's denatured, so A.setsex (" woman "); Imagine what it would be like if you didn't use the attributes, but what you did with the type?

So, when we are building a type system, we need to be clear when we refine it, whether it is a property or a subtype.

Variable, is the attribute.

Immutable, is the type.


Three may not be possible

Here, we analyze the behavior of the object. When we design a type, how to design the method rationally.

I often see some, incorrect methods of design. The method is not placed in the correct position, is an extra method, or should not be used with a function.

For example, someone has to design a reading software, so the creation of this type of book:

class book{protected:string name; string author;};

Then, because the book was to be turned, he had such a design:

Class Book{public:void Before_page () {if (This.cur_page > 0) {this.cur_page-= 1;}} void After_page () {if (This.cur_page < This.page_num) {this.cur_page + = 1;}}    Protected: size_t cur_page; size_t Page_num;};

Looks like it's nothing. Again, it seems that there is no problem.

But.

This is a wrong design.

Object-oriented, each object has its own behavior, properties. We have a good analysis, page up, PAGE down, who's behavior. Is it the book's Own act? For example, in reality, we talk to a book: Turn to the next page, and then this book is special? Did you turn it over? It's a fairy tale already.

The book is whose behavior, is the behavior of the reader, the reader with the hand over. Rather than flipping through the book itself. Of course if you really see a book, please tell me that I want to worship miracles.

So how about the design of the page turn this behavior? Simple, very simple. As a reader of the method, it is possible.

Class book{public:    ...    const string& page ( Size_t n);p rivate:    ...    vector<string> pages;}; Class reader{public:    size_t page ()  {return this.cur_page;}     void before_page () {if  (this.cur_page > 0)  {this.cur_page  -= 1;}  }    void after_page ()  {if  (This.cur_page < this.page_ num)  {this.cur_page += 1;}  }private:    size_t cur_page;}; class con{public:    void render  ()  {cout << cur_ Book.page (Cur_reader.page ());  << endl;} private:    reader cur_reader;    book   cur_book;};/ /  The above design also brings an additional benefit: reduced coupling.

What is the object of the book, is an information object, only need to have attributes, title, author, each page of text, and so on. Of course, we need to use methods to package properties, which is a very important tool in software engineering. In such cases, book.page () is not the act of the book, but just the wrapper on the property.

The book, is the reader is the behavior, so, put in the type of reader, the most appropriate.

But why don't I just put cur_book in reader, as a property? Because the reader and the book, is two different things, we picked up a book, just and this book to establish a temporary connection. As a friend asks you, you will never ask, "What is your current book?", this is an attribute of the question, and just ask: "What book you are reading now", this is the contact of the law. The attributes of the reader are all related, such as the title, the current page number and so on. We don't need to put cur_book inside, but we can put a string book_name inside, as a property of reader.


Look at another example, this is different, this is a widely used error example!

Class A:public object{public:a* Clone () {return new A ();}}; Class B:public object{public:b* Clone () {return new B ();}}; ... object* src = ...//new A or bobject* k = Src->clone ();

Yes, clone. A very extensive type of method design. This is the use of polymorphic to clone objects, we do not need to control what is cloned object, as long as there is a clone method, I will let you clone, grams die you.

Let's take a closer look at where the problem is and where it is.

That's impossible.

This is different from the above, the book page above, just the packaging of attributes. And this is the real behavior, is a pure verb, even the noun is gone. This is the behavior of the object, is a type of method.

However, this is not possible and this object cannot be acted on.

For example, scientists cloning a sheep, if used in this widely used way, that is the scene: "Ah, the sheep, hurry up, cloning a new you, don't ink ah." “

Funny, isn't it ridiculous?

In fact, how did scientists clone it? It is the scientists who use various means to reproduce the sheep. In other words, cloning is the act of a scientist, not the act of the goat, the cloned person. Even if this sheep is proficient in astronomical geography Physics mathematics, Ah, so that perhaps it can really clone themselves, of course, if it has a variety of equipment can be used, if you find such a sheep, please tell me, I will worship.

Of course, do not be misled by the above paragraph, this is actually an executor and target object relationship, cloning by the scientists, the executor is a scientist, the target is the sheep. And this goat is not proficient in astronomy, geography, physics, Mathematics, if he can clone himself, then the performer is the sheep, the target is itself, the behavior is cloning.

So how should we design, clone?

That is to analyze who is the executor and put it in the executor.

Class scientist{public:sheep* Clone (sheep* target), dog* clone (dog* target), pig* clone (pig* target), money* Clone (money* target);//Actually money is an animal:)};...secientist sei;sheep s;animal* k = Sei.clone (&s);

There are times when we can't analyze the performer, or really bother to analyze, then as a function on the line

Class String:public Object {};class Vector:public object {};//You may need to use friend to connect these functions string* clone (string* target); vecto r* Clone (vector* target);... string s (); object* k = Clone (&s);


Do we see things in reality that we can clone ourselves? It's a miracle that you can clone yourself.

but everything in the universe is incredible.

There really is something you can clone yourself: the virus.

This is an off-topic.

So, although I said the widely used clone method is a wrong design, but refers to the semantic error, in software design, in fact, that is the correct design. Viruses can clone themselves, then the computer strings, arrays can clone themselves, what is very strange. Strangely, the computer was unthinkable. Now that you can clone yourself, clone acts as an object, and there's nothing wrong with it.


So we set up the type system, is the "now" as the reference, for "Now" is the same. But later, perhaps humans are no longer classified as high-level animals, not animals, then all the human as a sub-type of animal design, but also redesigned.so we use object-oriented design software, do not indulge in the type, but to indulge in objects. It is because I am addicted to the type in the above example that there is a view that clone cannot act as an object. What behavior and attributes, an object can clone itself, then give him a clone method, not, do not need to have, there is no need to define the type. Why JavaScript is more object-oriented than C++/java, because JavaScript is not addicted to types, and C++/java is addicted to types.

Object-oriented, object-oriented.


The above is a description of the type, below, is the real object-oriented introduction


Four object-oriented design

So what is object-oriented design? In the field of object-oriented, there are several aspects: OOA (Object-oriented analysis), OOD (object-oriented design), OOP (object-oriented programming).

The first is object-oriented analysis, and we need to figure out what objects are needed in the software, what the relationships are, and what to do. Note that it is an object, not a type. Like a reading software, what are the objects? For example, we can find that there are books, reading people, and interfaces, user settings and so on objects.

Then the object-oriented design, we are the analysis of the object as the main body, around these objects, to design software. Instead of focusing on functionality, designing software around functionality is not object-oriented. Object-oriented can only be object-based, what a messy function, are just these objects in the interaction, exactly indirectly completed. The partitioning of the modules probably reflects that you are not object oriented. is object-oriented, basically each module corresponds to an object. Java, of course, is not Java, it is a type partitioning module, rather than an object partition module.


Simply put, as long as your design is the object of the main, then is the object-oriented design.

file* f = fopen ("A.txt", "RB"), const char* str = "Hello world!\n"; fwrite (str, strlen (str), 1, f); fclose (f);

C language is not OOP language, but C language is Ood language, C language in many places, all reflect the object-oriented, such as Fopen series, mathematical series, string series and so on.

Here, F is a file object, fopen to create this object, Fwrite writes the data to a file object, Fclose closes a file object. Fwrite do not need to open the file you are, as long as you can write, it will write to you. Fclose no matter what file you open, anyway you pass in a file object, you turn it off. Fopen series, around the file object, that is, the File* type object, this is object-oriented design. In this, you open the A.txt file and then write Hello world!, which is done indirectly through the file object. fopen/fread/fwrite/fclose/ Is the method of the file object, which is the property of the file object in the file* structure.

A bigger and better example is the handle of the Windows API, almost all functions that revolve around handle. A handle object, either the topmost handle type, or a more granular type such as Hmenu, hmodule, and so on. You don't have to know what these handle are like, anyway you just use handle object to do things, Windows API, is around the handle object, this is object-oriented design, and is the purest.

Object-oriented design, and what language is irrelevant. Because this is the design, is reflected in the appearance, not the concrete appearance. We can use object-oriented design in any language.

What--afraid--is----------

But object-oriented programming and language are relevant.


Five process-oriented

Where exactly is the difference? The difference is that the process-oriented function is mainly focused on the target. Like the a.txt example above, it is done in a process-oriented way.

OpenFile ("A.txt"); const char* str = "Hello world!\n"; Write (str, strlen (str), 1); CloseFile ();

Here, is the pure implementation of the function, open the A.txt file, and then write the line, and then off. There are no file objects. In partitioning the module is also the main function: OpenFile (open file)/write (write things to file)/closefile (Close file)

The object-oriented approach is to object-oriented, there are file objects, so we set up a file object, around this object, to do something indirectly. Does it look like a small difference? In fact, the difference.


Six object-oriented programming

The object-oriented design of light is not enough, we also need to have in the bones to support the object-oriented design of the language, is what we are now common, oop language. These languages allow you to design objects more naturally, providing encapsulation, refinement, and polymorphism so that you can better use objects.

Object-oriented does not include encapsulation, refinement, polymorphism.

Object-oriented programming can be encapsulated, refined, polymorphic.

Why? Because object-oriented, the subject is the object, in fact, regardless of type. However, we need a tube type, so object-oriented programming will provide encapsulation, refinement, polymorphism to us, that is, we can have a means to manage different objects. Without this, it is difficult for us to achieve object-oriented.

Why Str.size () vs. strlen (str) is a huge step forward because we have a powerful means of achieving object-oriented. Object-oriented has long been, but has not improved the level of software design, and the emergence of object-oriented programming language, the development of software design field level.

However, although we have the OOP language, but still unconsciously deviate from object-oriented, resulting in the use of Class C program, with the object to directly complete the appearance of the function.

This is because there is no clear object, no object design.

Another deviation is the excessive emphasis on type, the importance of class, and the neglect of objects. For example, some programmers, almost the object-oriented into the design pattern, the code is all design patterns, and the most important, the core of the object, are submerged in various patterns.


So, there are three main elements in the entire object-oriented domain:

Object-Oriented Analysis

Object-oriented design

Object-Oriented Programming


Indispensable

In C language, we will use object-oriented design, to the OOP language, it should be more object-oriented design. Do not think about the function, do not think about the target, but the interaction of the object is just indirect realization of the user's needs, just, indirect, this is two very important important very important concept. A principal is an object, and a type is used to design an object. Using the interaction of these objects to implement the function directly, can not realize the function, cannot.


At the end of the conclusion, I would like to tell you one of the drawbacks of most of the OOP languages today is the over-emphasis type. For example, a problem occurs when an object requires only one entity, and the singleton pattern introduced in design mode is a very serious bug. Why, because in this case, the type is not necessary, many languages are breathtaking type design means, and there is no way to design a single object. There is only one object, meaning what encapsulation, refinement, polymorphism are not required, it is necessary to establish a type, and then use a variety of strange, inexplicable means to limit the creation of only one entity object, is to trouble. Object-oriented, the core is the object, not the type.

Object-oriented design and object-oriented programming

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.