Before you can learn design patterns, you must understand class diagrams and sequence diagrams. This will help you understand the relationships between classes and help you understand design patterns.
UML class diagram
Class diagrams in UML are used to represent static relationships among classes, interfaces, instances, and so on.
Abstract: All are in italics. Static: All are marked with underscores.
+ : indicates public field – : indicates private field # : indicates protected field ~ : indicates default(default)
1.1 Inheritance (extends)
abstract class Fruit {
private String name;
static String color;
abstract String eat(a);
String desc(a) {}}class Apple extends Fruit {
String eat(a) {}
public String send(a) {}}Copy the code
1.2 Interface
public interface OrderService {
void confirm(a);
}
public class OrderServiceImpl implements OrderService {
void confirm(a) {}}Copy the code
1.3 the aggregation
As long as a class holds another instance (whether one or more), they are aggregated.
public class OrderServiceImpl implement OrderService {
private OrderMapper orderMapper;
private DeliveryOrderMapper deliveryOrderMapper;
}
Copy the code
Second, sequence diagram
class Client {
Server server;
void work(a) {
server.open();
server.print("hello word"); server.close(); }}class Server {
Device device;
void open(a) {}
void print(String s) {
device.write(s);
}
void close(a) {}}Copy the code
The black arrow indicates the method invocation. For example, client calls server's open method
Third, summary
The class diagram represents “relationships that do not change over time (static relationships).” Sequence diagrams represent “things that change over time (dynamic behavior)”