Facade
In different implementations and approaches, Facade may resemble either an Adapter or an Abstract Factory. Its goal seems straightforward—to simplify interacting with another class or a family of classes: When we think about simplifying, we usually think of the Adapter design pattern When we think about the family of classes, we usually think of an Abstract Factory That's where all the confusion usually comes from. To better understand it, let's go back to the example we used for the Abstract Factory design pattern.
Keep it simple Let's say that we would like to implement the loadGame() method. This method would take a file we already created (we'll discuss how later), or, at the least, the following will be required: At least two HQs will have to be created (otherwise, the game is already won) Each HQ will have to produce the buildings it had Each building will have to produce the units it had All units will have to magically teleport to the positions they were at when the game was saved If there were any commands given to units (like obliterating all enemy bases), they should resume executing them We'll discuss how we actually give commands to our units, with the Command design pattern. Stay tuned. Now, usually, it's not just one person working on a game unless it's Minecraft (TM). There's that other guy, Abhinaw, who deals with all the command logic. He's not much into constructing buildings. But in his role, he also needs to load saved games quite often. As the developer of all those bases that belong to you, you could give him a set of instructions you've written on how the game should be properly loaded. He may or may not follow this set of instructions. Maybe he'll forget to move the units, or build buildings. And the game will crash. You could use the Facade design pattern to simplify the job for him. What's the main problem Abhinaw has right now? To load a game, he needs to interact with at least three different interfaces: HQ Building Unit What he would like is to have only one interface, something like: interface GameWorld That has exactly the methods he needs: fun loadGame(file: File) GameWorld Hey, but that looks like a Static Factory Method there! Yep, sometimes, design patterns are embedded into one another. We use the Static Factory Method to create our class, but its goal is to be a Facade for other more complex classes. Using a Facade doesn't mean we don't expose interfaces our facade hides behind to the client. Abhinaw can still use every small unit to issue orders after the game is loaded successfully. Simple, right?
Note : take reference from my blog Abstract Factory , Factory , Adapter Design Pattern for code samples.
1.https://androidcodingworld.blogspot.com/2023/05/abstract-factory-in-kotlin-comparative.html
2.https://androidcodingworld.blogspot.com/2023/05/adapter-design-pattern-in-kotlin.html
3.https://androidcodingworld.blogspot.com/2023/05/static-factory-method-in-kotlin.html
4.https://androidcodingworld.blogspot.com/2023/05/kotlin-factory-method-comparative.html
No comments:
Post a Comment