lpxxn / rust-design-pattern

rust design patterns
Mozilla Public License 2.0
1.35k stars 110 forks source link

observer pattern restricted to ConcreteObserver struct instead of IObserver trait #9

Open electricherd opened 4 years ago

electricherd commented 4 years ago

Hi, I like your patterns very much btw., I needed the Observer Pattern (which doesn't consume like yours) and tried a little but needed one which can have not only have a ConcreteObserver but also AnotherConcreteObserver, which I think fits more to the purpose. Since I didn't need detaching, I didn't have the identifying problem when detaching, and came up with a version which uses box and dyn, and no partialeq. I am intermediate in Rust, I'd say, so I explain best with that.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=5115d33f36e98f73611e1b979988d0f4

I even tried a little with detach, which I believe you have to kind of first find out if the dyn type matches (don't know how) and then use the partialeq trait on the concrete type like you did. Because I also had trouble with the trait itself, dyn, box, box dyn, &? in trait alone, I stopped here (I don't need detach), and I believe it could be interesting to you.

electricherd commented 4 years ago

actually it is restricted to the first registered observer's struct type.