기존 코드를 건드리지 않고 새로운 패턴을 추가.
public class Triangle implements Shape {
@Override
public void printTo(Device device) {
if(Device instanceof Phone) {
//log
} else if(device instanceof Watch) {
//log
}
}
}
public class Rectangle implements Shape {
@Override
public void printTo(Device device) {
if(Device instanceof Phone) {
//log
} else if(device instanceof Watch) {
//log
}
}
}
public interface Shape {
void printTo(Device device);
}
public class Circle implements Shape {
@Override
public void accept(Device device) {
device.print(this);
}
}
public interface Device {
void print(Circle circle);
void print(Rectangle rectangle);
void print(Triangle triangle);
}
public class Phone implements Device {
@Override
public void print(Circle circle) {
//log
}
@Override
public void print(Triangle triangle) {
//log
}
@Override
public void print(Triangle Triangle) {
//log
}
}
public class Client {
public static void main(String[]] args) {
Shape rectangle = new Rectangle();
Deviece device = new Pad();
rectangle.accept(device);
}
}