我正在努力改進我的類和對象的工作策略。編程策略 - 如何將對象傳遞到類的鏈中
通過一系列特定的類傳遞一個對象以保持代碼組織的最佳方式是什麼?
示例:使用ZedGraph對象(注意)這可能不是最好的例子,但它會得到的想法。
class Graphhandler
{
private ZedGraphControl ZGC;
private SubGraphController PortionofGraph;
public class GraphHandler(ZedGraphControl _ZGC)
{
ZGC = _ZGC;
initializeGraph();
}
private void initializeGraph()
{
// notice I am putting the ZGC Object into another class
// and likely that ZGC object will go into another class
PortionofGraph = new SubGraphController(ZGC);
}
}
class SubGraphController
{
private ZedGraphControl ZGC;
private DeeperSubGraphController PortionofGraph;
public class SubGraphController(ZedGraphControl _ZGC)
{
ZGC = _ZGC;
initializeSubGraph();
}
private void initializeSubGraph()
{
PortionofGraph = new DeeperSubGraphController(ZGC);
// is there a better way?
}
}
有沒有更好的方式通過所有這些調用來傳遞一個yop級別的對象來操縱數據?
是的,我同意這一點,但是我所採取的方法會更好地將工作簡化爲子部分。假設你有2個可能的圖形結果,需要大量的圖表子部分的代碼工作,那麼你是否希望將ZGC向下傳遞給代碼? – Ashitakalax 2012-01-13 23:51:47
@ user970011,你能舉個例子嗎? – 2012-01-14 00:06:36
假設你有一個Graph對象,並且已經給出了你的數據。您可以使圖形成爲折線圖或條形圖。每個圖都有大量的代碼以您想要的方式自定義圖形。所以你創建類來處理折線圖和條形圖工作。爲了仍然使用這些對象,您仍然需要訪問該主圖對象。 – Ashitakalax 2012-01-14 00:14:21