lundi 19 juillet 2021

Abstract Designing Pattern

I was learning Abstract Designing Pattern and I have created the below code for understanding. My question is why I am seeing 'None' in the output of this code. I have created a Chair and Bed of two types Modern and Old. And I have created a factory for each type for producing that type of product.

Just getting started so maybe it's some silly mistake that I can't find :))

Code:

from abc import ABC, abstractmethod

class Chair(ABC):
    @abstractmethod
    def get_type():
        pass

class ModernChair(Chair):
    def get_type(self):
        print("Modern Chair!")

class OldChair(Chair):
    def get_type(self):
        print("Old Chair!")
        
class Bed(ABC):
    @abstractmethod
    def get_type():
        pass

class ModernBed(Bed):
    def get_type(self):
        print("Modern Bed!")

class OldBed(Bed):
    def get_type(self):
        print("Old Bed!")

class BaseFactory(ABC):
    @abstractmethod
    def get_chair():
        pass
    
    @abstractmethod
    def get_bed():
        pass

class ModernFactory(BaseFactory):
    @staticmethod
    def get_chair():
        return ModernChair()
    
    @staticmethod
    def get_bed():
        return ModernBed()

class OldFactory(BaseFactory):
    @staticmethod
    def get_chair():
        return OldChair()
    
    @staticmethod
    def get_bed():
        return OldBed()

m_chair = ModernFactory.get_chair()
m_bed = ModernFactory.get_bed()
o_chair = OldFactory.get_chair()
o_bed = OldFactory.get_bed()

for obj in list([m_chair, m_bed, o_chair, o_bed]):
    print(obj.get_type())

Output:

Modern Chair!
None
Modern Bed!
None
Old Chair!
None
Old Bed!
None

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire