I think the term "interface" is used in different meanings. You have the general term “interface for something” to access it, and you have the understandable term “C # interface definition” as part of this language. So what I think in your quote
to protect the client from a very complex system by creating an “interface”
the first common value is used. And the question is how to build it:
1. So, my first question is: are you implementing the “interface” correctly as a class?
I would use both. First, I would create a C # interface to define a contract, and then build a base class as a reference implementation. If you use only the base class, then all other possible classes should inherit from this, and they receive implementation details only because they want a contract. If other possible classes can use your interface, they only need to implement it, that is, they must provide methods in the definition of the interface and have no connection with each other.
2. My other question then, could you use the facade template along with the observer template. The facade class will be observed at all and then control which methods in different classes should be dependent on the object?
Yes, you can, but then observational behavior with all its classes is a “part” of the facade. If this is what you want, good.
3. Why does it make me think that there really is no point in doing this with the facade, since the whole point of the facade is to make it easier for the client? (or hide information), or am I wrong?
I think it makes sense to have a facade for the purpose for which it is defined, in order to link a complex system behind it. Only if there is nothing but observational behavior, then there is no difficulty to hide.
So, I can offer a small redesign as follows:
I would execute an ISubject with the event, because it does not need to know who is watching, it will just notify:
public interface ISubject { event EventHandler OnNotify; }
and then create a second interface for salary access:
public interface ISalerySubject: ISubject { int Salery { get; set; } }
IObserver may contain an ISubject :
public interface IObserver { ISubject Subject { get; set; } }
Now let's get specific. The SalerySubject class implements the ISalerySubject interface, so when the salon changes, the event is fired:
public class SalerySubject : ISalerySubject { public event EventHandler OnNotify; private int salery; public int Salery { get { return salery; } set { salery = value; if (OnNotify != null) OnNotify(this, new EventArgs()); } } }
The TeacherObserver class implements the IObserver interface and binds its Update method to the ISubject event:
public class TeacherObserver : IObserver { private int _currentSalery = 500; public string Mood { get; private set; } public ISubject subject; public ISubject Subject { get { return subject; } set {
Now you can use it:
class Program { static void Main(string[] args) { ISalerySubject salery = new SalerySubject(); TeacherObserver teacher = new TeacherObserver(); teacher.Subject = salery; Console.WriteLine("Teacher is " + teacher.Mood); salery.Salery = 100 ; Console.WriteLine("Teacher salery just went down. The teacher is now " + teacher.Mood); } }
So, up to this point there is no need for a facade. You might want to keep a list of watchers, but this is usually not stored on the facade. Perhaps your system is much more complex, so there is a good reason for the façade anyway.