add dril02 -exercise3
This commit is contained in:
parent
ac35528eab
commit
477323d1dd
@ -1,14 +1,15 @@
|
|||||||
package drills.drill02.exercise2
|
package drills.drill02.exercise2
|
||||||
|
|
||||||
|
import java.io.Serializable
|
||||||
import java.rmi.Remote
|
import java.rmi.Remote
|
||||||
import java.rmi.RemoteException
|
import java.rmi.RemoteException
|
||||||
import java.rmi.server.UnicastRemoteObject
|
import java.rmi.server.UnicastRemoteObject
|
||||||
|
|
||||||
interface Rev : Remote {
|
interface Rev : Remote {
|
||||||
@Throws(RemoteException::class)
|
@Throws(RemoteException::class)
|
||||||
fun <T> executeTask(task: () -> T): T
|
fun <T : Serializable> executeTask(task: () -> T): T
|
||||||
}
|
}
|
||||||
|
|
||||||
class RevImpl : Rev, UnicastRemoteObject() {
|
class RevImpl : Rev, UnicastRemoteObject() {
|
||||||
override fun <T> executeTask(task: () -> T): T = task()
|
override fun <T : Serializable> executeTask(task: () -> T): T = task()
|
||||||
}
|
}
|
||||||
|
11
src/main/kotlin/drills/drill02/exercise3/LogHandler.kt
Normal file
11
src/main/kotlin/drills/drill02/exercise3/LogHandler.kt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package drills.drill02.exercise3
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationHandler
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
|
||||||
|
class LogHandler(private val target: Any) : InvocationHandler {
|
||||||
|
override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any {
|
||||||
|
println("log: method '$method' has been invoked")
|
||||||
|
return method?.invoke(target, *(args ?: arrayOf())) ?: Unit
|
||||||
|
}
|
||||||
|
}
|
15
src/main/kotlin/drills/drill02/exercise3/Main.kt
Normal file
15
src/main/kotlin/drills/drill02/exercise3/Main.kt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package drills.drill02.exercise3
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val factory = MathFactory()
|
||||||
|
|
||||||
|
val math = factory.create()
|
||||||
|
val mathLog = factory.create(logger = true)
|
||||||
|
|
||||||
|
println("Enter two numbers:")
|
||||||
|
val a = readLine()?.toFloat() ?: 0f
|
||||||
|
val b = readLine()?.toFloat() ?: 0f
|
||||||
|
|
||||||
|
println("The sum is ${math.add(a, b)}.")
|
||||||
|
println("The sum is ${mathLog.add(a, b)}.")
|
||||||
|
}
|
18
src/main/kotlin/drills/drill02/exercise3/Math.kt
Normal file
18
src/main/kotlin/drills/drill02/exercise3/Math.kt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package drills.drill02.exercise3
|
||||||
|
|
||||||
|
interface Math {
|
||||||
|
fun add(a: Float, b: Float): Float
|
||||||
|
fun sub(a: Float, b: Float): Float
|
||||||
|
fun mul(a: Float, b: Float): Float
|
||||||
|
fun div(a: Float, b: Float): Float
|
||||||
|
}
|
||||||
|
|
||||||
|
class MathImpl : Math {
|
||||||
|
override fun add(a: Float, b: Float): Float = a + b
|
||||||
|
|
||||||
|
override fun sub(a: Float, b: Float): Float = a - b
|
||||||
|
|
||||||
|
override fun mul(a: Float, b: Float): Float = a * b
|
||||||
|
|
||||||
|
override fun div(a: Float, b: Float): Float = a / b
|
||||||
|
}
|
15
src/main/kotlin/drills/drill02/exercise3/MathFactory.kt
Normal file
15
src/main/kotlin/drills/drill02/exercise3/MathFactory.kt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package drills.drill02.exercise3
|
||||||
|
|
||||||
|
import java.lang.reflect.Proxy
|
||||||
|
|
||||||
|
class MathFactory {
|
||||||
|
fun create(logger: Boolean = false): Math {
|
||||||
|
return if (logger) {
|
||||||
|
Proxy.newProxyInstance(
|
||||||
|
this::class.java.classLoader,
|
||||||
|
arrayOf(Math::class.java),
|
||||||
|
LogHandler(MathImpl())
|
||||||
|
) as Math
|
||||||
|
} else MathImpl()
|
||||||
|
}
|
||||||
|
}
|
@ -20,7 +20,7 @@ class Client(private val handler: () -> Unit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun lookup(name: String, port: Int = 4242): Remote =
|
fun lookup(name: String, host: String = "localhost", port: Int = 4242): Remote =
|
||||||
Naming.lookup("rmi://localhost:$port/$name")
|
Naming.lookup("rmi://$host:$port/$name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user