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