# Include "stdafx. H"
Using namespace STD;
// Abstract Interfaces
Interface IX
...{
Virtual void _ stdcall fx1 () = 0;
Virtual void _ stdcall fx2 () = 0;
};
Interface Iy
...{
Virtual void _ stdcall fy1 () = 0;
Virtual void _ stdcall FY2 () = 0;
};
Void trace (const char * PMSG)... {cout <PMSG <Endl ;};
// Interface implementation
Class CA: Public IX, public Iy
...{
Public:
// Implement interface IX
Virtual void _ stdcall fx1 ()... {cout <"CA: fx1" <Endl ;}
Virtual void _ stdcall fx2 ()... {cout <"CA: fx2" <Endl ;}
// Implement interface Iy
Virtual void _ stdcall fy1 ()... {cout <"CA: fy1" <Endl ;}
Virtual void _ stdcall FY2 ()... {cout <"CA: FY2" <Endl ;}
};
Int main ()
...{
Trace ("client create an instance of the component ");
Ca * pA = new CA;
// Get an iX pointer
IX * pix = PA;
Trace ("client: Use the IX interface ");
PIX-> fx1 ();
PIX-> fx2 ();
// Get an Iy pointer
Trace ("client use the Iy interface ");
Iy * piy = PA;
Piy-> fy1 ();
Piy-> FY2 ();
System ("pause ");
}
Stdafx. h
# Pragma once
# Include <iostream>
# Include <tchar. h>
# Include <objbase. h>
Non-interface communication
I wonder if you still remember the previous article that we mentioned that communication between customers and components is only through interfaces. However, the customers in the program list do not follow this rule. There, the communication between the customer and the component is completed by a pointer pointing to the class ca instead of a pointer pointing to the interface. Use a pointer to Ca to ask the customer to know the definition of the class (usually a header file ). There are many implementation details in the ca-like declaration. Modifications to these Implementation Details make it necessary for the customer to be re-compiled. As mentioned earlier, adding or reducing component interfaces should not interrupt existing customers. This is why we have repeatedly insisted that the customer and components should communicate only through interfaces. Remember, an interface is implemented by a virtual pure base class without implementation details.