0

In the Strategy pattern, implemented like this:

object StrategyPattern {

  def add(a: Int, b: Int) = a + b
  def subtract(a: Int, b: Int) = a - b
  def multiply(a: Int, b: Int) = a * b

  def execute(callback:(Int, Int) => Int, x: Int, y: Int) = callback(x, y)

def main(args: Array[String]) {
  println("Add:      " + execute(add, 3, 4))
  println("Subtract: " + execute(subtract, 3, 4))
  println("Multiply: " + execute(multiply, 3, 4))
  }
}

I wanted to know (and find how to understand also the other cases, if there is a good reference for the types/forms of the binding times) if the binding time of methods add, substract, and multiply is "construction time" (if I can say so), or at runtime?

3
  • It's not quite clear what you're asking. Commented Jun 9, 2015 at 19:14
  • @Aaron Novstrup, I just wanted to know which is the binding time of these three methods add, substract and multiply? Commented Jun 10, 2015 at 8:31
  • @AaronNovstrup, for example: the binding time of object Something { final val THING: Boolean = true } is compile time. Commented Jun 10, 2015 at 11:38

1 Answer 1

1

The simple answer is that (for concrete classes) method definitions are bound to method names at compile time, just as they are in Java. Your add method, for example, is completely equivalent to this Java definition:

public int add(int a, int b) {
    return a + b;
}

Non-final methods

If you're analyzing the binding time of a non-final method from the perspective of a call-site where the concrete class is not known statically, then the method name would be considered to have runtime binding to its implementation (due to subclassing/overriding).

Dynamic Classloading

The simple answer is close to the truth, but dynamic classloading in the JVM complicates matters a bit. Because of dynamic classloading, method definitions are technically bound to fully qualified names (e.g., my.pkg.StrategyPattern.add) at runtime. It's certainly possible to have alternative implementations of a my.package.StrategyPattern module and to choose among them dynamically (by loading the corresponding class file(s)).

Of course, this distinction is only relevant to code outside of the compilation unit containing the StrategyPattern definition. Within the compilation unit, methods would always be considered bound at compile time.

Strategy

Since you're asking about the strategy pattern, I guess you have something else in mind? If you're asking whether you can select among the "strategies" at runtime, you can:

val op: (Int, Int) => Int = 
  if (args(0) == "+") add
  else if (args(0) == "-") subtract
  else multiply
execute(op, 3, 4)

In this case, op is bound to a "strategy" function at runtime, but add, subtract, and multiply are still bound to their definitions at compile time.

It's an implementation detail that each of these methods is also associated with an anonymous Function2 class at compile time, and that the appropriate class is instantiated at runtime based on the outcome of the conditional expressions. This detail really isn't relevant to the binding time analysis, since the meaning of the add, subtract, and multiply identifiers is fixed at compile time.

Sign up to request clarification or add additional context in comments.

3 Comments

you have explained to me exactly what I was searching for. wow. Thanks for your valuable time. I was searching also if I can find any reading material, possibly with examples, how I can undestand by myself also most of the cases ...
I don't know of specific resources that address binding time directly... I guess you could try the specifications for the Java virtual machine, Java language, and Scala language. My answer is based on my knowledge of the JVM and of how Scala gets compiled to JVM bytecode. Is there a specific programming problem that is motivating your search?
it's not a specific problem. Actually, I came in a situation where my work get related in some points so strong with the binding time of values, atributes, methods, ... and I stuck there :(,... "intuitively" I can distinguish most of the time the extreme cases when something is bound during the compile time or runtime, but that's it...I tried to search some useful explenation through the internet and to learn from it but it was very hard (impossible) to find a dedicated article! Thanks a lot, and I really appreciate your comments!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.