Nazgob
Nazgob

Reputation: 8563

How to design my classes to leverege factory and be extensible?

My c++ SOA app has a concept of "session" that is used exchange data between services. In example its used for checking legality of some service A operations before executing session B which commits or rollback changes. Whatever.

I have 2 types of session modes: normal and what-if. Going further, I have different session, session for legality, session for assign, session for commit etc. This is a main problem. Legality session can be what-if or real etc.

How to fix that and avoid code duplication?

I can make a ISessionFactory interface and have WhatIfFactory and RealFactory implement it. Then I could make a ILegalitySession and make WhatIfLegalitySession and RealLegalitySession implement it. Then my factories would return appropriate objects.

It has 2 major problems. What if new mode will come? I will have to implement new factory and new classes for all sessions! What if new session type comes? I have to change both of factories...

Perhaps resign from 2 hierarhies and have whatIf sessions "decorate" real session? How can I localize the change?

Upvotes: 0

Views: 233

Answers (2)

Sydius
Sydius

Reputation: 14257

I think the decorator pattern makes sense here. You might want to also look at the strategy pattern and its compile-time cousin, policy-based design. It's hard to say which is best without more information. Decorators are great for adding additional behavior, the other two for changing existing behavior.

Upvotes: 0

Mykola Golubyev
Mykola Golubyev

Reputation: 59834

Try to implement your WhatIf with decorators. Or extract some 'what if' specific parts to kind of strategy.

Another option is using of the State pattern. 'WhatIf' state and 'Real' state.

Upvotes: 1

Related Questions