In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to inherit and expand ObservableCollection. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Some time ago, a project I worked on had a small requirement, that is, for the paradigm collection type ObservableCollection
< T>Sort it. ObservableCollection
< >This type is very useful in WPF and Silverlight because it implements the INotifyCollectionChanged interface, and then, when data binding, if the ItemsSource property of ItemsControl is bound to an ObservableCollection
< T>Object, then when the collection changes (Add, Remove, Insert, Clear, etc.), the corresponding ItemsControl will also synchronize the Update. Due to ObservableCollection
< T>Inherited from Collection
< T>So it has no List
< T>The Sort method provided, so if we want to sort the ObservableCollection, we need to implement it ourselves.
Inheritance and extension: two solutions
To meet this requirement, I can think of two solutions. The first solution is to write a subclass, inherit ObservableCollection, and then implement the Sort method in it. The corresponding code is as follows:
1 public class SortableObservableCollection
< T>: ObservableCollection
< T>2 {3 public void Sort () 4 {5 Sort (Comparer
< T>.default); 6} 7 8 public void Sort (IComparer
< T>Comparer) 9 {10 int I, JTX 11 T index;12 for (I = 1; I
< this.Count; i++)13 {14 index = Items[i];15 j = i;16 while ((j >0) & (comparer.Compare (items [j-1], index) = = 1) 17 {18 Items [j] = Items [j-1]; 19 j = j-1 20} 21 Items [j] = index;22} 23} 24}
The second option is ObservableCollection.
< T>Add an extension method and the corresponding code is as follows:
1 public static class ObservableCollectionExtension 2 {3 public static void Sort
< T>(this ObservableCollection
< T>Collection) 4 {5 Collection.Sort (Comparer
< T>.default); 6} 7 8 public static void Sort
< T>(this ObservableCollection
< T>Collection, IComparer
< T>Comparer) 9 {10 int I, JTX 11 T index;12 for (I = 1; I
< Collection.Count; i++)13 {14 index = Collection[i];15 j = i;16 while ((j >0) & (comparer.Compare (Collections [j-1], index) = = 1) 17 {18 Collection [j] = Collection [j-1]; 19 j = j-1 X 20} 21 Collection [j] = index;22} 23} 24}
Note: the above code is just an example and does not check Argument as it should, nor does it consider whether other exceptions will occur at run time.
Comparison between inheritance and extension methods
Both of these solutions can meet the needs, so you need to compare them in order to adopt a better one. Combined with the above cases, I mainly compare the following points (of course, there are some examples that are not applicable to the above examples, which I have also compared):
1) merge assembly references and namespaces:
Whether it is inheritance or extension methods, if the inherited and extended classes are not in our assembly, then in order to use the new functionality of the extension, we must reference this new assembly. at the same time, we can write the namespace in the new assembly as the same namespace as the original class, so at this point, the two are tied.
2) for sealed class:
Since the sealed class can no longer be inherited, in this case if we need to extend the function for a sealed class, we can only consider the extension method, in this contest, the extension method + 1
3) for public, protected, internal, private members:
It must be clear to all that the extension method is just syntax sugar, so when we use the extension method, we cannot get access to the object private and protected members, and if we are not in an assembly, internal will become invisible. In contrast, the advantage of inheritance is that it can access protected members, so inherit + 1 in this battle
4) for polymorphism:
Extension methods can only extend public members to classes, so they can only achieve static polymorphism through the overload of methods. Although objects passed into the method body through the this keyword can use override methods on calls, you do not always implement override methods in extension methods, while inheritance can override abstract and virtual members in the base class to obtain dynamic polymorphism. Of course, overload has also become a pediatrics. So inherit + 1 in this battle.
5) extension methods for interfaces and abstract classes:
More than N extension methods have been added to the IEnumerable interface under the Linq namespace that comes out of IEnumerable 3.0. Although misuse will cause a lot of problems, they do make our development simple and straightforward. If you implement extension methods for interfaces and abstract classes, there will be many beneficiaries, and all classes that implement the interface will have these extension methods, but inheritance (here should not be called inheritance for interfaces, but implement) will not achieve this effect. Of course, there are some functions that we only want to have in a certain class or some classes, instead of making all the subclasses of abstract classes and classes that implement interfaces have this function, like extension methods, but in this comparison, extension methods are still better. Extension method + 1
6) for the scalability and maintainability of the code:
There are many times when some additional requirements suddenly appear after the code has been written. For example, I have written the logic of data binding, using ObservableCollection, and if I need to extend the Sort method with inheriting a subclass, then I will need to change a lot of code. Even sometimes the code is written by someone else and has been compiled into an assembly for you to use. You can get an ObservableCollection object in his code, but you can no longer turn it into SortableObservableCollection. Therefore, in this case, unless the generated assembly is decompiled, it can only be achieved through the extension method. And if there are new requirements, we can write another static class to add a new extension method, which inherits a subclass can not meet the needs. Therefore, the extension method + 1 in this battle
7) about reflection:
Everyone knows that reflection is a very convenient means, or a means that has to be used, at some point. In the case of reflection participation, the extension method can be said to have no opportunity to show its ability. For example, after I got the ObservableCollection (ObservableCollection), I still can't get the MethodInfo of the Sort method, so I can't Invoke, because that Sort method is defined in the static class ObservableCollectionExtension. If I want to use the Sort method, I have to reflect ObservableCollectionExtension, which is extremely inconvenient for users. Then when it comes to inheritance, this problem does not arise, and of course I can find the MethodInfo corresponding to the Sort method in the Type of SortableObservableCollection. Therefore, in the case of reflection participation, inherit + 1
8) about object-oriented:
I have always thought that object-oriented is a matter of opinion, and different applications on different occasions may have different choices for the two solutions. According to reason, there is no doubt that inheritance is a little more object-oriented, but of course this is just my personal feeling. In fact, as mentioned above, extension methods can also reflect polymorphism on method calls, but it just can't override methods. So in this comparison, inherit the subclass + 0.5
Through the above comparison, in fact, inheritance and extension methods are inextricably matched. I think it is necessary to analyze specific issues when choosing. For example, with regard to protected and sealed, we have to make a choice, and in more cases, we still need to consider factors such as ease of use and beautiful code. For example, with regard to my project, I finally chose the extension method because I didn't bother to change the previous code.
On how to inherit and expand ObservableCollection to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.