Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not familiar with C#, but I can offer you some python code in the hope that the ideas inside may be useful.</p> <p>I'll start with the client code, to show how the API is used : </p> <pre><code># Client code myapi = MyAPI() # then call different methods : message = "Some status" # send to all platforms myapi.broadcast(message) # or myapi.send_facebook(message) # send to fb only myapi.send_twitter(message) # send to twitter only # or myapi.send("facebook",message) # another way of doing the same thing </code></pre> <p>Now the implementation : </p> <pre><code># actual APIs import FacebookAPI1 import FacebookAPI2 ... import TwitterAPI1 import TwitterAPI2 ... # your individual wrappers, one for each API you want to use # they all expose a send method class FacebookAPI1Wrapper: def send(self,message): #use FacebookAPI1 functions class FacebookAPI2Wrapper: def send(self,message): #use FacebookAPI2 functions class TwitterAPI1Wrapper: def send(self,message): #use TwitterAPI1 functions class TwitterAPI2Wrapper: def send(self,message): #use TwitterAPI2 functions # Your API, this is the only class the client code needs. class MyAPI: def __init__(self): # you decide internally what wrappers to use self.fb_api = FacebookAPI1Wrapper() self.twitter_api = TwitterAPI2Wrapper() # No need for an intermediate level here I guess (Twitter and Platform_1 in your examples) # like : self.fb_api = Facebook() where Facebook chooses what wrapper to use internally (FacebookAPIWrapper) # it would just add unnecessary level of inderection. ... # other plateforms # hash-table-like structure, keys are plateform names, values are objects # this attribute is useful for the exposed send method where the first argument is a string # representing the name of the plateform the client wants to send a message to self.plateforms = {"facebook" : self.fb_api, "twitter" : self.twitter_api ... : ... } def broadcast(self,message): for plateform in self.plateforms.values() : #.values() will return the objects stored in the hash-table plateform.send_message(message) def send_facebook(self,message): self.fb_api.send(message) def send_twitter(self,message): self.twitter_api.send(message) #... def send(self,plateform_name,message): # a simple hash table lookup platform = self.platforms.get(plateform_name) plateform.send(message) </code></pre> <p>Changing your code internally (implementation code) won't break client code as long as you keep the same interface.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload