软件设计笔记(一) : 类与类之间的关系
一个自用的软件设计笔记。

1. 关联关系(Association)

关联关系表示类之间的一种弱关系,通常是“has-a”的关系。每个对象可以独立存在。

代码示例:

class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Course {
    private String title;

    public Course(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

class Enrollment {
    private Student student;
    private Course course;

    public Enrollment(Student student, Course course) {
        this.student = student;
        this.course = course;
    }

    public void showEnrollment() {
        System.out.println(student.getName() + " is enrolled in " + course.getTitle());
    }
}

// 用法
Student student = new Student("Alice");
Course course = new Course("Math");
Enrollment enrollment = new Enrollment(student, course);
enrollment.showEnrollment();

2. 聚合关系(Aggregation)

聚合是一种特殊的关联关系,表示整体和部分的关系,但部分可以独立于整体存在。

代码示例:

class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Classroom {
    private List<Student> students;

    public Classroom() {
        this.students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void showStudents() {
        for (Student student : students) {
            System.out.println(student.getName());
        }
    }
}

// 用法
Student student1 = new Student("Bob");
Student student2 = new Student("Charlie");
Classroom classroom = new Classroom();
classroom.addStudent(student1);
classroom.addStudent(student2);
classroom.showStudents();

3. 组合关系(Composition)

组合关系也是整体与部分的关系,但部分的生命周期依赖于整体。

代码示例:

class Room {
    private Wall wall;

    public Room() {
        this.wall = new Wall();
    }

    class Wall {
        public Wall() {
            System.out.println("Wall is built");
        }
    }
}

// 用法
Room room = new Room(); // 当Room对象被创建时,Wall对象也被创建

4. 依赖关系(Dependency)

依赖关系表示一个类依赖于另一个类的变化,通常是一个临时关系。

代码示例:

class Printer {
    public void printDocument(Document doc) {
        System.out.println("Printing: " + doc.getContent());
    }
}

class Document {
    private String content;

    public Document(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }
}

// 用法
Document doc = new Document("Hello, World!");
Printer printer = new Printer();
printer.printDocument(doc);

5. 继承关系(Inheritance)

继承是一种“is-a”关系,表示一个类是另一个类的子类,并继承其属性和方法。

代码示例:

class Animal {
    public void eat() {
        System.out.println("This animal eats.");
    }
}

class Cat extends Animal {
    public void meow() {
        System.out.println("Meow!");
    }
}

// 用法
Cat cat = new Cat();
cat.eat(); // 从 Animal 类继承的方法
cat.meow();

6. 实现关系(Realization)

实现关系是接口和实现类之间的关系,表示类实现了接口中定义的方法。

代码示例:

interface Flyable {
    void fly();
}

class Bird implements Flyable {
    public void fly() {
        System.out.println("Bird is flying.");
    }
}

// 用法
Bird bird = new Bird();
bird.fly();

最后修改于 2024-06-01