Python微信订餐小程序课程视频
https://blog.csdn.net/m0_56069948/article/details/122285951
Python实战量化交易理财系统
https://blog.csdn.net/m0_56069948/article/details/122285941
抽象编程怎么说呢,以观察者模式为例:
观察者模式有两个对象,一个是观察者,一个是可观察者(字面翻译很别扭observable),消息发布者(提供者)。
第一层如下,三个对象A、B、C分别有一个接收消息的方法,还有一个存储数据的字段,X就是发布消息的对象,它通过setdata方法设置自己的字段data,然后通知abc,abc如愿以偿地拿到了通知,完美!
internal class A
{
public int Data;
public void Update(int data)
{
this.Data = data;
}
}
internal class B
{
public int Count;
public void Notify(int data)
{
this.Count = data;
}
}
internal class C
{
public int N;
public void Set(int data)
{
this.N = data;
}
}
internal class X
{
private int data;
public A instanceA;
public B instanceB;
public C instanceC;
public void SetData(int data)
{
this.data = data;
instanceA.Update(data);
instanceB.Notify(data);
instanceC.Set(data);
}
}
using ObserverOne;
A a = new A();
B b = new B();
C c = new C();
Console.WriteLine("订阅前.................");
Console.WriteLine($"a.Data = {a.Data}");
Console.WriteLine($"b.Count = {b.Count}");
Console.WriteLine($"c.N = {c.N}");
X x =new X();
x.instanceA = a;
x.instanceB = b;
x.instanceC = c;
x.SetData(10);
Console.WriteLine("X发布data=10, 订阅后.................");
Console.WriteLine($"a.Data = {a.Data}");
Console.WriteLine($"b.Count = {b.Count}");
Console.WriteLine($"c.N = {c.N}");
再想一想,这好像不够灵活,订阅者是死的,那改进一下:
internal interface IUpdatebleObject
{
int Data { get; }
void Update(int newData);
}
internal class A : IUpdatebleObject
{
public int Data => data;
private int data;
public void Update(int newData)
{
this.data = newData;
}
}
internal class B : IUpdatebleObject
{
public int Data => data;
private int data;