Observer Pattern (749344)

Hello. I faced such a problem that in my 1 class I need to implement 2 actions for 2 different events, how can this be resolved?

Observer:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Observer : IObserver
{
    public IObservable obs1;
    public IObservable obs2;

    public Observer(IObservable first, IObservable second)
    {
        obs1 = first;
        obs2 = second;
    }

    public void Init()
    {
        obs1.AddObserver(this);
        obs2.AddObserver(this);
    }
   
    public void Invoke()
    {
        //doing for obs1
    }
   
    public void Invoke3()
    {
        //doing for obs2
    }
}

Event1:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Observable : IObservable
{
    private HashSet<IObserver> observers = new HashSet<IObserver>();
   
    public void AddObserver(IObserver o)
    {
        observers.Add(o);
    }

    public void RemoveObserver(IObserver o)
    {
        observers.Remove(o);
    }
   
    public void NotifyObservers()
    {
        var e = observers.GetEnumerator();

        while(e.MoveNext())
        {
            e.Current.Invoke();
        }
    }
}

Event2:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Observable2 : IObservable
{
    private HashSet<IObserver> observers = new HashSet<IObserver>();
   
    public void AddObserver(IObserver o)
    {
        observers.Add(o);
    }

    public void RemoveObserver(IObserver o)
    {
        observers.Remove(o);
    }
   
    public void NotifyObservers()
    {
        var e = observers.GetEnumerator();

        while(e.MoveNext())
        {
            e.Current.Invoke();
        }
    }
}

You overthinking things. Use unity events or native .net events that’s all. Observer pattern is more complex replacement for an event. You probably don’t need it if you just want execute actions when something happens.

Be extremely careful here; those don’t look like Unity objects, which means you need to watch out for the limitations of Unity’s serializer (see “When might the serializer behave unexpectedly”): Unity - Manual: Script serialization
That doesn’t address your question, though. Once you are sure you still want to do this after reading the docs on serialization, you have a few options. If you don’t want to change you structure too much, you can have the observable pass itself in as an argument to Invoke. Then, the observer can check which observable is calling invoke, and then call the relevant other method.

// In Observable1 and 2
public void NotifyObservers()
    {
        var e = observers.GetEnumerator();

        while(e.MoveNext())
        {
            e.Current.Invoke(this);
        }
    }

// In Observer
public void Invoke(IObservable ob)
    {
         if(ob == obs1)
         {
               // Do something, call Invoke1?
         }
         if(ob == obs2)
         {
               // Do something else, call Invoke2?
         }
        //doing for obs1
    }