Python Forum
which design / pattern when building classes and subclasses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which design / pattern when building classes and subclasses
#1
I need to build a mother-child-like class tree.

Dont know which is the best practice how to structure it.

I could define a top-level class A which has some own attributes and at least 1 sub class B.
The class B itself has again some own attributes and at least 1 sub class C, and so on...
I see 2 ways:
(1)
call instance of Class A, which has a method "fill_own_attributes" and "add subclass(B)".
When the instance of A is called it automatically sets its attributes and adds the subclass B.
When the instance of B is called it automatically sets its attributes and adds the subclass C.

(2)
Having a global builder-function which starts creating a temporary instance of Class C, setting all its attributes.
When all is done, adding this instance as a child to a new instance of Class B, then setting all attributes of B.
When all is done, adding this instance as a child to a new instance of Class A, then setting all attributes of A, which is the highest class. Then its done.

I think first one looks better, but is there any other way? Someone has any info material about my problem?
Reply


Messages In This Thread
which design / pattern when building classes and subclasses - by Phaze90 - Nov-18-2022, 08:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,266 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  Classes, OOP, building deck of 52 playing cards Drone4four 9 4,027 Jan-19-2022, 02:53 PM
Last Post: Drone4four
  User Subclasses holyghost 6 3,261 Mar-17-2021, 12:33 PM
Last Post: buran
  Design Pattern for accessing subclass attributes UGuntupalli 2 2,132 Jul-30-2019, 11:09 PM
Last Post: UGuntupalli
  Factory Design Pattern Prince 1 2,516 Apr-06-2018, 10:00 AM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 5,146 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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