In the following example class definition, only the eat(), sleep() and play() methods are relevant to the class Dog. Whilst relevant to the plugin as a whole, i18n() and enqueue_scripts() are not relevant to the class and in my opinion should be either a) stand-alone functions or b) members of a different class.
class Dog {
public $var;
private static $instance;
private function __construct() {}
private function __clone() {}
function i18n() {
// Load text domain. Whilst relevant to the plugin, it isn't directly relevant to the class.
}
function enqueue_scripts() {
// Enqueue styles and scripts. Whilst relevant to the plugin, it isn't directly relevant to the class.
}
function sleep() {
// The things in this method are directly related to the class.
}
function eat() {
// The things in this method are directly related to the class.
}
function play() {
// The things in this method are directly related to the class.
}
static function get_instance() {
if ( self::get_instance() == NULL )
self::$instance = new Dog;
return self::$instance;
}
}
Something I also see very often is a god-like class used with the singleton pattern which disallows multiple objects being created. I've built the above example class as a singleton, just to show visually what I mean.
My question
Is there any advantages of defining god-like classes which include methods for everything? I'm struggling to understand why developers do it instead of defining classes with just the (relevant) methods the class needs. Hoping someone can explain.
Refs:
Aucun commentaire:
Enregistrer un commentaire