jeudi 3 décembre 2020

How to implement Factory Design Pattern in JavaScript ? Need help in clean codign in JavaScript

I am creating an app in Javascript. The app will use either of the services provided by various companies. We may need to change the services. As I am learning design pattern it seems If I use factory design pattern, it might help me in the long run. I might be wrong. I will appreciate your suggestion if you have an alternative idea.

ScraperServiceFactory (){
  createScraper(type) {
      switch (type) {
       case "organe" : 
          return new OrangeScraper()
       case "apple" : 
          return new AppleScraper()
       default: 
          return new MannualScraper() 
     }
  }
}

Each of the scraper Service classes has two methods - doSearch() and getData(). How should I design them to make the code better rather than include them in each of the class?

appleService.js

 class AppleSraperService {
   doSearch(keyword) {}
   getData(resultId){}
 }

organgeService.js

 class OrangeSraper{
   doSearch(keyword) {}
   getData(resultId){}
 }

manualScraper.js

 class ManualSraper{
   doSearch(keyword) {}
   getData(resultId){}
 }

Aucun commentaire:

Enregistrer un commentaire