简体   繁体   中英

Class programming approach for child/parent relationship

I'm developing a program that scrapes content from various online archives, but am new to OOP. The way I believe would work best would be to have a parent class that specifies the shared variables and methods, then a child class for each archive which then contains the specific methods for grabbing info from that particular site, such as GrabStoryVariables() existing in each child class to accommodate the individual needs of that archive. The program takes a URL in a textbox, and then from there it will determine using the URL which child class to instantiate.

The problem I'm having is figuring out how to create the child class object and make it accessible to the entire program. For example, to create an instance of FanFictionAuthors : FanBook:

private void btnGetInfo(object sender, EventArgs e)
{
  CreateBook();
}

private void CreateBook()
{
  if (addressBar.Text.Contains("fanficauthors.net"))
  {
    FanFictionAuthors myBook = new FanFictionAuthors();
  }
  return;
}

The scope of myBook is just the CreateBook() function, so this approach won't do the trick. Any suggestions on the best way to handle this issue? I'm using this as an approach to better learn programming, so the "correct" way is what I'm trying to figure out, whatever that is.

Edit: The specific function of the program is to take a provided URL for an online story from fanfiction.net, fictionpress.com, or one of any number of other online story archives. There are a set of shared attributes each story will have, such as title, number of chapters, length in words, chapter titles, and the actual content of the story. The program compiles all of this to create a single html document (later to be expanded to allow for different ebook formats) rather than a bunch of small individual chapter files.

With that in mind, the only parts that should differ between each archive are the methods for grabbing the variables from the particular archive and how to iterate between the chapters based on the archive's function for that.

Currently what I'm doing is just creating a myBook object immediately upon launching the main form, then creating a different method name for the functions that grab the variables and do the iteration. As I add more archives, however, this becomes more complicated. What I originally wanted to do was to just cast the myBook to the individual archive types (FanFictionAuthors in this case) to grab the ability to use their specific functions. Looking online, it appears casting from parent to child isn't easy nor recommended, so I'm not sure how else to approach this.

Here's the GitHub link for the project. This version is slightly out of date, but lets you see how I'm currently approaching this: https://github.com/benroth/fBook

Create a super class where you common attributes and methods in there:

public class FanBook
{
    // use a common constructor
    public FanBook(string url)
    {
        grabHtml(url);
        // ...
    }

    protected string grabHtml(string address) { // SNIP }

    protected void CreateStoryHeader() { // SNIP }

    // other common methods which are the same for every subclass (maybe BuildToc, GetStory, etc.)

    // maybe if you want some easy access to attributes, you could add a dictionary
    public void Dictionary<string, string> Attributes;

    // Then use abstract methods to define methods that are different for subclasses
    protected abstract void GrabStoryVariables();
    protected abstract void GenerateStoryInfo();
}

Then create a subclass that derives from Book :

public class FFNETBook : FanBook {
    // FFNETBook constructor to call contructor from FanBook too
    public FFNETBook(string url) : FanBook(url) {
        // specific initializations for FFNET

    }

    public override void GrabStoryVariables() { // special implementation for FFNET here }

    public override void GenerateStoryInfo() { // special implementation for FFNET here }
}

I know OOP is hard to grasp when you don't have much experience in it. So feel free to ask questions. If you do it right, then you would never need to cast into subclasses.

To answer the question in the comment:

You could make a class variable in the form1.cs file:

private FanBook currentBook;

private void CreateBook()
{
    currentBook = new FFNETFanBook("http://...");
}

private void AnotherMethod() {
    if ( currentBook != null ) {
        currentBook.GrabStoryVariables();
    } else {
        throw new Exception("Book not initialized yet.");
    }
}

Implement an interface named IAuthors and define the Method you want

Interface IAuthors
{
//method
void authorMethod();
}

Implement the interfaces across your classes

Public Class FanFictionAuthors:IAuthors
{
public void authorMethod()
{
//fanfiction specific action
}
}

Public class SciFiAuthors:IAuthors
{
public void authorMethod()
{
//scifiauthor specific action
}
}

Now make the following changes in your existing code

private void btnGetInfo(object sender, EventArgs e)
{
  IAuthors auth=CreateBook();//Use a Interface ref
  auth.authorMethod();//Runtime will decide which authorMethod to call depending 
                      //on the object returned.
}

//Note that i am returning the Interface type instead of the void
private IAuthors CreateBook()
{
  if (addressBar.Text.Contains("fanficauthors.net"))
  {
    return new FanFictionAuthors();//Return your object
  }elseif(addressBar.Text.Contains("scificauthors.net"))
  {
    return new SciFiAuthors();//Return your object
  }
}

You could create a list in the parent and add childs to that list. Then you can akso walk that list to see if childs are finished and such.

ChildList.Add(new fan....)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM