Skip to content

Commit

Permalink
옵저버 패턴 자료 업로드
Browse files Browse the repository at this point in the history
  • Loading branch information
Cotidie committed Mar 28, 2022
1 parent 57d6270 commit 9fda915
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.example.apptivedevstudy.debug

import com.example.apptivedevstudy.debug.dataclass.Location
import com.example.apptivedevstudy.debug.dataclass.Size
import com.example.apptivedevstudy.debug.enums.Tile

class Matrix(private val rows: MutableList<Row>) {
val value: List<Row> get() = rows
val size: Size get() {
val size: Size
get() {
return Size(
width = value[0].size,
height = value.size
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/apptivedevstudy/debug/Row.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.apptivedevstudy.debug

import com.example.apptivedevstudy.debug.enums.Tile

class Row(var values: MutableList<Tile> = mutableListOf()) {
val size: Int get() = values.size

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.apptivedevstudy.debug

import com.example.apptivedevstudy.debug.dataclass.Location
import com.example.apptivedevstudy.debug.enums.Tile
import java.util.*

fun getMatrix(): Matrix {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/apptivedevstudy/observer/Listener.kt
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}의 값이 수정되었습니다! ")
}
}
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 app/src/main/java/com/example/apptivedevstudy/observer/Subject.kt
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 app/src/main/java/com/example/apptivedevstudy/observer/main.kt
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("변경한 값")
}

0 comments on commit 9fda915

Please sign in to comment.