Method এর ক্ষেত্রে Parameter Pass করার সময়ে --
যদি আমার একটি Object এর একটি Parameter প্রয়োজন হয়, সেক্ষেত্রে আমি শুধুমাত্র ঐ Object এর Parameter টাকে Pass করব। পুরা Object টাকে Pass করবো না। এটা করলে সেটা Law of Demeter-এর Violation হবে।
Never
break the Law of Demeter (LoD)
Breaking the Law of Demeter is Like Looking for a Needle in the Haystack (খড়ের গাঁদায় সূচ খোঁজা)
class Mechanic {
Engine engine;
Mechanic(Context context) {
this.engine = context.getEngine();
}
}
A given object should assume as little as possible about the structure or properties of anything else (including its sub-components)
The Mechanic does not care for the Context. You can tell because Mechanic does not store the reference to Context. Instead the Mechanic traverses the Context and looks for what it really needs, the Engine.
1. To write tests, I have to create the Context just so when I construct the Mechanic it can reach in the Context and get what it really needs, the Engine. But context is never something which is easy to create.
2. Today we have fancy tools such as JMock and EasyMock, surely we can mock out Context. Yes, you can! BUT:
3. Typical setup of a mock is about 5 lines of code. So your test will contain a lot of junk which will mask the real purpose of the test.
4. These tests will be fragile. Every time you refactor something in context, or how context interacts, you are running the risk of breaking your tests.
5. What if you want to test class Shop which needs a reference to Mechanic? Well then you have to mock out Context again
No comments:
Post a Comment