design patterns : visitor

This commit is contained in:
haerong22
2021-12-26 18:16:56 +09:00
parent fabe51fa21
commit eb11492061
17 changed files with 175 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package visitor.after;
public class Circle implements Shape {
@Override
public void accept(Device device) {
device.print(this);
}
}

View File

@@ -0,0 +1,10 @@
package visitor.after;
public class Client {
public static void main(String[] args) {
Shape rectangle = new Rectangle();
Device device = new Pad();
rectangle.accept(device);
}
}

View File

@@ -0,0 +1,9 @@
package visitor.after;
public interface Device {
void print(Circle circle);
void print(Rectangle rectangle);
void print(Triangle triangle);
}

View File

@@ -0,0 +1,18 @@
package visitor.after;
public class Pad implements Device {
@Override
public void print(Circle circle) {
System.out.println("Print Circle to Pad");
}
@Override
public void print(Rectangle rectangle) {
System.out.println("Print Rectangle to Pad");
}
@Override
public void print(Triangle triangle) {
System.out.println("Print Triangle to Pad");
}
}

View File

@@ -0,0 +1,18 @@
package visitor.after;
public class Phone implements Device {
@Override
public void print(Circle circle) {
System.out.println("Print Circle to Phone");
}
@Override
public void print(Rectangle rectangle) {
System.out.println("Print Rectangle to Phone");
}
@Override
public void print(Triangle triangle) {
System.out.println("Print Triangle to Phone");
}
}

View File

@@ -0,0 +1,9 @@
package visitor.after;
public class Rectangle implements Shape {
@Override
public void accept(Device device) {
device.print(this);
}
}

View File

@@ -0,0 +1,7 @@
package visitor.after;
public interface Shape {
void accept(Device device);
}

View File

@@ -0,0 +1,9 @@
package visitor.after;
public class Triangle implements Shape {
@Override
public void accept(Device device) {
device.print(this);
}
}

View File

@@ -0,0 +1,18 @@
package visitor.after;
public class Watch implements Device {
@Override
public void print(Circle circle) {
System.out.println("Print Circle to Watch");
}
@Override
public void print(Rectangle rectangle) {
System.out.println("Print Rectangle to Watch");
}
@Override
public void print(Triangle triangle) {
System.out.println("Print Triangle to Watch");
}
}

View File

@@ -0,0 +1,12 @@
package visitor.before;
public class Circle implements Shape {
@Override
public void printTo(Device device) {
if (device instanceof Phone) {
System.out.println("print Circle to phone");
} else if (device instanceof Watch) {
System.out.println("print Circle to watch");
}
}
}

View File

@@ -0,0 +1,10 @@
package visitor.before;
public class Client {
public static void main(String[] args) {
Shape rectangle = new Rectangle();
Device device = new Phone();
rectangle.printTo(device);
}
}

View File

@@ -0,0 +1,4 @@
package visitor.before;
public interface Device {
}

View File

@@ -0,0 +1,4 @@
package visitor.before;
public class Phone implements Device{
}

View File

@@ -0,0 +1,13 @@
package visitor.before;
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");
}
}
}

View File

@@ -0,0 +1,7 @@
package visitor.before;
public interface Shape {
void printTo(Device device);
}

View File

@@ -0,0 +1,14 @@
package visitor.before;
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");
}
}
}

View File

@@ -0,0 +1,4 @@
package visitor.before;
public class Watch implements Device{
}