What is this programming method called? And is it bad?
Asked Answered
S

1

6

Lately in my Unity projects, I have discovered that to create a more modular application it helps to have a static List in a class that contains references to all or some of the objects created so that they may be accessed easily from other parts of the program. An example is below:

private static List<Canvas> availableCanvases = new List<Canvas>();

void Start () {
    availableCanvases.Add(this);
}

public static void AddComponentToCanvas(Transform component) {
    for (int i = 0; i < availableCanvases; i++) {
        //Make sure the canvas still exists
        if (availableCanvases[i] != null) {
            component.SetParent(availableCanvases[i]);
            return;
        } else {
            availableCanvases.RemoveAt(i);
            i--;
        }
    }

    //Reached if no canvas found
    //Create new canvas or create error etc...
}

This simply allows an object that is instantiated at runtime to add itself to an available canvas without needing to access it through a findWithTag or findWithType method which will hurt performance if used too much.

Is this bad practice or good? My colleague reckons this represents singleton programming but it of course doesn't because it allows multiple objects to exist and be used.

Seve answered 17/6, 2015 at 3:5 Comment(4)
I don't understand your condition, why would availableCanvases ever be null?Relative
Updated to correct condition if (availableCanvases[i] != null)Seve
Please don't use the unity tag for questions about the game engine. Use unity3d.Commingle
"but it of course doesn't because it allows multiple objects to exist and be used." - well no, I think you'll find what your "colleague" was trying to say is that availableCanvases is essentially acting as a singleton within the scope of the containing class, not the items that are contained in availableCanvases.Whelp
F
8

This is essentially a Service Locator "Anti Pattern" that is (surprise surprise) based around the Singleton Pattern. However, instead of retrieving service instances, you are retrieving object instances.

Regardless of the name, this actually does NOT create a more modular application. On the contrary, you are creating hard dependencies throughout your application that makes it harder to split it apart into the respective components.

Martin Fowler documented the pattern quite well. You can find more information here.

Fagot answered 17/6, 2015 at 3:8 Comment(3)
Was going to take a stab at this, but your answer is bang on the money.Sherrie
Any idea how Unity's GameObject.FindWithTag("Tag") function would work then? Or would it also use the service locator since it needs static references?Seve
@LuminaryPromotions Unfortunately Unity3d does not document the internal workings of the GameObject.FindWith... methods very well. They only detail how to call them. In addition, those methods end up invoking CLR level functions and there is even less information on those. As a result, your guess is as good as mine as to how those methods actually work under the hood.Fagot

© 2022 - 2024 — McMap. All rights reserved.