How does one class request information from another?

I am working on a VB.NET PDF export program for CAD drawings. Programs work fine, but architecture is a mess. Basically, one big function performs the whole process from start to finish. I would like to make a separate class or several to do the export job.

Here's the problem:
Sometimes the pdf file that will be created by my program already exists. In this case, I would like to ask the user if he wants to overwrite existing PDF files. I only want to do this if there is something that will be overwritten, and I only want to do it once. In other words, yes = yes to all. It seems wrong to have a form (which will call this new PDF export class), find out which PDF files will be called and whether there will be any rewritable ones. In fact, it would be better if the PDF file names were determined when processing individual CAD drawings (because I could use information that would be available only after downloading the files in the CAD program in the background).

Here's the question:
How should I handle the user request process? I would like to leave all the GUI logic (even something as simple as a dialog box) from my PDF export class. I need a way for the PDF export class: “Hey, I need to know if I need to overwrite or skip this file,” and the form class (or any other class) to say: “Um, well, I'll ask the user and get back to you "

There seems to be some pattern for this situation. What is it?

traceability:

Events: It seems like a good way. Is this about how the code should look in the PDF export class?

    Dim e As New FileExistsEventArgs (PDFFile)
    RaiseEvent FileExists (Me, e)
    If e.Overwrite Then
        'Do stuff here
    End if

: PDF ?

+3
5

, , . , , , . #, :

void form_Load(object sender,EventArgs e)
{
   //We are subscribing to the event here. In VB this is done differently
   pdfExporter.FileExists+=new FileExistsEventHandler(pdfExporter_fileExists)
}

void pdfExporter_fileExists(object sender, FileExistsEventArgs e)
{
   //prompUser takes the file and asks the user
   if (promptUser(e.FileName)) 
   {
   }
}
+4

PDF . eventargs, , "Overwrite", GUI . PDF, . Gui .

, , !

+1

,

[OverwriteExisting as Boolean = False]

, . , , PDF . , / , , True .

0

.

: , .

, , GUI , - . , . , , , - .

, , , , .

0

GUI PDF. - , .

, Status Progress. , Status - , .

, IStatusDisplay IPrograssDisplay .

GUI , IStatusDisplay ( IProgressDisplay), DLL, . DLL NullStatus NullProgress, , .

, , GUI. , Null. , , , , , IStatusDisplay, IProgressDisplay.

, GUI. , , . , .

EDIT You can create a request class and IPromptDisplay to handle the situation, for example, asking if you want to overwrite files.

for example

Dim P as New Prompt(MyPromptDisplay,PromptEnum.YesNo)
'MyPromptDisplay is of IPromptDisplay and was registered by the GUI when the application was initialized

If PromptYesNo.Ask("Do you wish to overwrite files")= PromptReply.Yes Then
    'Do stuff here
End If
0
source

Source: https://habr.com/ru/post/1703176/


All Articles