Policy Mode + Singleton mode + simple Factory mode: Push service

Source: Internet
Author: User

First, the function:

Implement get data-based on user subscription checklist push here: QQ, Email, App, plugin, etc.

      Users can select multiple push modes.

Second, the realization

1. Push mode-enumeration (bitwise operation):

[Flags]      Public enum Pushtype    {        0,        2    }

2. Policy mode: Abstract push strategy

 Public Interface Ipush    {        bool Push (object  data);    }

3, QQ push send +email push

QQ Push: Singleton mode-Create a static QQ push service, thread safety.

  Public classQq:ipush {QQ () {}Private StaticQQ _instance =NewQQ ();  Public StaticQQ getinstance () {return_instance; }         Public BOOLPush (Objectdata) {Console.WriteLine ("QQ Push Service open, is pushing ..."); Console.WriteLine ("QQ push success."); return true; }    }

Email push:

 Public class Email:ipush    {        publicbool Push (object  data)        {            Console.WriteLine ("  mail push service on, pushing ... " );            Console.WriteLine (" Mail push succeeded. " );             return true ;        }    }

4, simple Factory mode to create a specific service type

public static class Pushservicefactory    {public        static Ipush creat (Pushtype type)        {            Ipush pushservice = null;            Switch (type)            {case                PushType.QQ:pushService = QQ. GetInstance (); break;                Case PushType.Email:pushService = new Email (); break;            }            return pushservice;        }    }

5. Theme: Push service

Get a subscription checklist, process data, push.

Of course, the push is based on the user's checked push mode (list<pushtype> types) to traverse the push.

 Public classPushprovider { Public ObjectGetusersubscribeddata (stringuserid) {Console.WriteLine ("get a subscription checklist"); return New Object(); }         Public ObjectHandledata (list<Object>data) {Console.WriteLine ("working with Data"); return New Object(); }             Public voidExcutepush (ObjectData, list<pushtype>types) {Console.WriteLine ("Push Data"); foreach(Pushtype typeinchtypes) {Ipush Pushservice=pushservicefactory.creat (type);            Pushservice.push (data); }        }    }

6. Call

 classProgram {Static voidMain (string[] args) {            //user Tick Subscription methodList<pushstategy.pushtype> pushtypes =NewList<pushstategy.pushtype>() {PushStategy.PushType.Email}; //assumption: The data that the system needs to pushlist<Object> resourcedata =Newlist<Object>(); Pushprovider Service=NewPushprovider (); //Get user list            ObjectUsersubscriptdata= service. Getusersubscribeddata ("Getdatafromuserid"); //filter out user data based on system data            Objectresult =service.            Handledata (Resourcedata); //push filtered data to usersservice.            Excutepush (result, pushtypes);        Console.readkey (); }    }

7. Conclusion

Through the singleton mode, we re-recognize the static, the instance and the multithreading, and comb the memory allocation by multithreading. The next chapter summarizes.

Policy Mode + Singleton mode + simple Factory mode: Push service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.