mercredi 3 juin 2015

Whats the difference between making an interface reference for an object or doing it directly from its class name java [duplicate]

This question already has an answer here:

im playing with the facade design pattern just to learn, and a this question came to my mind when i was doing it.

I have 1 interface called Shape, and 3 classes that implement this interface that are, square, circle and rectangle, its pretty simple.

The thing is that i have another class called ShapeMaker that is the class who does the facade function.

this is it:

public class ShapeMaker {

Shape circle;
Shape rectangle;
Shape square;

public ShapeMaker() {
    circle= new Circle();
    rectangle= new Rectangle();
    square= new Square();
}

public void drawCircle(){
    circle.draw();
}

public void drawRectangle(){
    rectangle.draw();
}

public void drawSquare(){
    square.draw();
}

}

As you can see, i have 3 objects as members and but they are references from an interface.

it works fine, but, if i change the reference from the shape interface to its class original name, it still works and does the same.

So thats why i make this question, what would be the difference?

thanks.

Aucun commentaire:

Enregistrer un commentaire