Skip to content

Instantly share code, notes, and snippets.

@adisney
Created February 19, 2020 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adisney/992112ad7e5acf512f90b522c32a015f to your computer and use it in GitHub Desktop.
Save adisney/992112ad7e5acf512f90b522c32a015f to your computer and use it in GitHub Desktop.
Outline of the construct-init-start-stop lifecycle pattern
class AbstractFoo {
int bar;
int baz;
Client client;
static create(bar, baz, client) {
this.bar = bar;
this.baz = baz;
this.client = client;
}
init() {
client.establishConnection();
}
start() {
this.client.streamData();
}
stop() {
this.client.disconnect();
}
}
class ConcreteFoo {
OtherClient concreteClient;
static create(bar, baz, client, concreteClient) {
super.create(bar, baz, client);
this.concreteClient = concreteClient;
}
init() {
super.init()
concreteClient.establishConnection();
}
start() {
super.start();
this.concreteClient.registerCallback(this.handle);
}
stop() {
super.stop()
this.concreteClient.disconnect();
}
}
run() {
try {
AbstractFoo foo = new ConcreteFoo('bar', 'baz', new thirdPartyApiClient(), new dbClient());
foo.init();
foo.start();
} catch (ShutdownEvent) {
foo.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment