Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 工厂方法分解为模块_Design Patterns_Python 3.6_Factory Pattern - Fatal编程技术网

Design patterns 工厂方法分解为模块

Design patterns 工厂方法分解为模块,design-patterns,python-3.6,factory-pattern,Design Patterns,Python 3.6,Factory Pattern,我一直在看一个简单的工厂示例 from __future__ import generators import random class Shape(object): # Create based on class name: def factory(type): #return eval(type + "()") if type == "Circle": return Circle() if type == "Square":

我一直在看一个简单的工厂示例

from __future__ import generators
import random

class Shape(object):
    # Create based on class name:
    def factory(type):
        #return eval(type + "()")
        if type == "Circle": return Circle()
        if type == "Square": return Square()
        assert 0, "Bad shape creation: " + type
    factory = staticmethod(factory)

class Circle(Shape):
    def draw(self): print("Circle.draw")
    def erase(self): print("Circle.erase")

class Square(Shape):
    def draw(self): print("Square.draw")
    def erase(self): print("Square.erase")

# Generate shape name strings:
def shapeNameGen(n):
    types = Shape.__subclasses__()
    for i in range(n):
        yield random.choice(types).__name__

shapes = \
  [ Shape.factory(i) for i in shapeNameGen(7)]

for shape in shapes:
    shape.draw()
    shape.erase()
并试图将其分为不同的文件

main.py 圆圈.py square.py 当运行它时,我得到 ImportError:无法导入名称“Circle”


因此,尽管当所有类都在同一个模块中时,该示例仍然有效,但从分离的模块导入它们时似乎存在问题。有什么想法吗?

我刚刚将factory类(Shape)与其他形状继承自的基类分开,这似乎起到了作用

from __future__ import generators
import random

from factory.shapes.circle import Circle
from factory.shapes.sqaure import Square


class Shape(object):
    # Create based on class name:
    def factory(type):
        #return eval(type + "()")
        if type == "Circle": return Circle()
        if type == "Square": return Square()
        assert 0, "Bad shape creation: " + type
    factory = staticmethod(factory)


# Generate shape name strings:
def shapeNameGen(n):
    types = Shape.__subclasses__()
    for i in range(n):
        yield random.choice(types).__name__

shapes = \
  [ Shape.factory(i) for i in shapeNameGen(7)]

for shape in shapes:
    shape.draw()
    shape.erase()
from __future__ import generators
import random

from factory.main import Shape

class Circle(Shape):
    def draw(self): print("Circle.draw")
    def erase(self): print("Circle.erase")
from __future__ import generators
import random

from factory.main import Shape

class Square(Shape):
    def draw(self): print("Square.draw")
    def erase(self): print("Square.erase")