diff --git a/zero/document/proxy.md b/zero/document/proxy.md new file mode 100644 index 0000000..1ed99d0 --- /dev/null +++ b/zero/document/proxy.md @@ -0,0 +1,43 @@ +eal Object에 대해서 실제로 필요할 때 instance가 생성되고 실제 작업이 진행될 수 있도록 하기 위해 적용되는 패턴 + +**데코레이터(Decorator)**데코레이터 패턴은 runtime에 real Object에 기능을 확장하고 싶을 때 사용 + +### 좀더 간단하게 + +**프록시** : 대신한다. + +**데코레이터** : 합쳐서 한다. + +## 다이나믹 프록시 + +자바에서 지원해주는 프록시 객체 + +런타임시 특정 객체를 생성해서 프록시의 용도로 사용 할 수 있다. + +### 기본 구성 + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9a8d12e9-4647-49b6-85ee-9733576a4794/Untitled.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9a8d12e9-4647-49b6-85ee-9733576a4794/Untitled.png) + +### 사용예제 1 (네트워크 리퀘스트) + +예제 : 서로다른 페이즈에서 네트워크 콜을 할건데 앞에 url을 다르게 해주고싶다. + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dcfe4099-cfd6-48a7-8695-0d5385be5512/_2021-07-04__7.17.09.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dcfe4099-cfd6-48a7-8695-0d5385be5512/_2021-07-04__7.17.09.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/48976e63-0536-43d5-ab6d-e5600d66cc4c/_2021-07-04__7.27.08.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/48976e63-0536-43d5-ab6d-e5600d66cc4c/_2021-07-04__7.27.08.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6ef97b58-3bc4-43fe-a42b-f470e89a747c/_2021-07-04__7.18.34.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6ef97b58-3bc4-43fe-a42b-f470e89a747c/_2021-07-04__7.18.34.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/310869a2-7643-4488-9eb3-1fb9662d1de4/_2021-07-04__7.18.21.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/310869a2-7643-4488-9eb3-1fb9662d1de4/_2021-07-04__7.18.21.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/115e0f2d-ffb4-41e4-9e24-12a3d496a764/Untitled.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/115e0f2d-ffb4-41e4-9e24-12a3d496a764/Untitled.png) + +### 사용예제 2 (네트워크 리스폰스) + +예제 : 데이터를 받아와서 특정 자료구조에 맵핑해서 반환하고 싶다. + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/75f982c7-a62a-47df-b6ae-5d9610486bcc/_2021-07-04__7.27.35.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/75f982c7-a62a-47df-b6ae-5d9610486bcc/_2021-07-04__7.27.35.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f83fbc7f-a394-4e5a-952c-e4385af02f58/Untitled.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f83fbc7f-a394-4e5a-952c-e4385af02f58/Untitled.png) + +![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8f8b3145-33ea-464b-af35-68943e4dc5fe/_2021-07-04__7.28.18.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8f8b3145-33ea-464b-af35-68943e4dc5fe/_2021-07-04__7.28.18.png) diff --git a/zero/src/main/kotlin/proxy/BuildConfig.kt b/zero/src/main/kotlin/proxy/BuildConfig.kt new file mode 100644 index 0000000..2b30a80 --- /dev/null +++ b/zero/src/main/kotlin/proxy/BuildConfig.kt @@ -0,0 +1,9 @@ +package proxy + +enum class BuildConfig(val prefix: String) { + SANDBOX("https://sandbox/"), BETA("https://beta/"), REAL("https://real/"); + + companion object { + fun currentPhase() = values().random() + } +} \ No newline at end of file diff --git a/zero/src/main/kotlin/proxy/Create.kt b/zero/src/main/kotlin/proxy/Create.kt new file mode 100644 index 0000000..a258dcf --- /dev/null +++ b/zero/src/main/kotlin/proxy/Create.kt @@ -0,0 +1,21 @@ +package proxy + +import java.lang.reflect.Proxy + +inline fun createApi() = Proxy.newProxyInstance( + ClassLoader.getSystemClassLoader(), + arrayOf(TestApiCall::class.java) +) { _, method, _ -> + + val annotation = method.getAnnotation(Test::class.java) + + //api 요청 + println("${BuildConfig.currentPhase().prefix}${annotation.data}") + + //받아온 결과를 통해 데이터 맵핑 (여기서는 실제 api콜을 하지 않기때문에 임의로 작성) + if (annotation.data.contains("kakao")) { + Response("kakao", "카카오엔터프라이즈", "zero.dev") + } else { + Response("google", "구글코리아", "로버트 마틴") + } +} as T \ No newline at end of file diff --git a/zero/src/main/kotlin/proxy/TestApiCall.kt b/zero/src/main/kotlin/proxy/TestApiCall.kt new file mode 100644 index 0000000..fbba8d9 --- /dev/null +++ b/zero/src/main/kotlin/proxy/TestApiCall.kt @@ -0,0 +1,15 @@ +package proxy + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +annotation class Test(val data: String) + +interface TestApiCall { + @Test("www.kakao.com") + fun callKakao(): Response + + @Test("www.google.com") + fun callGoogle(): Response +} + +data class Response(val pageName: String, val text: String, val author: String) \ No newline at end of file diff --git a/zero/src/test/kotlin/InvocationHandlerTest.kt b/zero/src/test/kotlin/InvocationHandlerTest.kt new file mode 100644 index 0000000..acea379 --- /dev/null +++ b/zero/src/test/kotlin/InvocationHandlerTest.kt @@ -0,0 +1,27 @@ +import org.junit.Before +import org.junit.Test +import proxy.TestApiCall +import proxy.createApi + +class InvocationHandlerTest { + private lateinit var api: TestApiCall + + @Before + fun setUp() { + api = createApi() + } + + @Test + fun phaseCallKakao() { + val res = api.callKakao() + println(res) + assert(res.author == "zero.dev") + } + + @Test + fun phaseCallGoogle() { + val res = api.callGoogle() + println(res) + assert(res.author == "로버트 마틴") + } +} \ No newline at end of file diff --git "a/\353\260\234\355\221\234\354\236\220\353\243\214 \354\230\244\354\240\204 1.49.06/Observer/zero.md" "b/\353\260\234\355\221\234\354\236\220\353\243\214 \354\230\244\354\240\204 1.49.06/Observer/zero.md" new file mode 100644 index 0000000..3a11b04 --- /dev/null +++ "b/\353\260\234\355\221\234\354\236\220\353\243\214 \354\230\244\354\240\204 1.49.06/Observer/zero.md" @@ -0,0 +1,31 @@ +Subject : 발행자(Observable) + +- 데이터 관리 +- 옵저버 관리 + +Observer : 구독자 + +- 구독받아 update() + +# 옵저버 패턴의 간단한 설명 + +간단하게 Observable이 observer의 리스트를 합성관계로 들고있고 + +데이터의 변화마다 리스트에게 뿌려주는 형태 + +### 나는 잠깐 생각해 보았다. + +스레드 스위칭을 지원하지 않으면 + +옵저버패턴은 **동기로** 동작한다. + +스크린샷 2021-04-30 오후 5 05 48 + + +# 상속(is-a) 말고 합성(has-a)을 + +사용해야 하는 이유 + +- 변경에 취약한 코드를 나을 수 있다. (부모가 바뀌면 자식 다 바뀜) +- 결합도가 무지무지하게 높아진다. +- 클래스가 엄청 생성 됨