我不確定我真的理解依賴管理。這是否意味着你只依賴於另一個班級的細節?這並不意味着與通話本身有任何關係?我一直聽到做更小的更具體的課,但他們不能相互依賴,這對我來說似乎不可能。有人可以試着簡單地向我解釋這一點。我在下面列舉了一些例子。依賴管理問題
//Bad Dependency
public class TestOne
{
TestTwo testTwo;
public void TestOneMethod()
{
testTwo = new TestTwo();
testTwo.SomeProperty = "Value";
testTwo.SomeMethodThatWorksWithSomeProperty();
}
}
//Bad dependency??
public class TestOne
{
TestTwo testTwo;
public void TestOneMethod()
{
int myInt = 0;
TestThree testThree = new TestThree();
//... Some Code that works with testThree
testTwo = new TestTwo();
myInt = testTwo.GetSomeInteger(testThree);
}
}
只能有設置每次運行設置,所以我爲什麼要不停的按數據庫每次一個新類叫?這是一個壞的依賴
public static class Application
{
public static int SomeSetting = 0;
public static GetSettingsFromDatabase()
{
//loads the settings for this store
DatabaseClass dbClass = DatabaseClassDataSource.LoadForStore();
SomeSetting = dbClass.SomeSetting;
}
}
public class MyClass
{
public void MethodOne()
{
if(Application.SomeSetting == 1) { //... }
}
}
public class MyClassTwo
{
public void MethodOne()
{
if(Application.SomeSetting == 1) { //... }
}
}