开发者

A concrete design case in C++, embedded AVR project

开发者 https://www.devze.com 2023-04-13 02:52 出处:网络
I have a design problem I pictured on a diagram below. There is a class Owner holding pointers to classes Switcher and Base. Switcher has a method, say Switcher::switch(Base* base) whose goal is to ch

I have a design problem I pictured on a diagram below. There is a class Owner holding pointers to classes Switcher and Base. Switcher has a method, say Switcher::switch(Base* base) whose goal is to change Owner::Base's implementation from Child1 to Child2 or back if needed. I wouldn't like to implement this functionality in Owner, as it is complex enough already. The variables Switcher* Owner::switcher and Base* Owner::base are private in Owner.

Is there a design pattern for this? How would should I implement it? If written some simple code to implement it but I guess I'm having problems with access control, as switcher and base member variables are declared private. I can't tell for sure, as I cannot step-by-step debug the system (it's an embedded project). I'm also trying to implement it with linux-gcc to test开发者_开发百科 what's wrong, but maybe there is a simple solution I I didn't think of, and hence the question here. I wouldn't like to use friend keyword, as I somehow have a feeling using it is a sign of bad design.

A concrete design case in C++, embedded AVR project


Why not access the base through switcher. That way, switcher can change base and owner will only see itself accessing the switch.

Otherwise, have switcher call a setbase(Base*) method in owner. The switching logic could then be handled in the switcher, but the access to the fields is in owner, where it belongs.


just to gain a bit of clarity, are child1 and child2 inherited from base? If so, how are you intending to "switch" them?

Im guessing your trying to avoid circular references?

EDIT: I don't think there is a design pattern that really matches up nicely to this problem, the closest I can think of, is maybe adapting the factory somewhat so that you can hand the base back, and ask for it in a different form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号