In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what is the principle of Java object-oriented synthesis / aggregation reuse". The editor shows you the operation process through an actual case, the operation method is simple and fast, and it is practical. I hope this article "what is the principle of Java object-oriented synthesis / aggregation reuse" can help you solve the problem.
Principle of synthetic reuse
Use some existing objects in a new object to make it part of the new object; the new object reuses these objects by delegating to them.
Composition and aggregation should be used if two classes are "Has-a" relationships, and inheritance can be used if they are "Is-a" relationships. "Is-A" is strictly taxonomically defined, meaning that one class is the "one" of the other. Unlike "Has-A", it means that a role has a certain responsibility.
What is synthesis? What is aggregation?
Composition and Aggregation are special types of Association.
Polymerization
Aggregation is used to denote the "ownership" relationship or the relationship between the whole and the part. The object that represents the part may be shared by multiple objects that represent the whole, and it may not be destroyed or destroyed with the destruction or destruction of an object that represents the whole. The life cycle of the part can go beyond the whole. For example, classes and students, when classes are deleted, students can still exist, and students can be cited by training institutions. Aggregation should not occur frequently in the design, which will increase the coupling of the design.
Aggregate relationship UML Class Diagram
Class Student {
}
Class Classes {
Private Student student
PublicClasses (Student student) {
This.student=student
}
} synthesis
Synthesis is used to represent a much stronger "ownership" relationship. In a synthetic relationship, the life cycle of part is the same as that of the whole. A synthetic new object has full control over its components, including their creation and annihilation. In programming language terms, the synthesized new object is absolutely responsible for the memory allocation and release of the components. The component objects of one composite relationship cannot be shared with another composite relationship.
Composite relation UML class diagram
Class Room {
Public Room createRoom () {
System.out.println ("create a room")
Returnnew Room ()
}
}
Class House {
Private Room room
Public House () {
Room=new Room ()
}
Public void createHouse () {
Room.createRoom ()
}
}
In other words, synthesis is Aggregation by Value of values, while aggregation is generally referred to as Aggregation by Reference of references.
Why use synthesis / aggregation reuse instead of inheritance reuse?
In object-oriented design, there are two basic ways to reuse existing designs and implementations in different environments, namely, through synthesis / aggregation and through inheritance. The characteristics and differences, advantages and disadvantages of the two are as follows.
Synthesis / polymerization multiplexing
Because composition or aggregation can incorporate an existing object into a new object and make it part of the new object, the new object can invoke the functionality of the existing object. The advantages of doing so are
The only way for a new object to access a component object is through the interface of the component object.
This reuse is black box reuse because the internal details of the component object are invisible to the new object.
This reuse supports packaging.
This kind of reuse requires less dependency.
Each new class can focus on a task.
This kind of reuse can be done dynamically during the run time, and the new object can dynamically reference the object of the same type as the component object.
In general, if a role gets more responsibility, you can use a composite / aggregation relationship to delegate new responsibilities to the appropriate object. Of course, this reuse also has its drawbacks. The main disadvantage is that the system built through this reuse will have more objects to manage.
Inheritance and reuse
Inheritance reuse obtains new functions by extending the implementation of an existing object, the base class obviously captures common properties and methods, and the subclass extends the implementation of the superclass by adding new properties and methods. Inheritance is the reuse of types.
Inherit the advantages of reuse
The new implementation is easier because most of the functionality of the superclass can be automatically entered into the subclass through inheritance relationships.
It is easier to modify or extend inherited implementations.
Inherit the disadvantages of reuse
Inheritance reuse destroys the wrapper because inheritance exposes the implementation details of the superclass to the subclass. Because the internal details of superclasses are often transparent to subclasses, this reuse is transparent reuse, also known as "white box" reuse.
If the implementation of the superclass changes, so does the implementation of the subclass. Therefore, when a base class changes, the change will be transmitted to one level of subclass after another, so that designers have to change these subclasses accordingly to adapt to the change of superclass.
The implementation inherited from the superclass is static and cannot change during run time, so it is not flexible enough.
Because of the above shortcomings of inheritance reuse, it is a very important design principle to reuse the implementation by using synthesis / aggregation instead of inheritance as far as possible.
This is the end of the introduction to "what is the principle of Java object-oriented composition / aggregation reuse". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.