Article Discussion
Design Principles from Design Patterns
Summary: In this interview, Erich Gamma, co-author of the landmark book, Design Patterns, talks with Bill Venners about two design principles: program to an interface, not an implementation, and favor object composition over class inheritance.
18 posts on 2 pages.      
« Previous 1 2 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: June 4, 2010 5:17 AM by Vincent
Fredy
Posts: 1 / Nickname: liquidsnk / Registered: October 20, 2007 0:26 PM
Re: Design Patterns
July 18, 2008 6:15 AM      
nikolai traikov:
I'm not even close to be an expert at this, as some people that replies here, but maybe i can help you..

You are missing some code there, but i think the idea is:
Say tomorrow you want to change Motif to something else..
Using the first example, changing 'new PMWidgetFactory();' for the new implementation of the factory should suffice.
But, in the second example, your change will expand to every place in which you use Motif!,
and, even if you where using interfaces for the components, you still would have the job of changing the creation of every individual Motif component to the creation of the new ones..
using the abstract factory.. you just need to know how to instantiate an implementation for it... and even then.. you could just accept the interface of the factory as a parameter for the constructor of the classes that use it and decouple the implementation even more.

Of course, you don't allways need to do something like this.. there are many scenarios where a pattern implementation is overkill.

If i'm wrong or mssing something, please, feel the need to correct me!!!.
Regards.
Michael
Posts: 3 / Nickname: mangelguy / Registered: June 1, 2010 4:42 PM
Re: Design Principles from Design Patterns
June 1, 2010 9:53 PM      
Hi,

I'm a bit new to interfaces. I'm not quite sold on them yet. Maybe someone here can help.

For example, I can implement one in my new class and fulfill all of its implementation requirements. I can then remove all references to the interface but keep the details of my implementation and my class still works the same. In other words, my class will work the same with or without the interface implementation references. So why keep the references?

What am I missing?

Thanks,

mangelguy
Vincent
Posts: 40 / Nickname: vincent / Registered: November 13, 2002 7:25 AM
Re: Design Principles from Design Patterns
June 4, 2010 5:17 AM      
For a single type of class, you are not missing anything.

However, consider the following class where Animal is an Interface:
import java.util.ArrayList;
import java.util.List;
 
public class AnimalHospital
{
    private final List<Animal> animals;
 
    public AnimalHospital()
    {
        // Initially the hospital has no animals to treat.
        this.animals = new ArrayList<Animal>();
    }
 
    void add(final Animal animal)
    {
        animals.add(animal);
    }
 
    void treatAllAnimals()
    {
        for(final Animal animal : animals)
            animal.treat();
    }
}
This AnimalHospital can accept any kind of animal for treatment - provided the animal's class (e.g. Cat, Dog, etc.) implements the Animal interface.

If, at a later date, someone creates a new animal class (such as a DuckBilledPlatypus) that implements the Animal interface then that animal can also be treated at the hospital without any change to the hospital code. As long as it's an Animal then the AnimalHospital can handle it.

The same is also hold true if someone later extended the Dog to create a Doberman sub-class of dog.

That is power that interfaces bring to your code.
18 posts on 2 pages.
« Previous 1 2 Next »