Interface implementations

When you create an interface, you are creating a definition that can never change after the interface definition has been released. This interface invariance is an important principle of component design, because it protects existing systems that have been written to use the interface.

When an interface is clearly in need of enhancement, a new interface should be created. This interface might be named by appending a "2" onto the original interface name, to show its relationship to the existing interface.

Generating new interfaces too frequently can bulk up your components with unused interface implementations. Well-designed interfaces tend to be small and independent of each other, reducing the potential for performance problems.

Factoring Interfaces

 

The process of determining what properties and methods belong on an interface is called factoring.

In general, you should group a few closely related functions in an interface. Too many functions make the interface unwieldy, while dividing the parts of a feature too finely results in extra overhead and diminished ease of use. For example, the following code calls methods on two different interfaces of the BusinessAccount class:

// C#

public void MergeAccounts(BusinessAccount account1,

   BusinessAccount account2)

{

   IAccount account = account1;

   ICustomer customer = account1;

   account1.Merge(account2);

   customer.ChangeAddress();

   account.PostInterest();

}

It is much harder to go wrong in designing interfaces than in creating large inheritance trees. If you start small, you can have parts of a system running relatively quickly. The ability to evolve the system by adding interfaces allows you to gain the advantages object-oriented programming was intended to provide.