1. 정의

  2. FlyweightFactory란

  3. Client

  4. 사용 시점

  5. 사용효과

  6. UML

  7. Code

    class TextStyleFlyweight:
        
        def __init__(self, font_info, color):
            self.font_info = font_info
            self.color = color
            
        def get_font(self):
            return self.font
        
        def get_color(self):
            return self.color
        
        def to_string(self):
            return "(" + self.font_info.to_string() + ',' + self.color + ")"
    
    class TextStyleFlyweightFactory:
        _instance = None
        pool = {}
        
        @classmethod
        def get_instance(cls, *args, **kargs):
            if not cls._instance :
                print('?')
                cls._instance = cls(*args, **kargs)
                print(cls._instance )
            return cls._instance 
    
        def get_text_style_flyweight(self, name):
            text_style = TextStyleFlyweightFactory.pool.get(name)
            return text_style
        
        def put_text_style_flyweight(self, name, text_style):
            TextStyleFlyweightFactory.pool[name] = text_style
    
    class FlyweightConstants:
        
        NUMBER_STYLE_NAME = 'number'
        ANSWER_STYLE_NAME = 'answer'
        
        CLOUMN_WIDTH = 50
        ROW_HEIGHT = 50
        
        OPERATORS = ("+","-","*","/")
        EQUAL_CHAR = "="
        
        DEFAULT_NUMBER_FONT_INFO = FontInfo("Times",18)
        DEFAULT_ANSWER_FONT_INFO = FontInfo("Times",26)
    
    class FontInfo:
        def __init__(self, name, size):
            self.name = name
            self.size = size
        
        def get_name(self):
            return self.name
        
        def set_name(self, name):
            self.name = name
        
        def get_size(self):
            return self.size
        
        def set_size(self, size):
            self.size = size 
        
        def to_string(self):
            return "(" + self.name + "," + str(self.size) + ")"
    
    class PrintAnswer:
        
        def __init__(self):
            self.text_style_flyweight_factory = TextStyleFlyweightFactory.get_instance()
            self.first_number = 0
            self.second_number = 0 
        
        def print_result(self):
            answers = [0,0,0,0]
            
            answers[0] = self.first_number + self.second_number
            answers[1] = self.first_number - self.second_number
            answers[2] = self.first_number * self.second_number
            answers[3] = self.first_number / self.second_number
            
            i = 0
            
            for answer in answers:
                operator = FlyweightConstants.OPERATORS[i]
                text_array = ["", "", "", "",""]
                text_array[0] = "" + str(self.first_number) + self._get_text_style(FlyweightConstants.NUMBER_STYLE_NAME).to_string()
                text_array[1] = operator
                text_array[2] = "" + str(self.second_number) + self._get_text_style(FlyweightConstants.NUMBER_STYLE_NAME).to_string()
                text_array[3] = FlyweightConstants.EQUAL_CHAR
                text_array[4] = "" + str(answer) + self._get_text_style(FlyweightConstants.ANSWER_STYLE_NAME).to_string()
                
                print(str(text_array))
                i += 1
            
        def _get_text_style(self, name):
            return self.text_style_flyweight_factory.get_text_style_flyweight(name)
        
        def set_first_number(self, first_number):
            self.first_number = first_number
        
        def set_second_number(self, second_number):
            self.second_number = second_number
    
    class Client:
        def setup_text_style_flyweight_factory(self):
            text_style_flyweight_factory = TextStyleFlyweightFactory.get_instance()
            
            name = FlyweightConstants.NUMBER_STYLE_NAME
            text_style = TextStyleFlyweight(FlyweightConstants.DEFAULT_NUMBER_FONT_INFO,"red")
            text_style_flyweight_factory.put_text_style_flyweight(name, text_style)
            
            name = FlyweightConstants.ANSWER_STYLE_NAME
            text_style = TextStyleFlyweight(FlyweightConstants.DEFAULT_ANSWER_FONT_INFO,"black")
            text_style_flyweight_factory.put_text_style_flyweight(name, text_style)
        def main(self):
            client = Client()
            client.setup_text_style_flyweight_factory()
            
            print_answer = PrintAnswer()
            
            print_answer.set_first_number(10)
            print_answer.set_second_number(20)
            
            print_answer.print_result()
            
    client = Client()
    client.main()