Skip to main content

Static polymorphism

The AgilEntity framework has a db vendor agnostic sub api that allows the client code for the platform to interact with the clusters loaded database without knowing the specific vendor type of the system in place. This enables the client programmers to write standard ANSI compliant SQL to perform calls on the the various tables added to the datastore. The design of the datastore promotes a relational model between Entities that are managed in an Entity relations table. Other Entities have specific mapping relationships as well but all db access occurs through the API. This is done using an abstraction of the db accessing methods through a class called CreateTable. The CreateTable base class , which is abstract as defined using the java OO syntax is extended by sub classes that implement the specific functionality that is required to mediate specific vendor databases. The base class enforces implemenations for methods that perform several of the following functions that tend to be database variable.
  • date/time conversion and formatting
  • editColumn
  • addRow
  • createTable
  • select for Vendor

These functions will vary depending on the vendor in use. For example, the date time format defaults vary significantly between mysql , mssql server and oracle. By building the change into a method in the db vendor class and calling those polymorphically we get automatic db agnostic operation. Some of the methods are purely abstract , others are abstract and static these methods allow invoking subclass specific implemenations without instantiating instances of the subclass objects. This is done using the static polymorphism pattern that I describe below:


The java API allows deep control over how methods and classes are called, it also allows us to define how the jvm loads up class path locations for reference of class files. This class loading mechanism is the power behind dynamic run time extension capabilities of many java application servers. These abilities are part of what java calls the reflection API. Static polymorphism uses the Class class to instantiate an object from its FQCN (fully qualified class name). This allows static polymorphism to proceed by having the a concrete static base class method call an implementation of a FQCN named subclass of that base class hieararchy...thus effecting a polymorphic call on a static method.



1) Define the base class of your inheritance hieararchy. In my case it was the "CreateTable" class which was defined as a public and abstract class (to allow both concrete and abstract implemenation signatures which is important to enable the static polymorphism.)

public abstract class CreateTable {

//empty arg abstract method goes here ...see below.

}


2) define the empty arg. abstract method signature for the method we require to override via polymorphism
in inheriting classes. This should be public or protected so that the subclasses can see it and thus be constrained to provide implemenations. The reason the argument is empty is that we just want to extract the db specific version of the function being requested. In this case the db specific default time pattern. We have the method name but we now need a way to statically select the class which is invoking that method. That is where the abstract FQCN argument method comes in as shown in 3.

public abstract class CreateTable {


public abstract String timePattern() ;

//static FQCN argument method with string argument goes below...

}


3) The implemented string argument version of timePattern() is shown. The green call to a method "getVendorInstance(fqcn)" is where the reflection happens as shown in the method implemenation.


public abstract class CreateTable {


public abstract String timePattern() ;

public static String timePattern(String fqcn) {
try {
//Extract in instance of the currently loaded derived createTable class.
CreateTable vinny = this.getVendorInstance(fqcn);
return vinny.timePattern();
} catch (GVIOException efio) {
throw new EntegraDBAccessException("IO Error from CreateTable static insertForVendorDB method.",efio);
}
}


}



public static CreateTable getVendorInstance(String vndrfqcn) {
//From the grouptype fqcn string generate a Class object.
Class childclass = Class.forName(vndrfqcn);

//From the class object generate a new instance of the class specified by the fqcn in object form.
java.lang.Object childobject = childclass.newInstance();
//caste object to its known basetype and return
return (CreateTable)childobject;

}


In my framework, the call to "Class.forName(..)" is abstracted further to a custom class loader, this class loader overrides the ClassLoader class of the java API to allow custom loading of packages from specific paths so that the framework can dynamically load classes during run time. This is how the framework enables run time extensibility just like standard java web application servers do.


4) The final step is to create a subclass of CreateTable and implement the forced implemenation of the abstract method signature for the no argument version of "timePattern()" It is this version of the method that is called when the corresponding "fqcn" is provided in the string argument method of the same name. Because the object is instanced from the class name at run time (when the name is specified) a dynamic selection of the code (polymorphism) is realized despite the call being made on a static method. In my framework the "fqcn" string comes from the system bootstrap configuration class DPSConfig which bootstraps from settings indicated in an xml configuration file. The file is managable by the UI , so changing back end db's is trivially performed without requiring down time on any of the nodes in the cluster. As well the desire to call necessary db vendor agnostic methods is achieved from all client code that utilizes the static method and passes in the "fqcn" from the system bootstrap settings without knowledge or care of which db vendor class it happens to be. Below I provide an example for a hypothetical subclass with customization for the DB2 DBMS.


public class CreateTableDB2 extends CreateTable {

public String timePattern() {
return " hh:mi:ss ";
}

}

Using static polymorphism has allowed me to get around the sticky problem of calling vendor specific methods in a static way without instancing the subclass that currently maps to the db in use on the system and is straight forward to implement, you might find it useful in your designs.

Comments

Popular posts from this blog

Highly targeted Cpg vaccine immunotherapy for a range of cancer

Significance?


This will surely go down as a seminal advance in cancer therapy. It reads like magic:

So this new approach looks for the specific proteins that are associated with a given tumors resistance to attack by the body's T cells, it then adjusts those T cells to be hyper sensitive to the specific oncogenic proteins targeted. These cells become essentially The Terminator​ T cells in the specific tumor AND have the multiplied effect of traveling along the immune pathway of spreading that the cancer many have metastasized. This is huge squared because it means you can essentially use targeting one tumor to identify and eliminate distal tumors that you many not even realize exist.

This allows the therapy for treating cancer to, for the first time; end the "wack a mole" problem that has frustrated traditional shot gun methods of treatment involving radiation and chemotherapy ...which by their nature unfortunately damage parts of the body that are not cancer laden but …

Engineers versus Programmers

I have found as more non formally trained people enter the coding space, the quality of code that results varies in an interesting way.

The formalities of learning to code in a structured course at University involve often strong focus on "correctness" and efficiency in the form of big O representations for the algorithms created.

Much less focus tends to be placed on what I'll call practical programming, which is the type of code that engineers (note I didn't use "programmers" on purpose) must learn to write.

Programmers are what Universities create, students that can take a defined development environment and within in write an algorithm for computing some sequence or traversing a tree or encoding and decoding a string. Efficiency and invariant rules are guiding development missions. Execution time for creating the solution is often a week or more depending on the professor and their style of teaching code and giving out problems. This type of coding is devo…

First *extra Galactic* planetary scale bodies observed

This headline


Significance?
So every so often I see a story that has me sitting at the keyboard for a few seconds...actually trying to make sure the story is not some kind of satire site because the headline reads immediately a nonsense.
This headline did just that.
So I proceeded to frantically click through and it appears it was a valid news item from a valid news source and my jaw hit the floor.
Many of you know that we've been finding new planets outside of our solar system for about 25 years now.
In fact the Kepler satellite and other ground observatories have been accelerating their rate of extra-solar planet discoveries in the last few years but those planets are all within our galaxy the Milky Way.
The three major methods used to detect the bulk of planets thus far are wobble detection, radial transit and this method micro lensing which relies on a gravitational effect that was predicted by Einstein in his general theory of relativity exactly 103 years ago.
https://exoplanet…