简体   繁体   中英

A design for sending objects to be added to the appropriate data structure

I have a class called DataStructures where I have a set of public static data structures that store objects. To add an object to a data structures is an involved process requiring a number of checks to be carried out, processes to be remembered and data to be rearranged. In another class called Foo , I need to add objects to the data structures. I was thinking I can do this by making a method called ObjectFeed which would take an object and the object's label as parameters. The label would tell the method which of the data structures the object should be added to. I would also have a method called addObject which would take the object to append and the appropriate target data structure as parameters:

Public Class DataStructures
{
    public static List<obj> object1Storage = new List<obj>();
    public static List<obj> object2Storage = new List<obj>();
    ...
}

Public Class Foo
{
    public void ObjectFeed(/* PARAMETERS */)
    {
      //Code that generates an object called inspectionObject
      //inspection object has an associated enum Type
        if(objectType == Type.Type1)
        {
             addObject(inspectionObject, DataStructures.object1Storage);
        }
        if(objectType == Type.Type2)
        {
             addObject(inspectionObject, DataStructures.object2Storage);
        }
        ...
    }

    private void addObject(obj inspectionObject, List<obj> objStorage)
    {
        objStorage.Add(inspectionObject);
        //And a lot more code
    }
}

Passing a public data structure as a parameter to a method that can just as well access that data structure directly doesn't feel correct. Is there a more clever and less intuitive way of doing this?

Edit:

In the example I originally contrived, the ObjectFeed method served no apparent purpose. I rewrote the method to look more like a method from the real world.

Where is the object type coming from? Passing a string value as a type of something is very rarely a good idea. Consider different options:

  1. Create an enum for these values and use this. You can always parse it from string or print it to string if you need to.
  2. Maybe it makes sense to have a couple of specific methods: FeedObjectType1(object obj), etc.? How often will these change?

Its really difficult to give you a definite answer without seeing the rest of the code.

Exposing public static lists from your DataStructures class is in most cases not a good design. To start with I would consider making them private and providing some methods to access the actual functionality that is needed. I would consider wrapping the lists with the addObject method, so that you don't have to pass the list as an argument. But again I am not sure if it makes sense in your case.

You seem to use DataStructures like some kind of global storage. I don't know what you store in there so I'm going to assume you have good reasons for this global storage.

If so, I would replace each list with a new kind of object, which deals with additions of data and does the checks relevant for it.

Something like:

interface IObjectStorage
{
   void Add(object obj);
   void Remove(object obj);
}

Each object storage type would derive from this and provide their own logic. Or it could derive from Collection<T> or something similar if collection-semantics makes sense. As your example is right now, I can't see the use for ObjectFeed, it serves as a fancy property accessor.

Selecting which property to access through a string sounds iffy to me. It is very prone to typos; I would rather use Type -objects available from any object in C# through the GetType-method or typeof() construct.

However. The whole setup feels a bit wrong to me, DataStructures et al.

First, testing your static class will be hard. I would pass around these stores to the types that need them instead. Replacing them with other stuff will also be hard, using interfaces will at least not tie you to a concrete implementation, but what if you want to use another location to store the objects in other code? Your static class is no longer relevant and you'll need to change a lot of code.

Maybe these things are out of your control, I don't know, the example code is a bit vague in that sense.

As pointed out in other answers:

  1. The public static List s are bad practice
  2. Since the addObject method is the same for every data structure, it should be implemented as a data structure accessor.

To this end, I moved the instantiation of the data structures into Foo and moved the addObject method from Foo to a new class called StorageLibrary that more accurately represents the data structure architecture.

private class StorageLibrary 
{
    private List<obj> storedObjects = new List<obj>();
    public void addObject(obj inspectionObject)
    {
        storedObjects.Add(inspectionObject);
        //And a lot more code
    }
}

public class Foo : StorageLibrary
{
    //Declaration of libraries
    public static StorageLibrary storage1 = new StorageLibrary();
    public static StorageLibrary storage2 = new StorageLibrary();
    ...

    private void ObjectFeed(/* PARAMATERS */)
    {
        //generate objects

        if (objectType == Type.Type1)
        {
            storage1.addObject(inspectionObject);
        }
        if (objectType == Type.Type2)
        {
            storage2.addObject(inspectionObject);
        }
        ...
    }
}

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