Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

In

...

extension

...

of

...

my

...

work

...

on

...

refactoring

...

the

...

HarvestScheduler

...

assignment

...

in

...

the

...

unit

...

test

...

class,

...

which

...

I

...

described

...

in theĀ More informative naming,

...

less

...

inline

...

comments

...

thread,

...

I've

...

struggled

...

with

...

the

...

instantiation,

...

assignment

...

and

...

cleanup

...

of

...

the

...

HarvestSchedule'er.

...

It

...

has

...

finally

...

dawned

...

on

...

me

...

that

...

the

...

the

...

problem

...

is

...

2

...

conflicting

...

characteristics

...

of

...

the

...

HarvestScheduler:

...

  1. The

...

  1. class

...

  1. is

...

  1. meant

...

  1. to

...

  1. be

...

  1. a

...

  1. Singleton

...

  1. .

...

  1. This

...

  1. means

...

  1. that

...

  1. only

...

  1. one

...

  1. instance

...

  1. of

...

  1. the

...

  1. class

...

  1. should

...

  1. exist

...

  1. a

...

  1. one

...

  1. time,

...

  1. which

...

  1. the

...

  1. Singleton

...

  1. pattern

...

  1. enforces

...

  1. by

...

  1. hiding

...

  1. the

...

  1. possibility

...

  1. of

...

  1. construction/creating

...

  1. instances

...

  1. from

...

  1. users

...

  1. of

...

  1. the

...

  1. class.

...

  1. The

...

  1. class

...

  1. extends

...

  1. the

...

  1. CleanupIF

...

  1. :

...

  1. This

...

  1. interface

...

  1. implies

...

  1. that

...

  1. it

...

  1. is

...

  1. possible

...

  1. to

...

  1. invoke

...

  1. this

...

  1. operation

...

  1. when

...

  1. closing

...

  1. down

...

  1. the

...

  1. class.

...

  1. This

...

  1. works

...

  1. fine

...

  1. in

...

  1. normal

...

  1. class,

...

  1. where

...

  1. the

...

  1. operation

...

  1. is

...

  1. symmetric

...

  1. to

...

  1. the

...

  1. construction

...

  1. of

...

  1. the

...

  1. class.

...

  1. Eg.

...

  1. an

...

  1. objects

...

  1. life

...

  1. cycle

...

  1. is:

...

  1. create

...

  1. ->

...

  1. living

...

  1. ->

...

  1. cleanup.

...

The

...

conflict

...

between

...

the

...

2

...

aspects

...

of

...

the

...

HarvestScheduler

...

arises

...

because

...

a

...

Singleton

...

pattern

...

hides

...

the

...

object

...

creation

...

control

...

from

...

its

...

API

...

control,

...

where

...

the

...

CleanupIF

...

is

...

logically

...

a

...

functionality

...

associated

...

with

...

the

...

destruction

...

of

...

a

...

object.

...

The

...

problem

...

can

...

be

...

seen

...

in

...

the

...

follow

...

HarvestScheduler

...

code:

...

}
Code Block
public void cleanup() {
  hsmon = null;
  instance = null;
}{code}

This

...

code

...

enables

...

a

...

user

...

to

...

create

...

multiple

...

instances

...

of

...

the

...

HarvestScheduler

...

as

...

simple

...

as

...

this:

...

}
Code Block
HarvestScheduler h1 = HarvestScheduler.getInstance();
h1.cleanup();
HarvestScheduler h2 = HarvestScheduler.getInstance();

This

...

means

...

the

...

that

...

the

...

contract

...

for

...

the

...

Singleton-pattern

...

has

...

been

...

broken.

...

It

...

also

...

means

...

that

...

application

...

behaviour

...

will

...

now

...

depend

...

on

...

when

...

the

...

created

...

HarvestScheduler

...

instances

...

are

...

garbage

...

collect.

...

What

...

is

...

even

...

worse

...

is

...

that

...

this

...

is

...

used

...

in

...

the

...

unit

...

test

...

to

...

emulate

...

contruction

...

of

...

a

...

new

...

HarvestScheduler

...

instance

...

in

...

each

...

test

...

case.

...

Eg.

...

}
Code Block
public class HarvestSchedulerTester extends TestCase {
...
  public void tearDown() {
   
if (harvestScheduler \!= null) {
            harvestScheduler.close();
        }
    ...
  }


 public void testGetHoursPassedSince() throws Exception {
        harvestScheduler = submitNewJobsAndGetSchedulerInstance();
  }
{code}

Note:

...

The

...

HarvestSchedulers

...

close

...

call

...

delegated

...

to

...

the

...

Cleanup

...

method.

...

Conclusion:

...

The

...

CleanupIF

...

should

...

never

...

be

...

used

...

on

...

Singletonic

...

classes

...

as

...

this

...

leads

...

to

...

complicated

...

and

...

brittle

...

code,

...

and

...

may

...

lead

...

to

...

inconsistent

...

application

...

behavior.

...

A solution could

...

be

...

to

...

split

...

the

...

HarvestScheduler

...

into

...

a

...

plain

...

thread

...

execution

...

scheduler

...

singleton,

...

and

...

a

...

'JobManager'

...

class containing the business logicextending the CleanupIF.

...

Another

...

solution

...

might

...

be

...

to

...

replace

...

the

...

singleton

...

pattern

...

with

...

a

...

Inversion-of-control

...

pattern

...

,

...

delegation

...

the

...

creation

...

control

...

to

...

a

...

class/container

...

external

...

to

...

the

...

HarvestScheduler

...

and

...

its

...

users.