Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

python - What OOP techniques are used in this code? Is it aggregation, composition, generalization, association or just dependency?

I would like to ask whether the aggregation or dependency is used in this code? In other words, how is class StartPage() dependant on the class SeaofBTCapp?

import tkinter as tk

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        frame = StartPage(container, self)
        self.frames[StartPage] = frame
        frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page")
        label.pack(pady=10,padx=10)

app = SeaofBTCapp()
app.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

how is class StartPage() dependant on the class SeaofBTCapp?

StartPage is designed to act as a single page in a multi-page app. It requires that some other object or program is responsible for creating it and causing it to be displayed. Loosly speaking, this is a view in the model, view, controller pattern.

SeaofBTCapp is a class designed to display one page at a time, where pages are implemented as frames composed of one or more additional widgets. It acts as the controller in the model, view, controller pattern.

Using your terminology, SeaofBTCapp is an aggregated object since it is composed of many pages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...