jeudi 12 mars 2015

How do I avoid inheritance and still make it look as though I'm calling one class?

This is a simplified example, and may actually be a decent candidate for inheritance, but don't treat it as "why would I want to do it this way", but rather "how would I do it this way".


I'd want to make a call in my app like this:



// I don't want to do this:
// $temp = new Sedan;
// $myCar = new Car($temp);
// $myCar->paint();

// Instead I want this:
$myCar = new Sedan;
$myCar->paint();


The paint method is actually part of class Car:



class Car {
private $carToPaintOn;

public function paint(){
// paint some car
}
}


What would class Sedan (or Coupe or Convertable or etc...) look like without using inheritance? The class code must use industry standard design patterns and follow guidelines for clean and testable code. Also please avoid duplicating the paint method by additionally adding it to Sedan.


Aucun commentaire:

Enregistrer un commentaire