I have come up with the following design for my requirement and want know if there is a better way to do it or if you have any feedback on the design.
Requirement
Assume that an app provides different add-ons and each of these add-ons have a licence. The user is given a license key and he can activate/deactivate the license key to use that add-on.
There is also a bundle license which provides the user with a single license key that can be used to activate/deactivate all the add-ons. But there can be only one bundle license in the app.
This will be implemented in PHP.
Design
This is the design that I have come up with.
class BaseLicense {
protected $license_key;
protected $addon_name;
public abstract activate();
public abstract deactivate();
public abstract is_active();
}
class License extends BaseLicense {
// Inherit abstract methods
}
class BundleLicense extends BaseLicense {
// Inherit abstract methods
// Singleton since there can be only one bundle license.
}
Best Answer
I think that the design is well oriented but I would sugggest the folowing:
$addon_name
property but amatches(add_on_name)
method should be added to the base class in order to know if a licence belongs to a given add-on, or better yet not pass the add-on name but the add-on itself so thematches
method can do a more complex validation as to whether or not the license belongs to that add-onBundleLicense
to reduce the visibility of the inherited constructors so they cannot be used to instantiate. Since that could be tricky, a better solution would be that every parent class has their constructors private (I don't know if that can be enforced in PHP) and instead provide agetInstance()
method. All non-singleton classes would return a new instance, but the singleton would return the same instance all the time. That way you don't hace to reduce the visibility of the superclasses' constructors, which I don't know if is possible in PHP.BundleLicense
seems to cover all add-ons, potentially giving access to add-ons it shouldn't give acces to. So I suggest classes to have a private list of add-on names intead of a single$addon_name
property. Non bundle licenses would have a list containing a single add-on name, but bundle licenses will contain a list with two or more add-on names.