我试图说服一个同事,一个函数应该把接口作为参数,而不是对象本身。我认为小对象可以很好地通过,但对于大对象,我会给他们一个接口,只是通过I/F,而不是整个事情。将* interface *或* object *作为参数传递给函数会更好吗?
请注意,只有这些大类中的一个 - i/f将永远不会用于不同的对象。这仅仅是为了隐藏对象的实现。
你是否同意将一个大类分离为一个接口是一种好的做法?
这样做有什么缺点吗?
实施例:
public interface class IVeryLargeClass
{
void DoSomething();
...
};
public ref class VeryLargeClass : public IVeryLargeClass
{
public:
virtual void DoSomething() { ... }
...
};
public ref class AnotherClass
{
public:
AnotherClass(VeryLargeClass^ vlc) { vlc->DoSomething(); }
// OR
AnotherClass(IVeryLargeClass^ vlc) { vlc->DoSomething(); }
};
下面的答案很好,但你应该明白,对象的“大小”与这个主题无关。 “大”班或“小班”与本次讨论无关。该接口用于隐藏实现。这根本不是性能问题。 – Bill 2009-06-17 10:19:10
@Bill - 在这种情况下,我确实同意你的大小并不重要(ahem),我只是用一个大类来说明将一个巨大的对象传递给一个想要使用其中的一个或两个函数的类。 – demoncodemonkey 2009-06-17 10:43:26