"Design Patterns" Object-oriented summary-interfaces, generics, and delegates

Source: Internet
Author: User

The previous article, through a general guide, summed up my understanding of object-oriented, as well as the difference between rewriting and overloading.


The main content of this article is writing, my understanding of object-oriented features: interfaces, generics, and delegates. Many people think that I do not use them, so I can achieve the functions of the program, why use them? It can be said that if you want to mix IT industry, then we will become professionals. Then, we write the code is not only to implement its function so simple, we also want to improve the performance of the program, more importantly, we write the code is for others to see, to let others understand, to achieve teamwork. interfaces, generics, and delegates make our code more structured and professional.

Interface

Interface Definition

For example, define a car interface, there are wheels, the definition of parts, if you want to implement a Mercedes-Benz class, to achieve this interface, you must implement the definition of wheels and parts. To write another Audi class, and then inherit this interface, you must also implement the definition of wheels and parts.

If you do not use the interface, then I wrote the Mercedes-Benz class, may only implement the wheel method, but forgot to write the method of the parts, then can not produce Mercedes-Benz.

Interface Benefits

Different programmers write code, it is possible to have a different method name problem, then others call the time will be very troublesome.

For example, a lot of people jointly develop a product, everyone division of labor and Cooperation. But how can each other's program be well linked up? For example, if you use a function I wrote, but you don't know how I wrote it, you have no way to embed my code in your program.

But if everyone is using an interface, you can use an excuse to define the function in your code, and in the actual run, you call me to write the function, because their name, parameters, return values are the same, so run without problems. Development efficiency has been greatly improved.

In General , I think the interface is a normative role, so that the program process clearer, it can reduce coupling, improve scalability, so that development efficiency greatly improved, and is very suitable for the development of large projects.

generic type

To understand generics, you must understand its predecessor-the collection class (for example, the ArrayList collection). Collection classes can be used in place of arrays for flexible management of object groups. Notice the words here, the collection class is the array (kind of thing), the collection class is used to store the object.
Collection Benefits : 1, according to the size of use, on-demand dynamic increase

2. You can add, insert, or remove a range of elements at your discretion.

Disadvantage : The collection type is unsafe, regardless of what type of object is acceptable.

In particular, ArrayList for data that holds value types, such as int, string, and so on. Using ArrayList means that value types need to be boxed as object objects, and when used, there is also a need to perform unboxing operations, which leads to significant performance losses

To avoid the drawbacks of collections, generics are introduced in Net2.0. Generics are a collection class that defines a return type, without a doubt, a generic is a collection, but it is a collection that determines the type of storage object. This bypasses the boxing unboxing action and does not result in a unboxing error.

It can be argued that the collection class is the base class part of the generic, and the newly introduced part of the generic is the polymorphic extension of the collection base class.

In general, generics make the program performance-enhancing.


Delegate

We can understand that a delegate is a pointer to a method that can be used as a parameter. It is appropriate to perform certain things at some time or in a certain situation, which you do not know beforehand.

Analogy with real-life examples: A lawyer can sue for people, he can fight any type of lawsuit, but only the plaintiff commissioned a lawyer in court to know what type of lawsuit, so the lawyer is a commission.

when to use

1, equivalent to using the method as another method parameter (similar to the C function pointer)
2 . As a bridge in two methods that cannot be called directly
3 . Use a delegate when you do not know what the method specifically implements, such as: Use a delegate in an event


Summary

For the object-oriented summary, the first temporary, the next learning edge induction. Next I will start learning design patterns, I hope I can continue to adhere to, as always, enthusiasm. The next article "Design pattern" Summary

"Design Patterns" Object-oriented summary-interfaces, generics, and delegates

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.