Я не знаком с C #, но могу предложить вам немного кода на Python в надежде, что идеи внутри могут быть полезны.
Я начну с кода клиента, чтобы показать, как используется API:
# 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
Теперь реализация:
# 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)
Внутреннее изменение кода (кода реализации) не нарушит код клиента, если вы сохраняете тот же интерфейс.