0

In this piece of code is it better to use abstract classes in place of the interfaces or is it good as it currently is? If so, why?

/** contract for all flyable vehicles **/
interface iFlyable {
    public function fly();
}

/* concrete implementations of iFlyable interface */
class JumboJet implements iFlyable {
    public function fly() {
        return "Flying 747!";
    }
}

class FighterJet implements iFlyable {
    public function fly() {
        return "Flying an F22!";
    }
}

class PrivateJet implements iFlyable {
    public function fly() {
        return "Flying a Lear Jet!";
    }
}

/** contract for conrete Factory **/
/**
* "Define an interface for creating an object, but let the classes that implement the interface
* decide which class to instantiate. The Factory method lets a class defer instantiation to
* subclasses."
**/
interface iFlyableFactory {
    public static function create( $flyableVehicle );
}

/** concrete factory **/
class JetFactory implements iFlyableFactory {
    /* list of available products that this specific factory makes */
    private  static $products = array( 'JumboJet', 'FighterJet', 'PrivateJet' );

    public  static function create( $flyableVehicle ) {
        if( in_array( $flyableVehicle, JetFactory::$products ) ) {
            return new $flyableVehicle;
        } else {
            throw new Exception( 'Jet not found' );
        }
    }
}

$militaryJet = JetFactory::create( 'FighterJet' );
$privateJet = JetFactory::create( 'PrivateJet' );
$commercialJet = JetFactory::create( 'JumboJet' );
2
  • 3
    This is probably better suited for codereview.stackexchange.com Commented Jan 7, 2015 at 19:51
  • 1
    This is also largely going to be opinion-base as well, but I will give one opinion (not an answer). I think that your example is too simplistic to determine which might be better. If for example there are common behaviors that might exist for all flying vehicles that might lend to better code reuse (i.e. change altitude, change heading, take-off, land) with overriding behaviors only in certain cases, then maybe an abstract class might be preferable to an interface. Commented Jan 7, 2015 at 19:55

1 Answer 1

4

the interface is more flexible. this way not everything that flies is forced to inherit from the same base class (php doesnt support multiple inheritance)

so

class bird extends animal implements flyable
class plane extends machine implements flyable
class cloud implements flyable

sometimes flexibility is not desired.

abstract classes can also provide function definitions which will reduce code duplication if multiple flying classes required the same fly() method

hope that helps you understand your choice

Sign up to request clarification or add additional context in comments.

1 Comment

Makes a lot of sense. Thnaks @David Chan

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.