Skip to content

Instantly share code, notes, and snippets.

@wyattbiker
Created May 7, 2019 21:20
Show Gist options
  • Save wyattbiker/80af9f218345b2a9479163e2a264231f to your computer and use it in GitHub Desktop.
Save wyattbiker/80af9f218345b2a9479163e2a264231f to your computer and use it in GitHub Desktop.
classes
import 'dart:io';
class Logxy {
static String sName;
static num slog;
static Logxy _logger;
// Logxy._() : super._();
// Logxy._(this.sName);
factory Logxy() {
print('Logger $slog');
if (_logger == null) {
slog = 1;
print('Logger $slog');
slog++;
sName = 'xyz';
_logger = Logxy.internal(); // Causes singleton instantiation
}
return _logger;
}
Logxy.internal();
log(String msg) {
print(msg);
}
}
class MyClass {
num instanceVar;
static num start;
static final MyClass _singleton = new MyClass._internal();
factory MyClass(num startparam) {
start = startparam;
return _singleton;
}
MyClass._internal() {
if (instanceVar == null) {
instanceVar = start;
} else {
instanceVar++;
}
// initialization logic here
}
// rest of the class
}
class Beast {
void chase(Beast x) {
print("Animal $x");
}
}
class Animal extends Beast {
void chase(Beast x) {
print("Animal $x");
}
}
class Mouse extends Animal {
}
class Cat extends Beast {
void chase(covariant Mouse x) {
print("x: ${x}");
}
}
main() {
var c = Cat();
var m = Mouse();
c.chase(m);
print(Platform.version);
// consuming code
MyClass myObj1 = MyClass(1); // get back the singleton
print('myObj1=${myObj1.instanceVar}');
// another piece of consuming code
MyClass myObj2 = MyClass(2); // still getting back the singleton
print('myObj2=${myObj2.instanceVar}');
// Logxy logger1 = Logxy(); // Logger instantiation
// logger1.log("Logger Instantiated ${Logxy.slog}");
// Logxy logger2 = Logxy(); // Logger Object Re-use
// logger2.log("Logger Object Re-used ${Logxy.slog}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment