diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index f584954..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) [2020] [Reza Mobaraki] | MR.Rezoo - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file diff --git a/src/factory2.py b/src/factory2.py index 46e4eb0..5631dfb 100644 --- a/src/factory2.py +++ b/src/factory2.py @@ -1,40 +1,54 @@ """ - Creational: - Factory method - 3 Component => 1.Creator, 2.Product, 3.Client + Factory + - Factory is a creational design pattern that provides an interface for creating objects + in a superclass, but allows subclasses to alter the type of objects that will be created. + + 3 component => 1. creator, 2. product, 3. client """ -from abc import abstractmethod +from abc import ABC, abstractmethod -class File: - def __init__(self, name, file_format): - self.name = name - self.file_format = file_format +class File(ABC): # creator + def __init__(self, file): + self.file = file + @abstractmethod + def make(self): + pass -class B: + def call_edit(self): + product = self.make() + result = product.edit(self.file) + return result - def edit(self, file): # client - return self._get_edit(file) - def _get_edit(self, file): # creator - if file.file_format == 'json': # identifier - return self.json_edit(file) - elif file.file_format == 'xml': # identifier - return self.xml_edit(file) - else: - raise ValueError("So Sorry. . .") +class JsonFile(File): # creator + def make(self): + return Json() - @abstractmethod - def json_edit(self, file): # product - print(f"Editing Json File. . . {file.name}") - @abstractmethod - def xml_edit(self, file): # product - print(f"Editing Xml File. . . {file.name}") +class XmlFile(File): # creator + def make(self): + return Xml() + + +class Json: # product + def edit(self, file): + return f'Working on {file} Json...' + + +class Xml: # product + def edit(self, file): + return f'Working on {file} Xml...' + + +def client(file, format): # client + formats = { + 'json': JsonFile, + 'xml': XmlFile + } + result = formats[format](file) + return result.call_edit() -if __name__ == '__main__': - first_file = File('first', 'xml') - b1 = B() - b1.edit(first_file) +print(client('show', 'xml')) diff --git a/src/singleton2.py b/src/singleton2.py index 2189d37..23a2b39 100644 --- a/src/singleton2.py +++ b/src/singleton2.py @@ -4,7 +4,7 @@ """ -class Singleton(type): +class Singleton1(type): _instance = None def __call__(cls, *args, **kwargs): @@ -13,17 +13,36 @@ def __call__(cls, *args, **kwargs): return cls._instance -class DB(metaclass=Singleton): +class Singleton2(type): + """ + Singleton metaclass + Based on Python Cookbook 3rd Edition Recipe 9.13 + Only one instance of a class can exist. Does not work with __slots__ + """ + + def __init__(self, *args, **kw): + super(Singleton2, self).__init__(*args, **kw) + self.__instance = None + + def __call__(self, *args, **kw): + if self.__instance is None: + self.__instance = super(Singleton2, self).__call__(*args, **kw) + return self.__instance + + +class DBMongo(metaclass=Singleton2): + pass + +class DBPostgres(metaclass=Singleton2): pass if __name__ == '__main__': - s1 = DB() - s2 = DB() - s3 = DB() + m1 = DBMongo() + m2 = DBMongo() - print(id(s1)) - print(id(s2)) - print(id(s3)) + p1 = DBPostgres() + p2 = DBPostgres() - print(id(s1) == id(s2) == id(s3)) + print(f"Mongo: {id(m1)} - {id(m2)} ", id(m1) == id(m2)) + print(f"Postgres: {id(p1)} - {id(p2)} ", id(p1) == id(p2))
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: