I am writing several small simple applications that have a common structure and should do the same things the same way (for example, logging, setting up a database connection, setting up an environment), and I'm looking for some tips on structuring reusable components. The code is written in a strictly and statically typed language (for example, Java or C #, I had to solve this problem in both cases). At the moment I have this:
abstract class EmptyApp //this is the reusable bit
{
abstract function body()
function run()
{
this.body()
}
}
class theApp extends EmptyApp //this is a given app
{
function body()
{
}
function main()
{
theApp app = new theApp()
app.run()
}
}
Is there a better way? Perhaps as follows? I have problems weighing compromises ...
abstract class EmptyApp
{
}
class ReusableBits
{
static function doSetup(EmptyApp theApp)
static function doCleanup(EmptyApp theApp)
}
class theApp extends EmptyApp
{
function main()
{
ReusableBits.doSetup(this);
ReusableBits.doCleanup(this);
}
}
One obvious trade-off is that with option 2, the framework cannot wrap the application in a try-catch block ...