开发者

Expose subset of a class - design question

开发者 https://www.devze.com 2023-02-15 12:50 出处:网络
Suppose i have a product class with about close to 100 properties. Now 开发者_如何学JAVAfor some operations (Say tax calculation) i dont really need this bulky product type, rather only a subset that

Suppose i have a product class with about close to 100 properties. Now 开发者_如何学JAVAfor some operations (Say tax calculation) i dont really need this bulky product type, rather only a subset that has price related properties. I am not sure if i should create different snap shots(class) of products that just has the properties that i am interested in. what would be the ideal approach so that i don't unnecessarily pass around unsought fluff?

Thanks in advance.


I would advice you to look at SRP and Interface Segregation Principle. A class with 100 properties is probably doing way too much. You should also pay attention to these subsets and if they are something you could extract out as interfaces or super classes. If yes then you need to use SRP and make sure you have a right inheritance hierarchy.

And btw what's your lanugage of implementation and target platform?


If your sole reason is to avoid "passing around unsought fluff" then there really isn't any reason to do what you're proposing. The odds are, the language you are using passes class instances around by reference, so if you feel like since the object is so big, it must be a performance problem, it likely isn't.

If however you are talking about wanting to make consuming methods that can take instances of different types but share similar properties that you want to work with in that method, then look at researching "base classes" or "interfaces"

0

精彩评论

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

关注公众号