This is the 21st day of my participation in the August Text Challenge.More challenges in August
- 📢 welcome to like: 👍 collect ⭐ message 📝 if there are mistakes please correct, give people rose, hand left lingering fragrance!
- 📢 This article was originally written by Webmote and originally published by Nuggets.
- 📢 author’s motto: life is toss about, when you don’t toss about life, life will start to toss about you, let us come on together! 💪 💪 💪
Preface 🎏
Observer mode, also known as Publish/Subscribe mode, since there are observers, there are also observed, just like in your life, there is a pair of bright eyes always watching you, big and small things happen to you, it knows everything.
🎏 01. Interpretation of the observer model
Intent: Define a one-to-many dependency between objects so that when an object’s state changes, all dependent objects are notified and automatically updated.
Problem area: It is generally used to solve the following problems.
-
Observer and observed need to be decoupled, or have multiple observers
-
You need to notify other objects of your status
Solution: We use UML diagrams to describe it.
As can be seen from the figure, the observed is Subject, which can have many observers (observers). The observed issues notices and the observers receive the notices.
Effect:
- Benefits:
- When an object changes state, all observers can be notified;
- The observer and the observed are coupled only at the abstraction level;
- There’s a trigger mechanism
- Limit:
- Notify all observers, it will take a lot of time when there are many observers
- There are cyclic dependencies between the observer and the observed, which can cause the system to crash
🎏 02. Dotnet Core source code appreciation
internal interface ICascadingValueComponent
{
// This interface exists only so that CascadingParameterState has a way
// to work with all CascadingValue<T> types regardless of T.
bool CanSupplyValue(Type valueType, string? valueName);
object? CurrentValue { get; }
bool CurrentValueIsFixed { get; }
void Subscribe(ComponentState subscriber);
void Unsubscribe(ComponentState subscriber);
}
/ / implementation class
voidICascadingValueComponent.Unsubscribe(ComponentState subscriber) { _subscribers? .Remove(subscriber); }private void NotifySubscribers(in ParameterViewLifetime lifetime)
{
foreach (var subscriber in_subscribers!) { subscriber.NotifyCascadingValueChanged(lifetime); }}Copy the code
Publish subscriptions are fairly common, and you can see many similar uses in asp.net core code.
🎏 03. Dotnet generator implementation
Build an abstract observer and implement several instances.
Instance observer implementation that can be added to the observed watch list.
public abstract class Visitor
{
public abstract void VisitConcreteElementA(
ConcreteElementA concreteElementA);
public abstract void VisitConcreteElementB(
ConcreteElementB concreteElementB);
}
/// <summary>
/// A 'ConcreteVisitor' class
/// </summary>
public class ConcreteVisitor1 : Visitor
{
public override void VisitConcreteElementA(
ConcreteElementA concreteElementA)
{
Console.WriteLine("{0} visited by {1}",
concreteElementA.GetType().Name, this.GetType().Name);
}
public override void VisitConcreteElementB(
ConcreteElementB concreteElementB)
{
Console.WriteLine("{0} visited by {1}",
concreteElementB.GetType().Name, this.GetType().Name); }}Copy the code
Callers can be directly used by instance type calls.
ObjectStructure o = new ObjectStructure();
o.Attach(new ConcreteElementA());
o.Attach(new ConcreteElementB());
// Create visitor objects
ConcreteVisitor1 v1 = new ConcreteVisitor1();
ConcreteVisitor2 v2 = new ConcreteVisitor2();
// Structure accepting visitors
o.Accept(v1);
o.Accept(v2);
// Wait for user
Console.ReadKey();
Copy the code
🎏 04. Summary
30 days without stop, the goal is very ambitious, today is the second day, come on, brothers!
Routine summary, rational view!
Knot is what ah, knot is I want you to praise but can not get lonely. 😳 😳 😳
👓 have seen this, but also care about a thumbs-up?
👓 has been liked, but also care about a collection?
👓 are collected, but also care about a comment?