1

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!

1 Answer 1

1

You seem to have misunderstood the purpose of factory and facade patterns.

The factory pattern is used to build things. So you will have a CharacterFactory and a WindowFactory. If customers need to have special characters, they need a way to replace your default CharacterFactory with theirs.

The character factory should then be able to produce different types of characters. This way, you can build default good and bad guys for your game. Customers can then override these methods to modify the defaults.

The facade is used to make an object appear as if it was of a different type. So your window might have a child of type View. A CharacterView would then be a facade which maps information from the character into data for the View interface which the window can then use to display information like health without knowing anything about the character or health.

This means you also need a configuration object that contains the default factories. Customers get one of those from the ConfigFactory.

class ConfigFactory:
    def createConfig(self):
        config = Config()
        config.characterFactory = self.createCharacterFactory()
        return config

    def createCharacterFactory(self):
        return CharacterFactory()

Customers will override this to get their own factory:

class CustomConfigFactory(ConfigFactory):
    def createCharacterFactory(self):
        return CustomCharacterFactory()
Sign up to request clarification or add additional context in comments.

5 Comments

Yes. Your example isn't a facade, it's a mix of factory, config factory, and config, plus a monkey wrench.
haha! Okay well thanks for the info, I need more convincing of this explanation, ill keep looking and understanding!
"level of abstraction" means "make independent of each other". What you did is you tried to move everyting into the Facade class instead of creating independent parts which you then wire together.
Ive tired to apply what this:en.wikipedia.org/wiki/Facade_pattern article suggests, the example (computer facade) is written in java, and i did it exactly like that. so my classes are window and character, with methods defined in them. like Character: rotate,move and window : setbackground_image. then I use the objects to create it to call on methods in facade.
But the article clearly states that a facade should have only a few methods (usually 1-2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.