01 스프링 등장 배경, 객체지향

p11

// 두 개의 정수를 더하는 메서드
public int add(int a, int b) {
    return a + b;
}

// 세 개의 정수를 더하는 메서드 (오버로딩)
public int add(int a, int b, int c) {
    return a + b + c;
}

p11

// 부모 클래스
public class Animal {
    // 부모 클래스의 메서드
    public void sound() {
        System.out.println("The animal makes a sound");
    }
}

// 자식 클래스 1: Dog
public class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("The dog barks");
    }
}

p14

// 단일 책임 원칙을 위반한 클래스
public class Employee {
    public void calculateSalary() {
        // 급여 계산 로직
    }

    public void generateReport() {
        // 리포트 생성 로직
    }
}

p14

public class Employee {
    public void calculateSalary() {
        // 급여 계산 로직
    }
}

public class ReportGenerator {
    public void generateReport() {
        // 리포트 생성 로직
    }
}

p15

// 개방-폐쇄 원칙을 위반한 코드
public class Rectangle {
    public double length;
    public double width;
}

public class AreaCalculator {
    public double calculateRectangleArea(Rectangle rectangle) {
        return rectangle.length * rectangle.width;
    }
}

p15

// 도형 인터페이스 정의
public interface Shape {
    double calculateArea();
}

public class Rectangle implements Shape {
    public double length;
    public double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double calculateArea() {
        return length * width;
    }
}

p15

public class AreaCalculator {
    public double calculateArea(Shape shape) {
        return shape.calculateArea();
    }
}

p16

public class Bird {
    public void fly() {
        System.out.println("Flying");
    }
}

public class Penguin extends Bird {
    @Override
    public void fly() {
        throw new UnsupportedOperationException("Penguins cannot fly");
    }
}

p16

public class Bird {
    public void move() {
        System.out.println("Moving");
    }
}

public class FlyingBird extends Bird {
    public void fly() {
        System.out.println("Flying");
    }
}

public class Penguin extends Bird {
    @Override
    public void move() {
        System.out.println("Penguin is walking");
    }
}

p17

public interface Worker {
    void work();
    void eat();
}

public class Robot implements Worker {
    @Override
    public void work() {
        System.out.println("Robot working");
    }

    @Override
    public void eat() {
        // 로봇은 먹지 않음. 불필요한 메서드.
    }
}

p17

public interface Workable {
    void work();
}

public interface Eatable {
    void eat();
}

public class Robot implements Workable {
    @Override
    public void work() {
        System.out.println("Robot working");
    }
}

p18

public class Light {
    public void turnOn() {
        System.out.println("Light turned on");
    }
}

public class Switch {
    private Light light;

    public Switch(Light light) {
        this.light = light;
    }

    public void operate() {
        light.turnOn();
    }
}

p18

public interface Switchable {
    void turnOn();
}

public class Light implements Switchable {
    @Override
    public void turnOn() {
        System.out.println("Light turned on");
    }
}

public class Switch {
    private Switchable device;

    public Switch(Switchable device) {
        this.device = device;
    }

    public void operate() {
        device.turnOn();
    }
}

Last updated