Object-oriented rethinking

Source: Internet
Author: User

When we first learned an object-oriented language, we wrote the code like this:

We will first write a puppy class, then new it, and finally call its method to implement the function.

 

For example:

 

DOG d = new dog (); // create a puppy

D. Shout (); // when the puppy sees stranger, it will call

 

After reading this code, we can see that your basic process-oriented language learning is good ~~~~~


Okay, this code is here first. Let's talk about the object-oriented features:


1. Abstraction:

 

First, when do we use abstraction?


A. when designing a system, I need to classify some things, such as puppies and kittens, into an animal class, and then let the puppies inherit this animal. this is a basic class Abstraction

 

B. there is another type of abstraction. For example, if we write an interface and let a class implement this interface, This is the abstraction of the interface. (abstract classes and interfaces are the same, and there is no method body, must be implemented by subclass );

 

C. Use superclasses or interfaces for class member variables:

 

When a class is a member variable of another class C, and this subclass has many changes, we need to consider making the changed part into multiple subclasses, abstract these child classes into a parent class and use the parent class as a member variable of class C.


For example, I now have a lot of billing policies, and then there is a method in Class C to call the computing billing method, but because of the many situations during the call, I must dynamically determine which billing policy to use during the call. Therefore, when defining the member variable of C, we need to use the abstract parent class, defines a parent class or interface for many policies.


At this time, the situation is the same as that of the above kitten and puppy. In addition, the abstract purpose in the pre-design is the same as that in the reconstruction method, both for system scalability and ease of modification.

 

In general, abstraction plays a general role, just like holding something that feels cool, long, and tongue-spitting. ask you, do you know what this is? You are not sure which kind of snake this is, but you will definitely answer: This is a snake! Although this answer is not accurate, it gives you the time for Baidu. After you wait for Baidu, you can say: this is @ # [email protected] # snake ....





 

2. Encapsulation

 

What are the benefits of encapsulation:


1. First, think about the entity class you have used. You can quickly write the student ID and the instructor ID... Here, encapsulation of object data plays a packaging role and binds attributes to the class for convenient search and use. The attribute encapsulation is described above. There are also methods in the class, so there are also method encapsulation.

 

2. In the design mode, we do not need to participate in the creation process of some objects. For example, in the singleton mode, we assign the process of creating an instance of the class to the class itself for processing, the class creation is encapsulated in the class to avoid creating multiple instances. In addition, we are familiar with various factory modes and migrate object creation to specific factory classes, remove the selection question.

 

3. encapsulation of class Behaviors

Looking back at the command mode, we made every command into a class, separated the object of the request operation from the object of the execution operation, and got in touch with the tight coupling here.

 

 

3. Inheritance

 

In the object-oriented design, we are inevitably using inheritance. First, let's take a look at the benefits of accepted inheritance:

 

A. Basic Benefits:

You can inherit the attributes and methods of the parent class to obtain the behavior of the parent class. For example, when we use the template method, we place all repeated items in the parent class. The subclass only needs to make a small change on the basis of inheriting the parent class.

 

B. Achieve "type matching" through inheritance"






In the decorator mode, it is unreasonable to let the clothing class inherit people, but in design, it is through this inheritance, we can call the parent class method on the middle layer of the image display to achieve a decoration effect. I think this model is very cool !!!

 

 

But have you ever thought about the consequences of inheritance?

 

1. consequence of Multi-inheritance: the powerful multi-inheritance in C ++ provides great convenience for code reuse, but it also brings many problems, such as the ambiguity of Multi-inheritance, there is also the confusion of inheritance relationships;

 

2. Inheritance used for reuse makes it difficult to maintain the added subclass, because the subclass probably does not have a method that inherits from the parent class, in addition, it cannot block methods inherited from the parent class. It's like the rich-generation dad wants to arrange for his son to take over his company, but his son wants to play music. If the subclass does not need the parent class method or cannot implement the parent class method at all, this makes the inheritance relationship very embarrassing.

 

 

In the face of the sequelae of inheritance, we were able to replace inheritance with combinations.

 

 

4. Polymorphism

 

If we say the reason why object-oriented systems are so scalable and flexible, we feel that polymorphism plays a major role.

 

First, let's go back to the problem of starting puppies:

 

DOG d = new dog (); // create a puppy

D. Shout (); // when the puppy sees stranger, it will call

 

 

If one day I don't want to hear a dog call, but I want to hear a cat call, we may write as follows:

 

// DOG d = new dog (); // create a puppy

// D. Shout (); // when the puppy sees stranger, it will call

 

Cat c = new CAT ();

C. Shout ();

 

However, after two days, I want to hear the call again. At this time, you may comment out the code of the kitten and change it to the code of the call. However, as a programmer, you must realize that, A similar piece of code is written third time, so it must be a problem with the design.

 

At this time, the role of polymorphism is displayed, which is why the statement has always been emphasized in programming: pointing the reference of the parent class to the object of the subclass.

 

If we use this method:

 

Animala = new CAT ();

A. Shout ();

 

We will make future changes more convenient. When we want to implement a specific change, we only need to make a slight change!


In addition, there are also the manifestations of polymorphism such as rewriting and overloading, but I still think the most classic.

 

 

 

PS: I recently learned Java + head first. I feel like this is the beginning of my real object-oriented approach ~~~

 


Object-oriented rethinking

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.