-1

Whenever we are able to call one function from different classes, would it be considered as Facade design pattern?

class Mobile:
    def __init__(self, name, ram, memory):
        self.name = name
        self.ram = ram
        self.memory = memory
    
    def info(self):
        return self.name, self.ram, self.memory
    
class Printer:
    def __init__(self, name, size, color):
        self.name = name
        self.size= size
        self.color= color
    
    def info(self):
        return self.name, self.size, self.color
    
    
mob = Mobile("Dark", 8, 128)        
prt = Printer("Alpine", '4-5', 'white')   

print(mob.info())
print(prt.info())

In the above code we are using the same function for both the classes. Would this be considered as a Facade design pattern?

Rohit_VE
  • 109
  • 5
  • Can you clarify how you think this relates to the Facade pattern *or* duck-typing? Facade *wraps*, *abstracts* or *bundles* other code, which your code does not do. Duck typing is a way of polymorphism, which your code also does not do. You've just *duplicated* a class, which will of course behave the same – no facade is involved and no duck typing is needed here. – MisterMiyagi May 17 '21 at 13:26
  • Is it necessary that a duplicate class is not a different class altogether? What change do you think would happen if we change the class formation? It would still be another class with the same function 'info' and anyways Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. – Rohit_VE May 17 '21 at 18:17

1 Answers1

0

A façade provides abstraction through a single interface

You're not abstracting anything here, just defining two independent instance methods, which can be done in any OOP language, so duck typing is irrelevant

Note that your classes are the exact same, so you don't need two classes, only two instances of class Mobile

class Mobile:
    def __init__(self, name, ram, memory):
        self.name = name
        self.ram = ram
        self.memory = memory
    
    def info(self):
        return self.name, self.ram, self.memory    
    
    
mob1 = Mobile("Dark", 8, 128)        
mob2 = Mobile("Alpine", 16, 128) 

If you defined an abstract base class, then you'd be using inheritance with subclasses, and still not a façade

An example of a Façade in your case would be some operator routing a call/message (through a single interface) to any "mobile", where the mobiles could have completely different ways to contact them (which contact() could be defined as an abstract function in a parent class)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I think whatever the input you have given is correct, however, you are too much focused on the classes rather than design itself, a Facade is basically generating a simple interface to the complex sub- systems, it can be abstract, bundles, or streamlining. an abstract factory pattern is also a Facade design pattern. Now my question was, as Duck Typing is some how making a simple interface to multiple methods of different classes. Would you consider that as a Facade design pattern? – Rohit_VE May 18 '21 at 09:38
  • As answered, duck typing is irrelevant to the question. More than one (unrelated) class sharing the same method signatures is not a façade, and can be done without duck typing. The most important aspect of a façade is the "underlying, complex subsystem" that it hides, not so much the methods/objects used to access it.. There's an example here - the Façade itself is the higher level class https://sourcemaking.com/design_patterns/facade/python/1 – OneCricketeer May 18 '21 at 12:17