Created
February 19, 2020 20:43
-
-
Save adisney/992112ad7e5acf512f90b522c32a015f to your computer and use it in GitHub Desktop.
Outline of the construct-init-start-stop lifecycle pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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