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
#2
Quote:If a user of this class wants to determine the mfg.year
Better to store the data in a dictionary IMHO. Have the key point to a list if you want to store more than the year.
class IPhone(): 
    def __init__(self,ver): 
        self.ver = ver
        self.ver_dic={'3G': 2007,
                      '3GS':2009,
                      '4':  2010,
                      '4s': 2011,
                      '5':  2012}
        self.phone_factory() 
     
    def phone_factory(self): 
        if self.ver in self.ver_dic:
            return self.ver_dic[self.ver]
        else: 
            ## Python returns None if there is no return
            ## so this is not necessary
            return None 
Reply
#3
I should elaborate a little further. Apologies.

mfg.year is just one attribute that I tried to demonstrate my problem with. Imagine if there were many such pieces of information,
for e.g:
1. Which apple factory location was that phone made in ?
2. Which apple warehouse did it get shipped to ?
3. How was it sold ? (online vs in store)

All these pieces of information would typically be stored in different databases, so separating them into sub-classes provides the flexibility to pull data from the correct database(s). However, what I am not sure of is since the user does not know which type of iphone he/she is instantiating, how to provide the user with visibility to sub-class attributes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which design / pattern when building classes and subclasses Phaze90 2 1,123 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,757 Feb-07-2022, 07:29 PM
Last Post: saavedra29
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,883 Jan-22-2021, 05:43 AM
Last Post: buran
  use of subclass ebolisa 4 2,206 Sep-17-2020, 01:08 PM
Last Post: jefsummers
  Accessing subclass with a variable David_S 2 2,128 May-19-2020, 05:55 PM
Last Post: David_S
  How can I create a subclass of XlsxWriter? aquerci 2 2,048 May-04-2020, 07:41 PM
Last Post: aquerci
  Factory Design Pattern Prince 1 2,471 Apr-06-2018, 10:00 AM
Last Post: Larz60+
  dynamically creating a subclass sidereal 2 4,872 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