开发者

How to allow abstraction in MVC while maintaining SOC?

开发者 https://www.devze.com 2023-04-08 15:41 出处:网络
I\'m refactoring a large w开发者_开发知识库eb game, and making some models to represent my database objects (you read that right - there were none before.) Now I know that traditionally subclassing of

I'm refactoring a large w开发者_开发知识库eb game, and making some models to represent my database objects (you read that right - there were none before.) Now I know that traditionally subclassing of models isn't really done because it can be tricky, but it seems to fit what I'm doing. Some of my tables represent things of which there can be a few closely related types. For example, an attack launched against another player can take a few forms, but they share almost all of their db information. So there's just a single field in the table distinguishing type.

To represent these in code, I have an abstract class Attack, and the subtypes defined. It works fine; that's not the problem. The problem I'm having is how I should refactor the UI part of them. Each type displays slightly differently.

On the one hand, in any other app, the different subtypes would overload a method to describe how they display their information. That would be the proper use of polymorphism. But this is MVC we're talking about. Putting UI code in the models is a mortal sin! But if I leave it where it should be, I have to have extra cruft like switching on subtypes, and now I'm violating LSP!

How can I reconcile this conflict between design principles? Is this why subclassing models is discouraged?

PS I'm sure the title of this question could be much better, I just couldn't describe it succinctly. Feel free to revise.

0

精彩评论

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