lundi 18 février 2019

New Components Which Implement Methods Not Already in an Interface

I have two class files (WallSwitch and ProximitySwitch) - let's assume they're third-party-supplied components - which implement the Methods On() and Off() from an IUserController interface which I have built in my core, consuming system.

I also have three classes - also third-party-supplied - namely, CeilingFan, Lightbulb and CCTVCamera, which implement Methods from an ICeilingComponent interface, which is part of the core system. These ICeilingComponent-implementing classes are instantiated and injected into the IUserController inheriting classes, for example:

ICeilingComponent component = new LightBulb();

IUserController controller = new WallSwitch(component);

Then...

controller.On();

controller.Off();

Everybody's happy.

I now have a new, third-party-supplied component called DimmerSwitch, which also implements the IUserController Methods of On() and Off(), but the DimmerSwitch class additionally implements the Methods IncreasePower(int amount) and DecreasePower(int amount), which only affects the CeilingFan and (dimmable) Lightbulb, not the CCTVCamera.

I'd like to accommodate this in the core system, but I'm failing to understand how to do it without compelling the other third-party components to change their construction as described by the interface I've contracted them to implement. That's the important bit. There must be a way I can handle this within the core system without affecting change elsewhere.

What design pattern or Interface methodology have I failed to understand correctly?

I had hoped that writing this all down would somehow present a solution to me. But nope. Maybe I can’t see the wood for the trees right now. Have searched for what I thought would be the right terms. This must be a common problem, surely. Feel free to make me look dumb.

P.S. This clearly isn't a real-world problem - I certainly don't wish to encourage discussions about voltage. But this isn't homework either: I only wish I were that young. I'm just trying to get my head around this problem to facilitate my understanding. Any responses appreciated (except any about electrical charge).

Aucun commentaire:

Enregistrer un commentaire