April 11, 2022
01.사용되는 상황
02.코드로 확인해보기
02.1 여러디바이스에 추가하고 싶은 경우
03.방문자 패턴이란?
public interface Device{
}
public Phone implements Device{
}
public Watch implements Device{
}
public class Client{
public static void main(String[] args)
{
Shape rectangle = new Rectangle();
rectangle.printTo(new Phone());
}
}
public interface Shape {
void printTo(Device device);
}// 모든 디바이스에 쓰기 위함
public class Rectangle implements Shape{
@Override
public void printTo(Device device){
if(device instanceof Phone){
System.out.println("print Rectangle to Phone");
}else if(device instanceof Watch){
System.out.println("print Rectangle to Watch");
}
}
}
public class Triangle implements Shape{
@Override
public void printTo(Device device){
if(device instanceof Phone){
System.out.println("print Triangle to Phone");
}else if(device instanceof Watch){
System.out.println("print Triangle to Watch");
}
}
}
기존 코드를 변경하기 않고 새로운 기능을 추가하는 방법
Dispatch는 어떤 다형성이나 분배, 배치