Im programming a library, I want to structure my code in a way that potential users find it easy to use this library. I have looked at this excellent video of great API design. One of the golden rules is to have a good design pattern. I have two in mind. The Facade design patten and the factory design patten. Both these design pattens are used to achieve a great level of abstraction in program code for users to use.
Im finding it ever so difficult on which or if both to apply. Of course, I would like to avoid having anti-pattens.
The language I am using is Python, which is quite different to Java Which is emphasised in the video link provided above. How my library would work:
Character class
Window class
Many character objects can be created.
Say if I was to use the facade design pattern I would have both this classes initialised in the facade constructor, so that they can be called in the methods:
class Facade():
__init__(self): self.character = Character() self.window = Window()
here I would create methods which represent methods created in the character and window class. These could then easily be called by users:
Facade = Facade()
Facade.methodUsedInCharacterClass()
Facade.methodUsedInCharacterClass()
Facade.methodUsedInWindowClass()
But here is where I see the facade having a problem, you can have two different characters, which you would want to have to do different things. You could do this:
Facade = Facade()
badguy= Facade.methodUsedInCharacterClass()
goodguy= Facade.methodUsedInCharacterClass()
Facade.methodUsedInWindowClass()
You cant then use the badguy or goodguy variables to invoke other behaviours, such as
badguy.rotate()
goodguy.jump()
This is not allowed, but is there a way of doing this?
Also, as the constructor is calling the classes, a factory method could be used, which choses at run time which class to execute by my own understanding. I can only visualise, but in this case how would it apply?
Thanks!