One stumbles upon this phrase when reading about design patterns.
But I don’t understand it, could someone explain this for me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Coding against interface means, the client code always holds an Interface object which is supplied by a factory. Any instance returned by the factory would be of type Interface which any factory candidate class must have implemented. This way the client program is not worried about implementation and the interface signature determines what all operations can be done. This can be used to change the behavior of a program at run-time. It also helps you to write far better programs from the maintenance point of view.
Here’s a basic example for you.
EDIT
I have updated the example above and added an abstract
Speakerbase class. In this update, I added a feature to all Speakers to “SayHello”. All speaker speak “Hello World”. So that’s a common feature with similar function. Refer to the class diagram and you’ll find thatSpeakerabstract class implementISpeakerinterface and marks theSpeak()as abstract which means that the each Speaker implementation is responsible for implementing theSpeak()method since it varies fromSpeakertoSpeaker. But all speaker say “Hello” unanimously. So in the abstract Speaker class we define a method that says “Hello World” and eachSpeakerimplementation will derive theSayHello()method.Consider a case where
SpanishSpeakercannot Say Hello so in that case you can override theSayHello()method for Spanish Speaker and raise proper exception.And we could achieve this behavior by simply adding a base abstract class Speaker and some minor modification in Each implementation thus leaving the original program unchanged. This is a desired feature of any application and it makes your application easily maintainable.