-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from jiwon83/main
한지원 - 디자인 패턴 - 프록시 예제 코드
- Loading branch information
Showing
8 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
241020/proxy/demo/src/main/java/com/example/demo/proxy/BubbleMiniGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.demo.proxy; | ||
|
||
public class BubbleMiniGame implements CanMiniGame { | ||
|
||
private String userName; | ||
|
||
public BubbleMiniGame() { | ||
initialWork(); | ||
} | ||
|
||
private void initialWork() { | ||
try { | ||
Thread.sleep(1000); | ||
System.out.println("Bubble 미니 게임 초기화 작업이 완료됐습니다."); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
@Override | ||
public void welcomeMessage() { | ||
System.out.println(userName+ "님, Bubble 미니 게임에 오신 것을 환영합니다 : ) "); | ||
} | ||
|
||
@Override | ||
public void playGame() { | ||
System.out.println("Bubble 미니 게임을 시작합니다."); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
241020/proxy/demo/src/main/java/com/example/demo/proxy/CanMiniGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.demo.proxy; | ||
|
||
public interface CanMiniGame { | ||
|
||
void setUserName(String userName); | ||
|
||
void welcomeMessage(); | ||
|
||
void playGame(); | ||
} |
36 changes: 36 additions & 0 deletions
36
241020/proxy/demo/src/main/java/com/example/demo/proxy/MarioMiniGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.example.demo.proxy; | ||
|
||
public class MarioMiniGame implements CanMiniGame { | ||
|
||
private String userName; | ||
|
||
public MarioMiniGame() { | ||
initialWork(); | ||
} | ||
|
||
private void initialWork() { | ||
try { | ||
System.out.println("Mario 미니 게임 초기화 작업은 조금 더 오래 걸립니다. 조금만 기다려 주세요!"); | ||
Thread.sleep(20000); | ||
System.out.println("Mario 미니 게임 초기화 작업이 완료됐습니다."); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
@Override | ||
public void welcomeMessage() { | ||
System.out.println(userName+ "님, Mario 미니 게임에 오신 것을 환영합니다 : ) "); | ||
} | ||
|
||
@Override | ||
public void playGame() { | ||
System.out.println("Mario 미니 게임을 시작합니다."); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
241020/proxy/demo/src/main/java/com/example/demo/proxy/MiniGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.demo.proxy; | ||
|
||
public class MiniGame implements CanMiniGame { | ||
|
||
private String userName; | ||
|
||
public MiniGame() { | ||
initialWork(); | ||
} | ||
public MiniGame(String userName) { | ||
this.userName = userName; | ||
initialWork(); | ||
} | ||
|
||
private void initialWork() { | ||
try { | ||
Thread.sleep(3000); | ||
System.out.println("MiniGame 초기화 작업이 완료됐습니다."); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
@Override | ||
public void welcomeMessage() { | ||
System.out.println(userName+ "님, MiniGame 에 오신 것을 환영합니다 : ) "); | ||
} | ||
|
||
@Override | ||
public void playGame() { | ||
System.out.println("START!!"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
241020/proxy/demo/src/main/java/com/example/demo/proxy/MiniGameProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.demo.proxy; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class MiniGameProxy implements CanMiniGame { | ||
|
||
// private MiniGame realMiniGame; | ||
private CanMiniGame realMiniGame; | ||
private String className; // Real MiniGame 객체의 클래스 이름 | ||
private String userName; | ||
|
||
public MiniGameProxy(String className) { | ||
this.realMiniGame = null; // 객체 생성을 지연 | ||
this.className = className; | ||
this.userName = "null"; | ||
} | ||
|
||
@Override | ||
public void setUserName(String userName) { | ||
if (realMiniGame == null) { | ||
this.userName = userName; | ||
}else{ | ||
realMiniGame.setUserName(userName); | ||
} | ||
} | ||
|
||
@Override | ||
public void welcomeMessage() { | ||
if (realMiniGame == null){ | ||
System.out.println(userName+ "님, "+ className + " 에 오신 것을 환영합니다 : ) "); | ||
}else{ | ||
realMiniGame.welcomeMessage(); | ||
} | ||
} | ||
|
||
@Override | ||
public void playGame() { | ||
if (realMiniGame == null){ | ||
try { | ||
realMiniGame = (CanMiniGame) Class.forName("com.example.demo.proxy."+ className).getDeclaredConstructor().newInstance(); | ||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | | ||
InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
realMiniGame.playGame(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
241020/proxy/demo/src/main/java/com/example/demo/proxy/PuzzleMiniGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.demo.proxy; | ||
|
||
public class PuzzleMiniGame implements CanMiniGame{ | ||
|
||
private String userName; | ||
|
||
public PuzzleMiniGame() { | ||
initialWork(); | ||
} | ||
|
||
private void initialWork() { | ||
try { | ||
Thread.sleep(500); | ||
System.out.println("Puzzle 미니 게임 초기화 작업이 완료됐습니다."); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
@Override | ||
public void welcomeMessage() { | ||
System.out.println(userName+ "님, Puzzle 미니 게임에 오신 것을 환영합니다 : ) "); | ||
} | ||
|
||
@Override | ||
public void playGame() { | ||
System.out.println("Puzzle 미니 게임을 시작합니다."); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
241020/proxy/demo/src/test/java/com/example/demo/proxy/MiniGameProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.demo.proxy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class MiniGameProxyTest { | ||
|
||
@Test | ||
void miniGameTestProxy(){ | ||
CanMiniGame miniGame = new MiniGameProxy("MiniGame"); | ||
miniGame.setUserName("쥴리"); // 실제 객체는 생성되지 않았다. | ||
miniGame.welcomeMessage(); | ||
|
||
// 사용자 입력에 따라 게임 실행 | ||
String userInput = "start"; | ||
|
||
if ("start".equals(userInput)) { | ||
miniGame.playGame(); | ||
} | ||
} | ||
|
||
@Test | ||
void onlyWelcomeProxy(){ | ||
CanMiniGame miniGame = new MiniGameProxy("MiniGame"); | ||
miniGame.setUserName("쥴리"); // 실제 객체는 생성되지 않았다. | ||
miniGame.welcomeMessage(); | ||
} | ||
|
||
@Test | ||
void proxyTestWhenVariousMiniGame(){ | ||
CanMiniGame miniGame = new MiniGameProxy("PuzzleMiniGame"); | ||
miniGame.setUserName("쥴리"); // 실제 객체는 생성되지 않았다. | ||
miniGame.welcomeMessage(); | ||
miniGame.playGame(); | ||
|
||
CanMiniGame miniGame2 = new MiniGameProxy("BubbleMiniGame"); | ||
miniGame2.setUserName("나띠"); // 실제 객체는 생성되지 않았다. | ||
miniGame2.welcomeMessage(); | ||
miniGame2.playGame(); | ||
|
||
CanMiniGame miniGame3 = new MiniGameProxy("MarioMiniGame"); | ||
miniGame3.setUserName("벨"); // 실제 객체는 생성되지 않았다. | ||
miniGame3.welcomeMessage(); | ||
miniGame3.playGame(); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
241020/proxy/demo/src/test/java/com/example/demo/proxy/MiniGameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example.demo.proxy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class MiniGameTest { | ||
|
||
@Test | ||
void gameTest(){ | ||
|
||
MiniGame miniGame = new MiniGame(); | ||
miniGame.setUserName("쥴리"); | ||
miniGame.welcomeMessage(); | ||
|
||
// 사용자 입력에 따라 게임 실행 | ||
String userInput = "start"; | ||
|
||
if ("start".equals(userInput)) { | ||
miniGame.playGame(); | ||
} | ||
} | ||
|
||
@Test | ||
void onlyWelcomeProxy(){ | ||
CanMiniGame miniGame = new MiniGame(); | ||
miniGame.setUserName("쥴리"); // 실제 객체는 생성되지 않았다. | ||
miniGame.welcomeMessage(); | ||
} | ||
} |