Skip to content

Commit

Permalink
add DynamicProxy1
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 2, 2021
1 parent 06f1caa commit 018b713
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@
- [ProxyDesignPattern2](./src/main/scala/ScalaBasic/ProxyDesignPattern2) : Remote Proxy -- RMI demo (remote method invocation)
- [remote-method-invocation-in-java](https://www.geeksforgeeks.org/remote-method-invocation-in-java/)
- RMI : via OOP, doing remote call (method)
- [DynamicProxy1](./src/main/scala/ScalaBasic/DynamicProxy1)

12. Scala Script example
- [DirectoryOP](./src/main/scala/ScalaBasic/DirectoryOP.scala) - Scala `Directory OP` example
Expand Down
42 changes: 42 additions & 0 deletions src/main/scala/ScalaBasic/DynamicProxy1/MatchService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

import java.lang.reflect.Proxy

class MatchService {

// create a person
val tom = getPersonInfo("tom", "male", "cook")

// get our own proxy (represents the tom instance)
val OwnerProxy = getOwnerProxy(tom)

println("Name = " + OwnerProxy.gatName()) // tom
//println("Interests = " + OwnerProxy.gatInterests()) // cook

OwnerProxy.setInterests("hiking")
//println("Interests = " + OwnerProxy.gatInterests()) // hiking

OwnerProxy.setScore(100)
//println("Score = " + OwnerProxy.gatScore()) // still 0

// method

def getPersonInfo(name:String, gender:String, interests:String):PersonBean = {
val person = new PersonBeanImplement()
person.setName(name)
person.setGender(gender)
person.setInterests(interests)

person
}

def getOwnerProxy(person: PersonBean):PersonBean = {
Proxy.newProxyInstance(
person.getClass().getClassLoader,
person.getClass().getInterfaces(),
new OwnerInvocationHandler(person)
).asInstanceOf[PersonBean]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

import java.lang.reflect.{InvocationHandler, Method}

// proxy used by OTHER USERS
class NonOwnerInvocationHandler extends InvocationHandler{

// attr
var person:PersonBean = _

// constructor
def this(person:PersonBean) {
this
this.person = person
}

// method
@throws(classOf[Throwable])
override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = {

// if `get` method, then we invoke directly
if (method.getName().startsWith("get")){
return method.invoke(person)

// *** OTHER USERS can use below methods (setScore)
} else if (method.getName().equals("setScore")){
return method.invoke(person, Integer.valueOf(args(0).toString))

// OTHER USERS CAN'T use set method
} else if (method.getName().equals("set")){
return new IllegalAccessException()
}
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

import java.lang.reflect.{InvocationHandler, Method}

// proxy used by ourselves
class OwnerInvocationHandler extends InvocationHandler{

// attr
// the instance is going to be called
var person:PersonBean = _

// constructor
def this(person:PersonBean){
this
this.person = person
}

// method
/**
* 1) the proxy here will work with OwnerInvocationHandler together
*/
@throws(classOf[Throwable])
override def invoke(proxy: scala.Any, method: Method, args: Array[AnyRef]): AnyRef = {

// if `get` method, then we invoke directly
if (method.getName().startsWith("get")){
return method.invoke(person)

// we CAN NOT score ourselves (setScore)
} else if (method.getName().equals("setScore")){
return new IllegalAccessException()

// if `set` method, then we invoke directly
} else if (method.getName().equals("set")){
return method.invoke(person, args(0).toString)
}
null
}
}
24 changes: 24 additions & 0 deletions src/main/scala/ScalaBasic/DynamicProxy1/PersonBean.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

// a scala trait, similar as interface in java
trait PersonBean {

// getter, setter
def gatName():String

def gatGender():String

def gatInterests:String

def gatScore():Int

def setName(name:String)

def setGender(gender:String)

def setInterests(interests:String)

def setScore(score:Int)
}
48 changes: 48 additions & 0 deletions src/main/scala/ScalaBasic/DynamicProxy1/PersonBeanImplement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

// the class is going to be used
class PersonBeanImplement extends PersonBean {

// attr
var name = ""
var gender = ""
var interests = ""
var score:Int = _

// method
override def gatName(): String = {
name
}

override def gatGender(): String = {
gender
}

// can be used by ourselves, but CAN'T be used by OTHER users
override def gatInterests: String = {
interests
}

override def gatScore(): Int = {
score
}

override def setName(name: String): Unit = {
this.name = name
}

override def setGender(gender: String): Unit = {
this.gender = gender
}

override def setInterests(interests: String): Unit = {
this.interests = interests
}

// can't be used by ourselves, but can be used by OTHER users
override def setScore(score: Int): Unit = {
this.score = score
}
}
7 changes: 7 additions & 0 deletions src/main/scala/ScalaBasic/DynamicProxy1/app.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ScalaBasic.DynamicProxy1

// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver

object app extends App {
var mMatchService: MatchService = new MatchService()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ object MyRemoteClient {
// for (i <- 1 to 5){
// new MyRemoteClient().go()
// }

}
}

0 comments on commit 018b713

Please sign in to comment.