Created
October 14, 2019 08:59
-
-
Save kleviscipi/f7cb2499840ce41feb1ac9266607be94 to your computer and use it in GitHub Desktop.
Pyhon OOP Example Factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| #Class Name : Article | |
| # class Article: | |
| # # Class Attributes | |
| # title=str | |
| # author=str | |
| # date=str | |
| # category=int | |
| # # Initializer / Instance Attributes | |
| # def __init__(self, title, author, date, category): | |
| # self.title = title | |
| # self.author = author | |
| # self.date = date | |
| # self.category = category | |
| # # Instance method | |
| # def getData(self): | |
| # # Here is a pattern problem | |
| # # Every time we need to add a new type | |
| # # we go to update the class | |
| # # One of principles of a class is : | |
| # # The class should do one thing and should do it well | |
| # # or the class is created for one purpose | |
| # # Using this princicple we go to refactor the code | |
| # type_data = self.getDataType() | |
| # if type_data == "XML": | |
| # res = "" | |
| # res += "<article>" + str(self.title) + "<article>" | |
| # res += "<author>" + str(self.author) + "<author>" | |
| # res += "<date>" + str(self.date) + "<date>" | |
| # res += "<category>" + str(self.category) + "<category>" | |
| # return res | |
| # if type_data == "JSON": | |
| # return json.dump({ | |
| # "article":{ | |
| # "title":self.title, | |
| # "author":self.author, | |
| # "date":self.date, | |
| # "category":self.category | |
| # } | |
| # }) | |
| # #Instance method | |
| # def getDataType(self): | |
| # return "JSON" | |
| # 1 ) | |
| # First we make divide the tow types (JSON AND XML) | |
| # in tow classes JSONGenerator and XMLGenerator | |
| # which have just one method and as a first parameter | |
| # it take the Article object | |
| class JSONGenerator: | |
| def generate(self, obj): | |
| return json.dumps({ "article":{ | |
| "title":obj.title, | |
| "author":obj.author, | |
| "date":obj.date, | |
| "category":obj.category | |
| }}) | |
| class XMLGenerator: | |
| def generate(self,obj): | |
| res = "" | |
| res += "<article>" + str(obj.title) + "<article>" | |
| res += "<author>" + str(obj.author) + "<author>" | |
| res += "<date>" + str(obj.date) + "<date>" | |
| res += "<category>" + str(obj.category) + "<category>" | |
| return res | |
| # 2) | |
| # Second we go to modify the getData method on class Article | |
| # We go to add a new attribute *generator* which may | |
| # be the generator object JSONGenerator or XMLGenerator | |
| # Now the question is where we take the generator ? | |
| # For that may be more than one situation and solutions | |
| # And i'm go to make a simple example creating a Factory class | |
| # Class Name: Article | |
| class Article: | |
| # Class Attributes | |
| title=str | |
| author=str | |
| date=str | |
| category=int | |
| # Initializer / Instance Attributes | |
| def __init__(self, title, author, date, category): | |
| self.title = title | |
| self.author = author | |
| self.date = date | |
| self.category = category | |
| # Instance method | |
| def getData(self, generator): | |
| return generator.generate(self) | |
| # 3) | |
| # Create a Factory class for one purpose. | |
| # Should generate for us the generator object JSONGenerator or XMLGenerator | |
| # Class Name: Factory | |
| class Factory: | |
| # Class Attributes | |
| generator_type = 'XML' # Default | |
| #Initializer / Instance Attributes | |
| def __init__(self, generator_type = ''): | |
| if generator_type is not '': | |
| self.generator_type = generator_type | |
| #Instance Method | |
| def getGenerator(self): | |
| return eval(str(self.getGeneratorType())+str('Generator'))() | |
| #Instance method | |
| def getGeneratorType(self): | |
| # Here we make an exmaple of take the type of object | |
| # Which we can take bay an post request or we can set it a different way | |
| # It depend by the situation whchi you use the code. | |
| return self.generator_type | |
| # 4) | |
| # Use the code in a real life: | |
| Article = Article("Python is awesome","Klevis Cipi", '2019/10/14',2) | |
| Generator = Factory("XML") | |
| data = Article.getData( Generator.getGenerator() ) | |
| print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment