Go to: https://cainluo.github.io/14719369255829.html


The author testimonials

I am working on a new project these days. When I was using the notification center, I suddenly realized that I have been using the notification center for so long. Why not implement one by myself? No matter what we say, we will study the principle of notification center, including **KVO, which is based on the observer mode **, now that we know the principle, we will Do It!!

Finally: if you have better suggestions or dissatisfaction with this article, please contact me, I will refer to your opinions and then modify, contact me, please note factory mode, if you feel good, I hope you can also reward ~ hee hee ~ WISH you a happy learning ~ thank you ~


Introduction to the

Speaking of observation mode, in fact, we can take the experimental object as an example. The researcher is the ** observer, and the mouse is the observed. When the researcher conducts some experiments on the mouse, the mouse will produce some reactions, and the researcher will record and feedback the corresponding phenomena. This is called observer mode **.

Say so much nonsense, directly on the code demo ~~


New construction

Open the **workSpace project as before and create a new ** ObserverPattern project.

Here we take the wechat public number as an example. First of all, we should have a wechat public number center, which is convenient for us to find the public number we want.


Programming for interfaces

Here is a popular idea, is ** for interface programming, speaking of interface programming, it is estimated that there will be some dao friends will be confused, what is for interface programming? In fact, in our daily development, we have already done for interface programming, we just don’t realize itself, in fact, the so-called for interface programming, is we are writing a module or at the time of writing the SDK, we will first design the public API, and then implement the corresponding internal logic, which is aimed at interface programming * *.

Here is an article on interface programming that I think is good, and if you don’t feel good about it, you can poke fun at bloggers, which I won’t object to

So let’s just go ahead and declare some apis that we need to use.


Restrict user

Although we have declared some interfaces, it is not enough, because the public account does not know whether you have subscribed or not. If you do not subscribe, you cannot receive the messages sent by the public account. Therefore, we need to declare a **Protocol** to restrict users. Because not all users can receive messages sent by a subscription number, only users who subscribe to the subscription number can receive messages sent by the subscription number.

Remember to change the object to the one that complies with the change protocol to receive the message


Implement internal methods

Now that we have our interfaces and user-limiting protocols written, we need to implement the methods in **PublicNumberCenter** so that we can fully implement an observer pattern.

Since the notification center is a singleton, we must implement a singleton pattern first

Once the notification center is implemented, we need to implement the internal methods, so that our notification center is complete


Invoke the custom notification center

So now we’re going to try calling the notification center, and we’re going to do a simple implementation here, just make the ViewController the user, and obey the PublicNumberProtocol.


The project address

The address of the project: https://github.com/CainRun/DesignPattern