Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Toybox.Lang;
- (:background) function isBackground() as Boolean {
- // return evcc.isBackground;
- return true; // for testing purposes
- }
- class A_FG extends A_BG {
- function initialize() {
- A_BG.initialize();
- }
- function notNeededInBackground() as Void {
- // [NOTE 1]
- // this assumes that B_BG has an empty implementation
- // of notNeededInBackground(), for convenience
- _b.notNeededInBackground();
- // if not, use the following code instead:
- // (_b as B_FG).notNeededInBackground();
- }
- }
- (:background) class A_BG {
- protected var _b as B_BG;
- function initialize() {
- // some complicated logic
- _b = B_BG.create();
- // some complicated logic
- }
- function neededInBackground() as Void {
- _b.neededInBackground();
- }
- }
- //! Do not directly call `new B_FG()`; use B_BG.create() instead
- class B_FG extends B_BG {
- // would've been nice if this could've been protected
- function initialize(_doNotCallDirectly as Number, _seriouslyDontDoIt as Symbol) {
- B_BG.initialize(42, :initialize);
- }
- function notNeededInBackground() as Void {
- // do stuff
- // ...
- }
- }
- //! Do not directly call `new B_BG()`; use B_BG.create() instead
- (:background) class B_BG {
- // would've been nice if this could've been protected
- function initialize(_doNotCallDirectly as Number, _seriouslyDontDoIt as Symbol) {
- // ...
- }
- (:typecheck(disableBackgroundCheck))
- static public function create() as B_BG {
- if (isBackground()) {
- return new B_BG(42, :initialize);
- } else {
- return new B_FG(42, :initialize);
- }
- }
- function neededInBackground() as Void {
- // do stuff
- // ...
- }
- // empty stub function
- // See [NOTE 1] above
- function notNeededInBackground() as Void {
- // do nothing
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement