-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
1 change: 1 addition & 0 deletions
1
app/src/main/java/com/example/apptivedevstudy/debug/boj14502.kt
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
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/example/apptivedevstudy/observer/Listener.kt
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,14 @@ | ||
package com.example.apptivedevstudy.observer | ||
|
||
import androidx.lifecycle.LiveData | ||
|
||
interface LiveDataObserver<T> { | ||
fun update(data: T) | ||
} | ||
|
||
class Observer<T>(var value: T, val name: String): LiveDataObserver<T> { | ||
override fun update(data: T) { | ||
value = data | ||
println("${name}의 값이 수정되었습니다! ") | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/example/apptivedevstudy/observer/SimbleObserver.kt
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,60 @@ | ||
package com.example.apptivedevstudy.observer | ||
|
||
interface Observable { | ||
fun update() | ||
} | ||
|
||
class Armor: Observable { | ||
override fun update() { | ||
println("방어구가 데미지를 받았습니다!") | ||
} | ||
} | ||
|
||
class Weapon: Observable { | ||
override fun update() { | ||
println("무기가 데미지를 받았습니다.") | ||
} | ||
} | ||
class Subject(private var text: String) { | ||
private val events = ArrayList<Observable>() | ||
|
||
fun attach(event: Observable) { | ||
events.add(event) | ||
} | ||
|
||
fun detach(event: Observable) { | ||
events.remove(event) | ||
} | ||
|
||
fun setText(newText: String) { | ||
text = newText | ||
notifyObservers() | ||
} | ||
|
||
private fun notifyObservers() { | ||
for (event in events) { | ||
event.update() | ||
} | ||
} | ||
} | ||
|
||
fun myLambda() { | ||
println("첫 번째 람다") | ||
println("첫 번째 람다가 실행되었습니다.") | ||
} | ||
|
||
private fun main() { | ||
val subject = Subject("초기값") | ||
val armor = Armor() | ||
val weapon = Weapon() | ||
|
||
subject.attach(armor) | ||
subject.attach(weapon) | ||
|
||
subject.setText("데미지 1 받음") | ||
subject.setText("데미지 2 받음") | ||
|
||
subject.detach(armor) | ||
println("-----------------방어구 깨짐") | ||
subject.setText("데미지 3 받음") | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/example/apptivedevstudy/observer/Subject.kt
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,22 @@ | ||
package com.example.apptivedevstudy.observer | ||
|
||
class MyLiveData<T>( | ||
private var data: T | ||
) { | ||
val events = ArrayList<LiveDataObserver<T>>() | ||
|
||
fun observe(event: LiveDataObserver<T>) { | ||
events.add(event) | ||
} | ||
|
||
fun setValue(newData: T) { | ||
data = newData | ||
notifyObservers() | ||
} | ||
|
||
private fun notifyObservers() { | ||
for (event in events) { | ||
event.update(data) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/example/apptivedevstudy/observer/main.kt
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,16 @@ | ||
package com.example.apptivedevstudy.observer | ||
|
||
private fun main() { | ||
val liveData = MyLiveData("초기값") | ||
val observer1 = Observer(value = "1", "1번 옵저버") | ||
val observer2 = Observer(value = "2", "2번 옵저버") | ||
val observer3 = Observer(value = "3", "3번 옵저버") | ||
val observer4 = Observer(value = "4", "4번 옵저버") | ||
|
||
liveData.observe(observer1) | ||
liveData.observe(observer2) | ||
liveData.observe(observer3) | ||
liveData.observe(observer4) | ||
|
||
liveData.setValue("변경한 값") | ||
} |