Share a design pattern thinking

Share a design pattern thinking
0.0 0


The CCCallFunc ( CCCallFuncN, CCCallFuncO……) action need create with a CCObject target and a CCObject::selector.
In common case, we add a selector member method into the CCLayer or CCScene which we are working on.
But sometimes,there are lots of selectors in Layer class definition, but they are just inside implementation, no need to be exposed at .h file.
So. I define a SelectorSet class inherit from CCObject in .cpp file and put all selector methods into it.
Use case like this:

static class SelectorSet : public CCObject{
void selector1( void ){
//do somthing
void selector2( CCNode * node ){
//do somthing with node
void selector3( CCObject * obj ){
//do somthing with obj
} selectorSet;

void GameLayer::doAction(){
CCCallFunc * func = CCCallFunc::create( &selectorSet, callfunc_selector( SelectorSet::selector1 ) );
void GameLayer::doActionWithNode{
CCCallFuncN * func = CCCallFuncN::create );
func~~>setTarget( this );
void GameLayer::doActionWithObj{
CCCallFuncO * func = CCCallFuncO::create, this );
this~~>runAction( func );

So the implementation will be hidden, the .h file will be keep clean.


Thank you for your sharing.