Skip to content

Instantly share code, notes, and snippets.

@developerjamiu
Last active May 10, 2022 09:25
Show Gist options
  • Save developerjamiu/d85539da95165f7d7332914185a5849d to your computer and use it in GitHub Desktop.
Save developerjamiu/d85539da95165f7d7332914185a5849d to your computer and use it in GitHub Desktop.
A snippet of code describing OOP. It is not near perfect
/// Please note that some instantiations are to be done in the main method
/// I placed it orderly (following the class created) for clarity and simplicity.
void main() {}
/// OOP is a computer programming model that organizes software development around entities
/// and objects, rather than functions and logic.
/// The building blocks of OOP includes the:
/// CLASSES: Classes are blueprints for creating objects. They consist of properties / attributes ...
/// ... getters, setters, methods ets.
class Person {
// properties || attributes
String name = '';
// getters
DateTime get yearOfBirth {
return DateTime.now().subtract(Duration(days: age * 365));
}
int get age => 22;
// setters
set age(int newAge) {
age = newAge;
}
// methods
void walk() {
print('I am walking!');
}
}
/// OBJECTS: are instances of a class. In dart we don't instantiate objects using ...
/// ... the new keyword
final jamiu = Person();
/// METHODS: This is a procedure or function associated with a class
void eat(String food) {
print('I\'m eating $food');
}
/// ATTRIBUTES: These are data stored in a class to represent the state of that class
String name = '';
/// ========================================================================
/// ========================================================================
/// The main principles of OOP
/// INHERITANCE: allows a class to derive from another class.
class Human {
Human({required this.name});
String name;
void introduce() {
print('My name is $name');
}
}
class Painter extends Human {
Painter({required String name}) : super(name: name);
void paint() {
print('I am painting');
}
// overriding
@override
void introduce() {
print('My name is $name');
}
}
/// ENCAPSULATION: It is the act of restricting direct access to some components of an object
/// Notice that name is encapsulated and cannot be accessed outside the class
class PersonWithName {
PersonWithName(this._name);
final String _name;
String get name => _name;
}
/// POLYMORPHISM: It allows for objects to appear in different forms
abstract class Bag {
String get name;
}
class ToteBag extends Bag {
@override
String name = 'Tote';
}
class SchoolBag extends Bag {
@override
String name = 'School';
}
/// the objects above can appear in different forms
final Bag bag = ToteBag();
final Bag bag2 = SchoolBag();
/// ABSTRACTION: this is used for hiding implementations of certain methods and attributes
abstract class NetworkService {
void get() {}
void post(String data) {}
}
/// Implementation
class HttpNetworkService implements NetworkService {
@override
void get() {
print('performing get operation');
}
@override
void post(String data) {
print('performing post operation');
}
}
/// Now, the class that needs a network service can depend on the abstraction instead of the implementation
/// Also, mulitple implementations can be created
class AuthViewModel {
AuthViewModel(this.service);
final NetworkService service;
}
/// Constructing the object
/// Notice that we are passing the actual implementation. We can als swap implementations e.g. MockNetworkService
final authViewModel = AuthViewModel(HttpNetworkService());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment