relationship between objects

There are four relationships between Java objects/classes: dependency, association, composition, aggregation, inheritance, and realization.

1. Dependence

Dependency: The functionality of one object depends on another object.

  • Analogy: Humans depend on food and air for survival

  • Embodiment: the dependent is embodied as the call of the dependent's local variables , parameters , and static methods

  • Example:

    UML:

    code:

    public class Human {
          
          
        public void live(Food food) {
          
          
            Air.breath();
            food.eat();
        }
    }
    
    class Air {
          
          
        public static void breath() {
          
          
            System.out.println("呼吸...");
        }
    }
    
    class Food {
          
          
        public void eat() {
          
          
            System.out.println("进食...");
        }
    }
    

2. Association

Association (Association): A class needs to know the situation (properties, methods) of another class, which can be two-way or one-way.

  • Analogy: Humans need to know whether the food has expired when eating

  • Reflection: The associated object is generally used as a member variable

  • Example:

    UML:

    code:

    public class Human {
          
          
    
        private Food food;
        public void judge() throws ParseException {
          
          
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            if (format.parse(food.getExpiration()).compareTo(new Date()) > 0 ) {
          
          
                food.eat();
            } else {
          
          
                System.out.println("食物已经过期");
            }
        }
    
        public Human(Food food) {
          
          
            this.food = food;
        }
    }
    
    class Food {
          
          
    
        private String expiration;
    
        public void eat() {
          
          
            System.out.println("食物安全,可以食用");
        }
    
        public Food() {
          
          
            this.expiration = "2023-04";
        }
    
        public String getExpiration() {
          
          
            return expiration;
        }
    }
    

3. Aggregation

Aggregation: A strong association relationship (has-a), the objects of the association are at the same level, and the aggregation is the relationship between the whole and the constituent individuals. Individuals can be shared between different wholes , for example, hydrogen molecules are also composed of hydrogen atoms.

  • Analogy: A water molecule is made up of hydrogen and oxygen atoms

  • Embodiment: The aggregated object is used as a member variable

  • Example:

    UML:

    code:

    public class WaterMolecule {
          
          
    
        private HydrogenAtom2 hydrogenAtom2;
        private OxygenAtom atom;
    
        public WaterMolecule(HydrogenAtom2 hydrogenAtom2, OxygenAtom atom) {
          
          
            this.hydrogenAtom2 = hydrogenAtom2;
            this.atom = atom;
        }
    }
    
    class OxygenAtom {
          
          
    
    }
    
    class HydrogenAtom2 {
          
          
    
    }
    

4. Combination

Compostion: A stronger association relationship (is-a) than aggregation. The composition relationship requires that the object representing the whole in the aggregation relationship is responsible for the entire life cycle of the object representing the individual/part, so the composition relationship cannot be shared . If the object representing the whole is destroyed or destroyed, then the object representing the individual must also be destroyed or destroyed.

  • Analogy: Human and human heart, brain

  • Embodiment: individual objects as member variables

  • Example:

    UML:

    code:

    public class Human {
          
          
        private Brain brain;
        private Heart heart;
        private String status;
    
        public void sustainLife() {
          
          
            brain.control(status);
            heart.beat(status);
        }
    
        public Human(Brain brain, Heart heart, String status) {
          
          
            this.brain = brain;
            this.heart = heart;
            this.status = status;
        }
    }
    
    class Brain {
          
          
        public void control(String status) {
          
          
            if (status.equals("0")) {
          
          
                System.out.println("脑死亡");
            } else {
          
          
                System.out.println("大脑控制身体");
            }
        }
    }
    
    class Heart {
          
          
        public void beat(String status) {
          
          
            if (status.equals("0")) {
          
          
                System.out.println("心跳停止");
            } else {
          
          
                System.out.println("心脏跳动");
            }
        }
    }
    

5. Inheritance

Inheritance (extends): The most coupled relationship between objects is the relationship between parent and child.

  • Analogy: Father and Son
  • Embodiment: subclass (derived class) and parent class (base class, super class)
  • Example:

    UML:

    code:
    public class Tom {
          
          
    	public void farm() {
          
          
    		System.out.println("种地");
    	}
    }
    
    class Jack extends Tom {
          
          
    	@Override
    	public void farm() {
          
          
    		System.out.println("种地");
    	}
    
    	public void makeWine() {
          
          
    		System.out.println("酿酒");
    	}
    }
    

6. Achieve

Implements: The relationship between the interface and the implementation class. The implementation class needs to implement all the methods declared in the interface.

  • Example:

    UML:

    code:

    public interface Animal {
          
          
        void live();
    }
    
    class Cat implements Animal {
          
          
        @Override
        public void live() {
          
          
            System.out.println("生存");
        }
    } 
    

Guess you like

Origin blog.csdn.net/m0_54355172/article/details/129030090