Runtime Extensibility Flashcards

1
Q

Runtime Extensibility

A

 Extensibility is a key feature of Java
⚫ Inheritance, method overriding and interfaces provide
mechanisms to achieve this… at compile time.

 Sometimes we need runtime extensibility
⚫ Need to make your program more ‘powerful’
⚫ Without recompiling your code.
⚫ Why? What examples are there?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Common Design Pattern 1

A

 Programmer A writes library code.
 At a later date, Programmer B writes some application code and integrates codebases at compiler time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Common Design Pattern 2

A

 Programmer B writes application
 At a later date, Programmer C writes some support code that needs to be integrated into Programmer B’s application…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Analysis 1

A

 Case 1 is trivial… Case 2 less so!
▪ Inheritance allows creation of new class (Borg)
▪ Polymorphism allows instances of the Borg class to be still treated as Person instances within Programmer B’s application…
▪ But how does an instance of Borg get created?
▪ Programmer B can’t do it, as the Borg class hasn’t yet been written, and can never predict a Borg invasion.
▪ Programmer C can’t directly do it either, as it is Programmer B’s application that will likely decide when it needs to instantiate a
class!
▪ Remember Programmers do not always release source code for their programs, and then there’s the question of ownership…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Analysis 2

A

 The issue is that programmer B does not know about the exact type of Programmer C’s class…
▪ …so can’t call new().
▪ Programmer B could defer this to Programmer C however!
▪ If Programmer C writes a method for creating Borgs that meets a well defined interface defined by Programmer B…
▪ Programmer B can call it even though it hasn’t been written yet!
▪ Programmer B just needs to define a minimum set of functionality required create an instance of a future class!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Class Factories 1

A

 Solution is something known as a Class Factory

 To create a Class Factory, define:
▪ A base type that you expect future classes to be polymorphically compatible with – either a base class or an interface. In our example, this is the Person class…
▪ An abstract class or interface defining a method that creates instances and returns object references of your base type. This is
known as a Class Factory.
▪ Programmers wishing to extended the base type to provide new functionality also extend the class factory and override that
abstract method…

 Programmers can then extend your base class, and create their own implementation of a class factory.

 Later programmers then:
▪ Create new types that extends/implements the base type
▪ Implement a Class Factory meeting the given interface
▪ Register the class factory with the library code (application specific).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Dynamic Class Loading…

A

 Class factories are an elegant way to help make code extensible

 But process still occurs at compile time. i.e. Something still needs to create an instance of BorgFactory!

 Not ideal… there are many applications that need to be extended in the field – e.g. browser plugins etc.

 Java runs inside a virtual machine
⚫ Separated from the operating system and hardware
⚫ Classes are actually loaded ON DEMAND by your program…

 It is possible to control this process
⚫ Define the classes you want at RUNTIME, not compile time
⚫ Type checking is also done at runtime…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reflection

A

 Ability of a program to introspect, and reason about its own structure…

 The Java VM holds a model of all the classes in a program
 Programmatically access the description of a class
 Programmatically invoke methods (including constructors) on a class.

 The class Class implements this functionality.
 No constructor, but can obtain a Class instance by:
⚫ Invoking getClass on any instance.
⚫ Using the Class.forName(String s) method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The Class class

A

 Represents a single class.
 Many methods can be used to find out about the class
⚫ getMethods()
⚫ getFields()
⚫ getInterfaces()
⚫ getPackage()
⚫ getSuperclass()

 newInstance() – creates an instance of that class!

 Check out the Java API documentation for more details:
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

newInstance()

A

 Watch out for exceptions.
⚫ if the class cannot be found, this is now a runtime error…
⚫ ClassNotFoundException, InstatiationException, anything from constructor

 What type is the returned object?
⚫ The class of the one created (obviously)

 But how do we reference it?
⚫ Creating it is OK, but we know we often need to have references to object for later use…

⚫ Don’t KNOW the name of the class at compile time…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

instanceof

A

 We can also test the class of an instance at runtime using instanceof

<object> instanceof <class>
 Returns a boolean value. True if and only if:
⚫ object is an instance of class or
⚫ object is a subclass of class or
⚫ object implements the interface class.

 E.g.
Lecturer l;
if (l instanceof(Person))
{

}
</class></object>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Case Study: Class Loading

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly