jeudi 25 novembre 2021

I need to understand how to use a strategy design on a project PHP Project?

Note: it's a PHP Project

I have a situation where i use 2 API providers for my project. They are like similar with what info they (API) provides. I must set this right way since maybe tomorrow there will be some more APIs added. So my project will need some methods. Basic, here is what i have so far:

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()

since every sub class implements this interface , i code by interface

Now, i have a situation, where SpaceShips has more methods to add, like getEquipment() and some more info, methods that other classes are not implementing. Trucks also has more methods that others do not implements , like, hasTrailer(), trailerLength() etc...

My question is , what to do now, should i use Interface segregation, and add Interfaces to classes that implements those methods, and later check if object instantiated is having this method than run, else some exception, or to add missing models into the abstract classes A and B, and override methods into the classes that use those methods, or maybe there is even more good way of solving this. Quite new into Design Patterns btw...

Maybe i am overengineering but i really want to do this in a good way.

Thanks

Aucun commentaire:

Enregistrer un commentaire