You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromabcimportABC, abstractmethodfromsysimportplatformclassButton(ABC):
@abstractmethoddefpaint(self):
passclassLinuxButton(Button):
defpaint(self):
return"Render a button in a Linux style"classWindowsButton(Button):
defpaint(self):
return"Render a button in a Windows style"classMacOSButton(Button):
defpaint(self):
return"Render a button in a MacOS style"classGUIFactory(ABC):
@abstractmethoddefcreate_button(self):
passclassLinuxFactory(GUIFactory):
defcreate_button(self):
returnLinuxButton()
classWindowsFactory(GUIFactory):
defcreate_button(self):
returnWindowsButton()
classMacOSFactory(GUIFactory):
defcreate_button(self):
returnMacOSButton()
ifplatform=="linux":
factory=LinuxFactory()
elifplatform=="darwin":
factory=MacOSFactory()
elifplatform=="win32":
factory=WindowsFactory()
else:
raiseNotImplementedError(f"Not implemented for your platform: {platform}")
button=factory.create_button()
result=button.paint()
print(result)
JavaScrip implementation:
functionEmployee(name){this.name=name;this.say=function(){console.log("I am employee "+name);};}functionEmployeeFactory(){this.create=function(name){returnnewEmployee(name);};}functionVendor(name){this.name=name;this.say=function(){console.log("I am vendor "+name);};}functionVendorFactory(){this.create=function(name){returnnewVendor(name);};}functionrun(){varpersons=[];varemployeeFactory=newEmployeeFactory();varvendorFactory=newVendorFactory();persons.push(employeeFactory.create("Joan DiSilva"));persons.push(employeeFactory.create("Tim O'Neill"));persons.push(vendorFactory.create("Gerald Watson"));persons.push(vendorFactory.create("Nicole McNight"));for(vari=0,len=persons.length;i<len;i++){persons[i].say();}}