From 477323d1dd9cb97232555b67592134aa278bebda Mon Sep 17 00:00:00 2001 From: norangebit Date: Mon, 23 Mar 2020 17:55:33 +0100 Subject: [PATCH] add dril02 -exercise3 --- .../kotlin/drills/drill02/exercise2/Rev.kt | 5 +++-- .../drills/drill02/exercise3/LogHandler.kt | 11 +++++++++++ .../kotlin/drills/drill02/exercise3/Main.kt | 15 +++++++++++++++ .../kotlin/drills/drill02/exercise3/Math.kt | 18 ++++++++++++++++++ .../drills/drill02/exercise3/MathFactory.kt | 15 +++++++++++++++ src/main/kotlin/util/rmi/Client.kt | 4 ++-- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/drills/drill02/exercise3/LogHandler.kt create mode 100644 src/main/kotlin/drills/drill02/exercise3/Main.kt create mode 100644 src/main/kotlin/drills/drill02/exercise3/Math.kt create mode 100644 src/main/kotlin/drills/drill02/exercise3/MathFactory.kt diff --git a/src/main/kotlin/drills/drill02/exercise2/Rev.kt b/src/main/kotlin/drills/drill02/exercise2/Rev.kt index 12592dc..b38d2a7 100644 --- a/src/main/kotlin/drills/drill02/exercise2/Rev.kt +++ b/src/main/kotlin/drills/drill02/exercise2/Rev.kt @@ -1,14 +1,15 @@ package drills.drill02.exercise2 +import java.io.Serializable import java.rmi.Remote import java.rmi.RemoteException import java.rmi.server.UnicastRemoteObject interface Rev : Remote { @Throws(RemoteException::class) - fun executeTask(task: () -> T): T + fun executeTask(task: () -> T): T } class RevImpl : Rev, UnicastRemoteObject() { - override fun executeTask(task: () -> T): T = task() + override fun executeTask(task: () -> T): T = task() } diff --git a/src/main/kotlin/drills/drill02/exercise3/LogHandler.kt b/src/main/kotlin/drills/drill02/exercise3/LogHandler.kt new file mode 100644 index 0000000..1e740e5 --- /dev/null +++ b/src/main/kotlin/drills/drill02/exercise3/LogHandler.kt @@ -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?): Any { + println("log: method '$method' has been invoked") + return method?.invoke(target, *(args ?: arrayOf())) ?: Unit + } +} diff --git a/src/main/kotlin/drills/drill02/exercise3/Main.kt b/src/main/kotlin/drills/drill02/exercise3/Main.kt new file mode 100644 index 0000000..a9bbfd8 --- /dev/null +++ b/src/main/kotlin/drills/drill02/exercise3/Main.kt @@ -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)}.") +} diff --git a/src/main/kotlin/drills/drill02/exercise3/Math.kt b/src/main/kotlin/drills/drill02/exercise3/Math.kt new file mode 100644 index 0000000..a9aa1fe --- /dev/null +++ b/src/main/kotlin/drills/drill02/exercise3/Math.kt @@ -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 +} diff --git a/src/main/kotlin/drills/drill02/exercise3/MathFactory.kt b/src/main/kotlin/drills/drill02/exercise3/MathFactory.kt new file mode 100644 index 0000000..84ed984 --- /dev/null +++ b/src/main/kotlin/drills/drill02/exercise3/MathFactory.kt @@ -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() + } +} diff --git a/src/main/kotlin/util/rmi/Client.kt b/src/main/kotlin/util/rmi/Client.kt index 35b611e..14da997 100644 --- a/src/main/kotlin/util/rmi/Client.kt +++ b/src/main/kotlin/util/rmi/Client.kt @@ -20,7 +20,7 @@ class Client(private val handler: () -> Unit) { } companion object { - fun lookup(name: String, port: Int = 4242): Remote = - Naming.lookup("rmi://localhost:$port/$name") + fun lookup(name: String, host: String = "localhost", port: Int = 4242): Remote = + Naming.lookup("rmi://$host:$port/$name") } }