Python Forum
Design Pattern for accessing subclass attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Design Pattern for accessing subclass attributes
#1
All,
I am looking for guidance on what would be good design pattern to use / implement given a particular problem.

Problem Statement:
Let us say we would like to define a class that defines i-phone. Please note the below example is only for illustration.
If a user of this class wants to determine the mfg.year which is only set in the sub-class, what is the best way to get that information, without knowing which sub-class to call.
The user is always only going to instantiate the base class that is of the IPhone().

class IPhone(): 
    def __init__(self,ver): 
        self.ver = ver
        self.phone_factory() 
    
    def phone_factory(self): 
        if self.ver == '3G':
            return IPhone3G()
        elif self.ver == '3GS': 
            return IPhone3GS() 
        elif self.ver == '4': 
            return IPhone4()
        elif self.ver == '4s': 
            return IPhone4s()
        elif self.ver == '5': 
            return IPhone5() 
        else: 
            pass 
        
class IPhone3G(): 
    def __init__(self): 
        self.year = 2007 

class IPhone3GS(): 
    def __init__(self): 
        self.year = 2009 

class IPhone4(): 
    def __init__(self): 
        self.year = 2010
    
class IPhone4s(): 
    def __init__(self): 
        self.year = 2011

class IPhone5(): 
    def __init__(self): 
        self.year = 2012
Potential Solution:
One way I can think of addressing this problem is by doing the following:

class IPhone(): 
    def __init__(self,ver): 
        self.ver = ver
        self.phone_factory() 
    
    def phone_factory(self): 
        if self.ver == '3G':
            t = IPhone3G()
            self.year = t.year
        elif self.ver == '3GS': 
            t = IPhone3GS() 
            self.year = t.year
        elif self.ver == '4': 
            t = IPhone4()
            self.year = t.year
        elif self.ver == '4s': 
            t = IPhone4s()
            self.year = t.year
        elif self.ver == '5': 
            t = IPhone5() 
            self.year = t.year
        else: 
            pass 
        
class IPhone3G(): 
    def __init__(self): 
        self.year = 2007 

class IPhone3GS(): 
    def __init__(self): 
        self.year = 2009 

class IPhone4(): 
    def __init__(self): 
        self.year = 2010
    
class IPhone4s(): 
    def __init__(self): 
        self.year = 2011

class IPhone5(): 
    def __init__(self): 
        self.year = 2012
But, is this a good approach ? I have looked at class methods and static methods as alternative options but I am not really sure what is the best approach. Can anybody advice?
Reply


Messages In This Thread
Design Pattern for accessing subclass attributes - by UGuntupalli - Jul-30-2019, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  which design / pattern when building classes and subclasses Phaze90 2 1,153 Nov-19-2022, 08:42 AM
Last Post: Gribouillis
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,885 Feb-07-2022, 07:29 PM
Last Post: saavedra29
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,932 Jan-22-2021, 05:43 AM
Last Post: buran
  use of subclass ebolisa 4 2,272 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  Accessing subclass with a variable David_S 2 2,177 May-19-2020, 05:55 PM
Last Post: David_S
  How can I create a subclass of XlsxWriter? aquerci 2 2,106 May-04-2020, 07:41 PM
Last Post: aquerci
  Factory Design Pattern Prince 1 2,515 Apr-06-2018, 10:00 AM
Last Post: Larz60+
  dynamically creating a subclass sidereal 2 4,925 Jan-04-2018, 11:10 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020