注意:这是一个 PHP 项目
我有一种情况,我为我的项目使用了 2 个 API 提供程序。它们与它们(API)提供的信息相似。我必须设置正确的方式,因为也许明天会添加更多的 API。所以我的项目需要一些方法。基本的,这是我到目前为止所拥有的:
abstract class A {}// first api class, mostly contains API configs and call
abstract class B {}// second api class, also mostly contains API configs and call
//than first API has a sub classes of something like cars and trucks
class Car extends A implements ApiMethodsInterface {} // for the cars
class Truck extends A implements ApiMethodsInterface {} // for the trucks
//now second API has a sub classes for cars , trucks and spaceships
class Car extends B implements ApiMethodsInterface {} // for the cars
class Truck extends B implements ApiMethodsInterface {} // the trucks
class SpaceShip extends B implements ApiMethodsInterface {} // for the space ships
//they all have more or less similar methods
// so i used an Interface that all above classes
interface ApiMethodsInterface
//methods are
public function getModels()
public function getYears()
public function getPrice()
由于每个子类都实现了这个接口,我按接口编码
现在,我有一种情况,SpaceShips 有更多方法要添加,比如 getEquipment() 和更多信息,其他类没有实现的方法。Trucks 还有更多其他人没有实现的方法,例如 hasTrailer()、trailerLength() 等...
我的问题是,现在该怎么办,我应该使用接口隔离,并将接口添加到实现这些方法的类中,然后检查实例化的对象是否具有此方法而不是运行,否则会出现异常,或者将缺少的模型添加到抽象中类 A 和 B,并将方法覆盖到使用这些方法的类中,或者也许有更好的方法来解决这个问题。顺便说一句,设计模式很新……
也许我过度工程,但我真的想以一种好的方式做到这一点。
谢谢