Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SpecialEmployee emp = new SpecialEmpoyee();
- interface IBird
- {
- public void fly();
- public void makeSound();
- }
- class Sparrow : IBird
- {
- public void fly()
- {
- // Print "Fly"
- }
- public void makeSound()
- {
- // Print "Sound"
- }
- }
- interface ToyDuck
- {
- // toyducks dont fly they just make
- // squeaking sound
- public void squeak();
- }
- class PlasticToyDuck implements ToyDuck
- {
- public void squeak()
- {
- //Print "Squeak"
- }
- }
- class BirdAdapter implements ToyDuck
- {
- // You need to implement the interface your
- // client expects to use.
- // Instead of using implementation we are programming against interface
- // here, by using IBird instead of "new Sparrow" or something like it.
- // Further, it is an example of constructor dependency injection where we are inserting dependency "IBird" through constructor.
- IBird bird;
- public BirdAdapter(IBird bird)
- {
- // we need reference to the object we
- // are adapting
- this.bird = bird;
- }
- public void squeak()
- {
- // translate the methods appropriately
- bird.makeSound();
- }
- }
Add Comment
Please, Sign In to add comment