Detailed examples of composite mechanism in Java

Detailed examples of composite mechanism in Java

Inherited defects

The defect of inheritance is caused by its too powerful function. Inheritance makes subclasses depend on the implementation of superclasses, which is inconsistent with the principle of encapsulation. Once the superclass changes with the release of the version, the subclass may be destroyed, even if its code has not changed at all.

To be more specific, suppose we use a HashSet in our program, we need to add a function to count how many elements have been added to the HashSet since it was created.

Without knowing the defects of inheritance, we designed a class, inherited HashSet, added an attribute addcount for statistics, copied the add and addall methods, and modified the value of addcount in the method,

The code is as follows:

This class looks reasonable, but it doesn't work properly. Execute this Code:

Because only three elements were inserted, we expected the getaddcount method to return 3, but the fact was that it returned 6. What went wrong?

In fact, within the HashSet, the addall method is implemented based on the add method. Therefore, adding three elements with addall will call addall once and add three times. Look at our replication method again, and you can see why getaddcount returns 6.

Of course, you will say that since HashSet is implemented in this way, we don't need to duplicate the addall method. Yes, that's right.

However, although it works normally, its correctness depends on the fact that the addll method of HashSet is implemented on the add method.

Once the superclass modifies the implementation details, our functions may be affected.

In general, there are three natural defects in inheritance, which can make the software very fragile:

1) If a subclass calls the method of the parent class, it will depend on the parent class. Once the parent class is changed, the subclass may not work normally. 2) If the parent class adds a new method, and the child class happens to have provided a method with the same signature but different return values, the child class will not be able to compile. 3) Using inheritance when it should not expose unnecessary APIs to subclasses. In this regard, the Java platform makes mistakes. A typical example is that properties inherit hashtable, which is unreasonable. The property list is not a hash table, but the properties in Java code inherit hashtable. As a result, after users create property instances, there are two methods: put and setproperties, and two methods: get and getproperties, The put and get methods should not be exposed to users. Because in properties, both key and value should be strings, while HashMap can be other types or even objects.

Alternative to composite inheritance

The previous section talked about the defects of inheritance. In this section, let's take a look at the solution to this problem - composition.

First, we need a class that holds the set object. This class implements the set interface and calls the corresponding method of the held set object in the implementation method. Therefore, we also call it forwarding class:

Then, we can design a class with statistical function. We only need to inherit the forwarding class we just created, and then write the statistical logic code:

This implementation method avoids all the problems mentioned in the previous section. Because inheritance is not used, it does not rely on the implementation logic of superclasses, nor does it worry about the impact of new methods added to superclasses on us.

Moreover, there is another advantage of this writing. This class can add statistical functions to all classes that implement the set interface, not just HashSet, but also other sets such as TreeSet.

In fact, this is the decoration mode. Instrumentedset modifies the set and adds the count attribute to it.

summary

Inheritance will destroy the encapsulation of a class, making subclasses very fragile and vulnerable to destruction.

Use the compound method to modify the superclass, so that the subclass is more robust and powerful.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>