Rule mode + simple factory mode + Singleton mode: simple PUSH Service

Source: Internet
Author: User

I. functions:

Implement data acquisition-> push based on the user subscription list-> here, push Methods: QQ, email, app, plug-in, etc.

      You can select multiple push methods.

II. Implementation

1. Push method-enumeration (bitwise operation ):

 [Flags]    public enum PushType    {        QQ = 0,        Email = 2    }

2. Policy mode: Abstract push Policy

 public interface IPush    {        bool Push(object data);    }

3. QQ push + email push

Qq push: Singleton mode-static creation of qq push Service, thread security.

Public class QQ: ipush {QQ () {} Private Static QQ _ instance = new QQ (); public static QQ getinstance () {return _ instance ;} public bool push (Object Data) {console. writeline ("qq push Service enabled, pushing... "); console. writeline ("QQ push successful. "); Return true ;}}

Email push:

Public class email: ipush {public bool push (Object Data) {console. writeline ("directmail service enabled, pushing... "); console. writeline ("email pushed successfully. "); Return true ;}}

4. Create a specific service type in simple factory Mode

 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. Topic: Push Service

Obtain the subscription list, process data, and push.

Of course, the push here is based on the push method selected by the user (list <pushtype> types) for traversal push.

Public class pushprovider {public object getusersubscribeddata (string userid) {console. writeline ("Get Subscription List"); return new object ();} public object handledata (list <Object> data) {console. writeline ("processing data"); return new object ();} public void excutepush (Object Data, list <pushtype> types) {console. writeline ("Push data"); foreach (pushtype type in types) {ipush pushservice = pushservicefactory. creat (type); pushservice. push (data );}}}

6. Call

Static void main (string [] ARGs) {// select the subscription method list <pushstategy. pushtype> pushtypes = new list <pushstategy. pushtype> () {pushstategy. pushtype. QQ, pushstategy. pushtype. email}; // assume: list of data to be pushed by the System <Object> resourcedata = new list <Object> (); pushprovider service = new pushprovider (); // obtain the user list object usersubscriptdata = service. getusersubscribeddata ("getdatafromuserid"); // filter user data object result = service based on system data. handledata (resourcedata); // push the filtered data to the user service. excutepush (result, pushtypes); console. readkey ();}

7. Conclusion

Through the singleton mode, the static, instance, and multithreading are re-recognized, and the memory allocation is reorganized by multithreading. The following is a summary.

 

Rule mode + simple factory mode + Singleton mode: simple PUSH Service

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.