// 자주 변동이 일어나지않는 기타의 정보 클래스 (변동이 일어나지 않는 정보에 대해서만 책임을 가지고있음)
public class GuitarInfo {
private Integer serialNumber;
public GuitarInfo(Integer serialNumber) {
this.serialNumber = serialNumber;
}
public Integer getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(Integer serialNumber) {
this.serialNumber = serialNumber;
}
}
// 자주 변동이 일어나는 기타의 정보 클래스 (자주 변동이 일어나는 정보에 대해서만 책임을 가지고 있음)
package SOLID;
public class GuitarSpec {
private Integer price;
private String model;
public GuitarSpec(Integer price, String model) {
this.price = price;
this.model = model;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
// 기타를 만드는 책임만 가지고있음
package SOLID;
public class Guitar {
private final GuitarInfo guitarInfo;
private final GuitarSpec guitarSpec;
public Guitar(GuitarInfo guitarInfo, GuitarSpec guitarSpec) {
this.guitarInfo = guitarInfo;
this.guitarSpec = guitarSpec;
}
public GuitarInfo getGuitarInfo() {
return guitarInfo;
}
public GuitarSpec getGuitarSpec() {
return guitarSpec;
}
}
// 기타 행동제어,상태변화에 대해서만 책임을 가짐
public class GuitarManager {
public void updatePrice(Guitar guitar, Integer newPrice) {
guitar.getGuitarSpec().setPrice(newPrice);
}
}
// 실제 활용 예시
public class Main {
public static void main(String[] args) {
GuitarManager gm = new GuitarManager();
GuitarInfo guitarInfo = new GuitarInfo(2148123);
GuitarSpec guitarSpec1 = new GuitarSpec(1000,"첫번째 기타");
Guitar guitar1 = new Guitar(guitarInfo,guitarSpec1);
System.out.println(guitar1.getGuitarSpec().getPrice());
gm.updatePrice(guitar1,5000);
System.out.println(guitar1.getGuitarSpec().getPrice());
}
}
public class ViolinInfo implements Info {
private Integer serialNumber;
public ViolinInfo(Integer serialNumber) {
this.serialNumber = serialNumber;
}
@Override
public Integer getSerialNumber() {
return serialNumber;
}
@Override
public void setSerialNumber(Integer serialNumber) {
this.serialNumber = serialNumber;
}
}
public class ViolinSpec implements Spec {
private Integer price;
private String model;
public ViolinSpec(Integer price, String model) {
this.price = price;
this.model = model;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
public class Violin implements MusicalInstrument {
private final Info info;
private final Spec spec;
public Violin(Info info, Spec spec) {
this.info = info;
this.spec = spec;
}
public Info getInfo() {
return info;
}
public Spec getSpec() {
return spec;
}
}
public class ViolinManager implements Manager {
private final MusicalInstrument musicalInstrument;
public ViolinManager(MusicalInstrument musicalInstrument) {
this.musicalInstrument = musicalInstrument;
}
public void updatePrice(Integer newPrice) {
musicalInstrument.getSpec().setPrice(newPrice);
}
@Override
public void play() {
System.out.println("바이올린을 킵니다");
}
}
public class Main {
public static void main(String[] args) {
// guitar
Info guitarInfo = new GuitarInfo(12426);
Spec guitarSpec = new GuitarSpec(1000,"어쿠스틱 기타");
MusicalInstrument guitar = new Guitar(guitarInfo,guitarSpec);
Manager guitarManager = new GuitarManager(guitar);
guitarManager.play();
guitarManager.updatePrice(10000);
System.out.println(guitar.getSpec().getPrice());
Info violinInfo = new ViolinInfo(771512);
Spec violinSpec = new ViolinSpec(715000,"100년산 바이올린");
MusicalInstrument violin = new Violin(violinInfo,violinSpec);
Manager violinManager = new ViolinManager(violin);
violinManager.play();
violinManager.updatePrice(7000000);
System.out.println(violin.getSpec().getPrice());
}
}
리스코브 치환의 원칙 지키는 사례
class Animal {
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow");
}
}
리스코브 치환의 원칙 어기는 사례
class Animal {
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
class Bird extends Animal {
@Override
public void makeSound() {
throw new UnsupportedOperationException("Birds don't make sounds");
}
}
// ComputerType Enum (컴퓨터 타입을 구분하는 enum)
public enum ComputerType {
A_COMPUTER,
B_COMPUTER
}
// Computer 클래스
public class Computer {
private ComputerType type;
public Computer(ComputerType type) {
this.type = type;
}
public void playComputer() {
switch (type) {
case A_COMPUTER:
System.out.println("A 컴퓨터를 합니다");
break;
case B_COMPUTER:
System.out.println("B 컴퓨터를 합니다");
break;
default:
throw new UnsupportedOperationException("Unknown computer type");
}
}
}
class AComputer implements Computer {
@Override
public void playComputer() {
System.out.println("A 컴퓨터를 합니다");
}
}
class BComputer implements Computer{
@Override
public void playComputer() {
System.out.println("B 컴퓨터를 합니다");
}
}
interface Computer {
public void playComputer();
}
class Computer {
public void playComputer() {
System.out.println("컴퓨터를 합니다");
}
}
class Mouse {
public void playMouse() {
System.out.println("마우스를 합니다");
}
}
package SOLID.computer;
class Computer extends Machine{
public void playComputer() {
System.out.println("컴퓨터를 합니다");
}
}
class Mouse extends Machine{
public void playMouse() {
System.out.println("마우스를 합니다");
}
}
class Machine {
public void on() {
System.out.println("전원을 킵니다");
}
}
package SOLID.isp.powerControl;
public class Main {
public static void main(String[] args) {
PowerControlManager powerControlManager = new PowerControlManager();
PowerControlStateManager powerControlStateManager = new PowerControlStateManager();
Tv tv = new Tv(powerControlManager,powerControlStateManager);
tv.on();
tv.off();
tv.getOn();
tv.getOff();
Ramp ramp = new Ramp(powerControlManager,powerControlStateManager);
ramp.on();
ramp.off();
ramp.getOn();
ramp.getOff();
}
}
interface PowerControl {
void on();
void off();
}
interface PowerControlState {
void getOn();
void getOff();
}
class PowerControlStateManager implements PowerControlState {
@Override
public void getOn() {
System.out.println("전원이 켜져있는값 반환");
}
@Override
public void getOff() {
System.out.println("전원이 꺼져있는값 반환");
}
}
class PowerControlManager implements PowerControl {
@Override
public void on() {
System.out.println("전원을 킵니다");
}
@Override
public void off() {
System.out.println("전원을 끕니다");
}
}
class Tv implements PowerControlState,PowerControl {
private final PowerControl powerControl;
private final PowerControlState powerControlState;
public Tv(PowerControl powerControl,PowerControlState powerControlState) {
this.powerControl = powerControl;
this.powerControlState = powerControlState;
}
@Override
public void getOn() {
powerControlState.getOn();
}
@Override
public void getOff() {
powerControlState.getOff();
}
@Override
public void on() {
powerControl.on();
}
@Override
public void off() {
powerControl.off();
}
}
class Ramp implements PowerControlState,PowerControl {
private final PowerControl powerControl;
private final PowerControlState powerControlState;
public Ramp(PowerControl powerControl,PowerControlState powerControlState) {
this.powerControl = powerControl;
this.powerControlState = powerControlState;
}
@Override
public void getOn() {
powerControlState.getOn();
}
@Override
public void getOff() {
powerControlState.getOff();
}
@Override
public void on() {
powerControl.on();
}
@Override
public void off() {
powerControl.off();
}
}
Caller: 호출 하는쪽 (컨트롤러라고 봐도될듯)
Callee: 호출 당하는쪽(서비스로 봐도될듯)
훅 메서드
슈퍼클래스에서 디폴트 기능을 정의해두거나, 비워뒀다가 서브클래스에서 선택적으로 오버라이드 할 수 있도록 만들어준 메서드를 읽컫습니다