Composition Reuse Principle and UML class diagram

Posted by roach on Fri, 28 Jan 2022 13:59:59 +0100

Synthetic Reuse Principle

Composite Reuse Principle (CRP) is also called Composition/Aggregate Reuse Principle (CARP)

  • The principle is to try to use composition / aggregation instead of inheritance
  • Find out what changes may be needed in the application, separate them, and don't mix them with the code that doesn't need to change.
  • Programming for interfaces, not for implementations.
  • Loose coupling design between interactive objects

UML class diagram

1) idea installation plug-in PlantUML
2) Download and install graphviz
Download address: http://www.graphviz.org/download/.
Configure environment variables after installation

Verify the windows command line interface and enter dot -version

3) Click "Settings" - > "Other Settings" - > "PlantUML", and set "dot.exe file path in bin directory of graphviz" on the right

4) The corresponding uml diagram can be drawn according to the rules

Advanced usage of intellij idea using UML class diagram plug-in

Refer to this blog Advanced usage of intellij idea using UML class diagram plug-in.

Dependencyrely on
Associationrelation
GeneralizationGeneralization (inheritance)
Realizationrealization
Aggregationpolymerization
Compositecombination
  1. use case diagram
  2. Static structure diagram: class diagram, object diagram, package diagram, component diagram and deployment diagram
  3. Dynamic behavior diagram: interaction diagram (sequence diagram and cooperation diagram), state diagram and activity diagram

UML class diagram

  • It is used to describe the composition of classes (objects) in the system and various static relationships between classes (objects).
  • Relationships between classes: dependency, generalization (inheritance), implementation, association, aggregation and composition.

Class diagram 1: dependency

As long as the other party is used in the class, there is a dependency between them. If there is no other party, you can't even pass the compilation.

  • Class uses each other
  • If it is a member property of a class
  • If it is the return type of the method
  • Is the type of parameter received by the method
  • Method
public class Department {
}
public class IDCard {
}
public class Person {
}
public class PersonDao {
}
public class PersonServiceBean {
	private PersonDao personDao;// class

	public void save(Person person) {
	}

	public IDCard getIDCard(Integer personid) {
		return null;
	}

	public void modify() {
		Department department = new Department();
	}
}

Generalization (inheritance), implementation, association, aggregation and composition are inherently dependent

E # generalization
It can be understood as inheritance relationship (A inherits B, and A and B have generalization relationship)

public abstract class DaoSupport{
	public void save(Object entity){
	}
	public void delete(Object id){
	}
}
public class PersonServiceBean extends DaoSupport {

}

Implementation

Implementation relationship - > class a implementation B interface

public interface PersonService {
	public void delete(Integer id);

}
public class PersonServiceBean implements PersonService{

	@Override
	public void delete(Integer id) {
		System.out.println("delete..");
	}

}

Association

public class IDCard {
}
public class Person {
    private IDCard card; 
}

One way relationship 1 to 1

public class IDCard {
	private Person person ; 
}
public class Person {
    private IDCard card; 
}

Two way relationship 1 to 1

E # aggregation
Aggregation relationship represents the relationship between whole and part, which can be separated.

public class Computer {
	private Mouse mouse; //The mouse can be separated from the computer
	private Moniter moniter;//The monitor can be separated from the Computer
	public void setMouse(Mouse mouse) {
		this.mouse = mouse;
	}
	public void setMoniter(Moniter moniter) {
		this.moniter = moniter;
	}
}
public class Moniter {
}
public class Mouse {
}

Composition

It is also the relationship between the whole and part, but the whole and part cannot be separated.

public class Computer {
	private Mouse mouse = new Mouse(); //The mouse can not be separated from the computer
	private Moniter moniter = new Moniter();//The monitor can not be separated from the Computer
	public void setMouse(Mouse mouse) {
		this.mouse = mouse;
	}
	public void setMoniter(Moniter moniter) {
		this.moniter = moniter;
	}
	
}
public class Moniter {
}
public class Mouse {
}

Contrast aggregation and combination

public class Person {
    private IDCard card; //Aggregation relation
    private Head head = new Head(); //synthetic relation
}
public class IDCard {
}
public class Head {
}

Topics: Java UML