简体   繁体   中英

Better class organization for my C# project

I'm trying to organize my functions and classes in better way. Currently I have following primary classes (Note: all these classes have lots of properties and functions, but for sake of clarification I wash them all away):

internal class Class0{ } 

internal class Class1: Class0 { }

internal class Class2: Class1 
{ 
    List<Class1> mainDataStructure; 
}

I'm quite OK with organization of above mentioned classes, but my main concern are following classes.

public class Class3: Class2
{
    InsertClass anInstance = new InsertClass();

    public void Insert(Item item)
    {
        anInstance.mainDataStructure = mainDataStructure;
        anInstance.Insert(item);
    }
}

internal class InsertClass
{
    List<Class1> mainDataStructure;

    internal void Insert(Item item)
    {
        // adds item to mainDataStructure.

        // some clean-ups using following function
        // how to clean-up ? User will define it.
        CleanUp();
    }

    // a function that use should implement
    // define it as:
    internal abstract string CleanUp();
    // or as:
    internal Func<string> CleanUp();
}

So as you noticed, my problem arises when I'm trying to define some classes (eg, Class4 ) that manipulate my mainDataStructure . What I can do (as I did here) is to pass the mainDataStructure to the classes that will manipulate it. Although it works, but for me it does not seem elegant. I'm wondering if there is any possibility of re-organizing my Class3 and Class4 in a better way.

Besides, the classes that manipulate mainDataStructure (eg, Class4 ) has some functions that should be defined by end-user ... which I'm totally stuck there! If I define the function as abstract the user must inherit the class and define the abstract function in the child class, or if I define them with delegates Func<> the user must pass the function definition. The main concern with these solutions are the accessibility level of the class! I'm highly recemended to have only Class3 as public. Therefore, inheritance is out of question, and for delegate, there should be a way in new organization to pass the function definition to Class4 from within Class3 .

I appreciate any help for this project organization.

Class Explanation
This project is an Information Retrieval project. mainDataStructure stores the information. Class3 is kinda user interface. The user begins interaction by getting an instance of this class; and does everything by that instance (eg, add new information, retrievals, statistics, and etc.). Class4 adds new information to mainDataStructure . Class5 retrieves the some kind of similarity between stored information. Class6 gets some statistics of mainDataStructure (eg, right-tailed probability) and so on so forth.

Example: add a new information item Use the instance of Class3 and call the public void Add(Item item) . This method will then use the instance of Class4 and it's internal void Add(Item item) method to take care of all new item insertion process. See modified above code

I propose a Factory combined with eg a Strategy .

public interface MainDataStructureManipulations {
   // define all needed functions here.
}

public abstract class Class3 {
    static Class3 getInstanceWith(MainDataStructureManipulations strategy) {
        return new Class4(strategy);
    }

    private Class3() {}
}

internal class Class4 : Class3 {
    MainDataStructureManipulations strategy;
    public Class4(MainDataStructureManipulations strategy) {
        this.strategy = strategy;
    }
}

You might not even need an additional Class4 if all your strategy requirements are covered with MainDataStructureManipulations .


A Factory is a method that creates an object instance for you. The great thing is you don't have to know exactly of which class this instance is, as long as it implements the base class the factory specified. In this case above, you do not have to expose Class4 , and the user of the library will never see it, either, but can interact with the instance just like with any instance of a class that is derived from Class3 .

A Strategy encapsulates functionality with simple methods. I chose it because it seemed to fit quite well to your description, but I might have been wrong. A strategy just holds the methods for interaction, if you want to store state maybe another pattern would be more appropriate. If you tell us what you are really trying to do we can give more detailed advice.

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