Trying to get a handle on what the singleton pattern is, and what it's for. I think I'm close, but I'm not quite sure. I understand we use this technique when we only want one instance of a certain thing. But I haven't gotten my head around what that means and what the code part actually does.
FOR ANALOGY ONLY: Let's say I own a store with only one cash register. Employees and customers are scripts. I want every employee and customer in the building to use that one register for checkout related tasks. (I think that's what singleton is for)
public class cashRegister
{
public static cashRegister TheeOneAndOnlyCashRegister;
void Start()
{
}
void Update()
{
}
void Awake()
{
if (TheeOneAndOnlyCashRegister == null)
TheeOneAndOnlyCashRegister = this;
}
}
So what I don't understand is what I actually did here (if I even did this correctly).
Did I tell the store that there must be 'this' register in existence? Or did I tell the store that all people can only ever use this register? Or did I merely just name the register a name and demand the name was TheeOneAndOnlyCashRegister?
Aucun commentaire:
Enregistrer un commentaire