多媒体测试系统
 
STUDENT
 
FACULTY
 
SCHOOL
 
SUPPORT
 
PUBLIC
 
SIGNUP
DAILY QUIZ
 
     
  B U L L E T I N    B O A R D

How to code recursive relationships

(Subject: Systems Analysis/Authored by: Liping Liu on 3/20/2022 5:00:00 AM)/Views: 3178
Blog    News    Post   

First, the recursive relationships must be association. There is no recursive inheritance relationship! Second, to code a recursive association, just treat the class as two, one on each of the association. Follow the same rules on cardinality and navigability for regular associations. Of course, you will create one class, rather than two. Here are a few examples.
 
 
Example 1: two-way recursive relationship between Components: Component (*) —— (*) Component
 
 
class Component
{
        // native attributes and operations of component
 
        List ingredientCompoments;  
            //these are parts contained in the current component object
        List assemblyComponents;
            // these are the components that contain the current object as a part
 
}
 
 
Example 2:  one-way recursive relationship between Components: Component (assembly side *) ——> (ingredient side *) Component
 
 
 
class Component
{
        // native attributes and operations of component
 
         List ingredientCompoments;  
            //these are parts contained in the current component object
 
}
 
 
Example 3: one-way recursive relationship between Employees: Employee (manager side 0..1) ——> (subordinate side *) Employee
 
 
 
class Employee
{
        // native attributes and operations of Employee
 
        List subordinates;
             //these are employees managed by the current employee object
 
}
 
 
Example 4: two-way recursive relationship between Employees: Employee (manager side 0..1) —-- (subordinate side *) Employee
 
 
class Employee
{
        // native attributes and operations of Employee
 
        List subordinates;  
            //these are employees managed by the current employee object
        Employee manager;
            //this is the manager of the current employee object
 
}

 

Example 5: In the assembly pattern, recursive relationships are often translated into the relationship between the representative class and one of its subclass. Here is an example, and the code to implement the diagram is attached below.

 

public class Product
 
    // code
 
}
 
public class SelfMadeProdiuct : Product {
 
    //code
 
}
 
public class Part : SelfMadeProduct
{
     // code 
 
}
 
 
public class SemiProduct : SelfMadeProduct
{
 
    // code
    List ingredients;
        // parts or other semi-finished products that make up the current semi-priduct
}

 


           Register

Blog    News    Post
 
     
 
Blog Posts    News Digest    Contact Us    About Developer    Privacy Policy

©1997-2025 ecourse.org. All rights reserved.