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