开发者

Avoiding binding objects in OOP

I've read that it's recommended not binding components together since if you remove a part the rest might not function properly. However, I see no alternative to binding objects together when using their methods?

Say I have a security class which checks for malicious input, a logging class that logs errors, custom messages etc., and a comment class that handles user comments.

Now, if I wanted to scan the input coming to the comment class, wouldn't it be wisest calling upon the security class' scanInput() method? Also, if malicious input was found, wouldn't it be good to then log this through the logging class' saveDetails()?

But then, if I removed the security class, then the comment class would get problems, or if I removed the logging class the security class wouldn't know how to log.

I'm trying to improve my design, but I don't开发者_运维技巧 think that making every class doing everything is a good idea (it's also common sense) so I could really need a head's up here, plain and simple. Spending hours reading about aggregation and composition didn't really help.


You can reduce your dependence on specific objects by separating the interface from the implementation. For this you can use abstract classes or interface constructs. See also dependency injection.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜