Skip to content

Instantly share code, notes, and snippets.

@sujayy1983
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujayy1983/091f769ed9fd62cd4fd4 to your computer and use it in GitHub Desktop.
Save sujayy1983/091f769ed9fd62cd4fd4 to your computer and use it in GitHub Desktop.
An attempt at creating a visitor design pattern in Python. Tested with Python 2.7.6
"""
@Author: Sujayyendhiren Ramarao
@Description: Visitor design pattern
"""
# For creating abstract base class.
import abc
""" Add features to this class using Visitor design pattern"""
class SmartTV (object):
def __init__(self):
self.diagonal = 50 #Diagonal length in inches
self.mirroring = True #Mirroring with handheld devices
self.applicationExtensibility = True #More applications can be added to software
def __str__(self):
return "\n\nDiagonal length: " + str(self.diagonal) + "\nMirroring feature: " + str(self.mirroring) + "\nApplication additions: " + str(self.applicationExtensibility)
def features(self):
return str(self)
class AbsVisitor( object ):
""" Abstract class for Visitor features."""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def visit(self, platform ):
return
class AbsElement( object ):
""" Abstract class for SmartTV new feature that implements accept """
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def accept(self, input):
return
class AddSmartFeaturesVisitor( AbsVisitor):
""" Implementing skype video feature to SMART TV of different platforms"""
def visit(self, platform ):
if isinstance( platform, SmartTVAndSkypeVideoLinux):
print("\nLinux platform ")
self.skypeAppLinux(platform)
elif isinstance( platform, SmartTVAndSkypeVideoIOS ):
print("\nIOS platform")
self.skypeAppIOS(platform)
elif isinstance( platform, SmartTVAndSkypeVideoTizenOS ):
print("\nTizen platform")
self.skypeAppTizenOS(platform)
def videoCall(self):
print("Create video call session.")
def skypeAppLinux(self, inp):
print("Linux specific initialization")
#print( isinstance(inp) )
self.videoCall()
print("Linux specific cleanup")
def skypeAppIOS(self, inp):
print("IOS specific initialization")
self.videoCall()
print("IOS specific cleanup")
def skypeAppTizenOS(self, inp):
print("Tizen specific initialization")
self.videoCall( )
print("Tizen specific cleanup")
class SmartTVAndSkypeVideoIOS( SmartTV , AbsElement ):
def __init__(self):
pass
def accept( self, visitor ):
visitor.visit( self )
def __str__(self):
return "Skype feature added in SmartTVAndSkypeVideoIOS."
class SmartTVAndSkypeVideoLinux(SmartTV , AbsElement):
""" Smart TV for Linux"""
def __init__(self):
pass
def accept( self, visitor ):
visitor.visit( self )
def __str__(self):
return "Skype feature added in SmartTVAndSkypeVideoLinux."
class SmartTVAndSkypeVideoTizenOS(SmartTV , AbsElement):
""" Smart TV for TizenOS """
def __init__(self):
pass
def accept( self, visitor ):
visitor.visit( self )
def __str__(self):
return "Skype feature added in SmartTVAndSkypeVideoTizenOS."
if __name__ == "__main__":
smartTV = SmartTVAndSkypeVideoIOS()
visitor = AddSmartFeaturesVisitor()
#New feature accessed for IOS
smartTV.accept( visitor )
smartTV = SmartTVAndSkypeVideoLinux()
#New feature accessed for Linux
smartTV.accept( visitor )
smartTV = SmartTVAndSkypeVideoTizenOS()
#New feature accessed for TizenOS
smartTV.accept( visitor )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment