1

I know how to declare and rules to create interface.

i'm trying to understand exact need of interface in real time.I'm reading many articles with examples.still i'm not satisfied.

Can any one explain clearly taking our Magento as a Case Study.

For example in shipping module

interface Mage_Shipping_Model_Carrier_Interface
{

    /**
     * Check if carrier has shipping tracking option available
     *
     * @return boolean
     */
    public function isTrackingAvailable();

    /**
     * Get allowed shipping methods
     *
     * @return array
     */
    public function getAllowedMethods();

}


class Mage_Shipping_Model_Carrier_Freeshipping
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
.......................some code...............
public function getAllowedMethods()
    {
        return array('freeshipping' => $this->getConfigData('name'));
    }
}
0

3 Answers 3

8

If you're coding features for your Magento system you really don't need to worry about interfaces. Just have your classes implement the correct interfaces and you'll be fine.

Interfaces — and abstract classes — are a way for systems developers (Magento core team) to inform/force client developers (you and store owners) along a certain development path.

The best way to consider these OOP features is to consider life without interfaces. Let's say you have a team of two developers. Each developer needs to implement a shipping carrier. Developer one implements one class — developer two implements another. Theses classes have wildly different methods and approaches. When a third developer needs to implement these into their system, it's going to be more work. When a third developer needs to implement a third carrier — they're going to see two different ways of doing it and will probably end up reinventing the wheel.

Interfaces prevent this from happening. The original system developer creates an interface for a carrier class, and all carrier classes must implement this interface. That means all carrier classes will have the same methods — isTrackingAvailable and getAllowedMethods. This makes it clear to future developers which method they need to implement to create a carrier, and it means the rest of Magento can know it's ok to call these methods on an object whose type it doesn't specifically know, but it knows implements the carrier interface.

1
  • Nice explanations :)
    – Shivam
    Apr 26, 2018 at 10:25
3

I think the PHP manual says it quite nicely:

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

If the Mage_Shipping_Model_Carrier_Interface was not implemented, we would have people designing new shipping modules with no consistent regard for what that shipping module is expected to do.

When Magento calls isTrackingAvailable(), it doesn't care if you're using Table Rates, FedEx, or Delivery by Carrier Pigeon, all it wants to know is if the tracking is available so that it can get some further information. It also doesn't care how the shipping class goes about collecting the tracking information (since each shipping class will be different).

Without the interface, the guy that built the Delivery by Carrier Pigeon module might omit this method, which would result in an error further down the line when Magento expects isTrackingAvailable() to be called.

In fact, the example of Magento's shipping classes and the isTrackingAvailable() method makes a great use-case to explain interfaces in a real-world scenario.

0
3

One of the main advantages of interfaces is Code maintenance.

I like cricket very much. So I try to explain as simpler as with following example.

For Example Espncricinfo team will display weather report along with scores. Normally they will depend on some third party.

Class ThridpartyA
{
Weatherinfo()
{
Return temperature;
}
}
Class EspncricInfo extends ThirdpartyA
{
firstInningsScore();
secondInningsScore();
}

So whenever they required they will use this method.

For Example In home page

espn=new Espncricinfo();
espn. Weatherinfo();

In Fixtures page

espn=new Espncricinfo();
espn. Weatherinfo();

In Results page

espn=new Espncricinfo();
espn. Weatherinfo();

Suddenly thirdpartyA increased the cost to give the service.So now Espncricinfo team decides to change their request for service to some other thridpartyB.

Class ThridpartyB
{
Weathercalculation()
{
Return temperature;
}
}

So

 Class EspncricInfo extends ThirdpartyB
{
firstInningsScore();
secondInningsScore();
}

So Now you should replace with Weatherinfo() with Weathercalculation() in everywhere.

In home page

espn=new Espncricinfo();
espn. Weathercalculation();

In Fixtures page

espn=new Espncricinfo();
espn. Weathercalculation();

In Results page

espn=new Espncricinfo();
espn. Weathercalculation();

So it will be headache. For that we can overcome with following way.

Interface Weather
{
calWeather();
}

And you will use same method everywhere. And every third party system should follow to give their service.

Class ThridpartyA implements Weather
{
//their logic will go here to calculate temperature.
calWeather()
{
Return temperature;
}
}
Class ThridpartyB implements Weather
{
calWeather()
{
// their logic will go here to calculate temperature.
Return temperature;
}
}

Now In home page

espn=new Espncricinfo();
espn. calWeather() ;

In Fixtures page

espn=new Espncricinfo();
espn. calWeather() ;

In Results page

espn=new Espncricinfo();
espn. calWeather() ;

Some other day Espncricinfo decide to some other thirdpartyC.

Class ThridpartyC implements Weather
{
//their logic will go here to calculate temperature.
calWeather()
{
Return temperature;
}
}
Class EspncricInfo extends ThirdpartyC
    {
    firstInningsScore();
    secondInningsScore();
    }

So Now there is no headache for Espncricinfo.They don’t need to change anything in any pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.