Figuratively, we have to reach the point of take-away service reminder, remind customers of their choice in the way. Can be a phone alerts, SMS alerts, you can knock on the door to remind here, "remind" This behavior is a US corporation or hungry it provides such a platform, the equivalent of library functions, but a reminder of the way is determined by the customer and tell Platform , that is, the callback function. The customer told how the platform to remind ourselves of action, that is, the callback function storage transfer operation, known as registered callback function (to register a callback function)
Obviously callback is a design pattern, and the same is by proxy (callbacks can also abstract class) interface inheritance to achieve
1, the callback interface
Package Penalty for Interface; / ** * This interface is a reminder takeaway service interface provides three ways to remind * / public interface TakeOutServiceInterface { / ** * reminder: Tel * / public void TelephoneReminderService (the PhoneNumber String, String Content); / * * * remind ways: SMS * / public void MessageReminderService (the PhoneNumber String, String Content); / ** * prompt mode: knock * / public void KnockDoorReminderService (String Content); }
2, the client
package Service; import Interface.TakeOutServiceInterface; public class Guest implements TakeOutServiceInterface { private String _phoneNumber; private String _content; private String _type; public Guest(String PhoneNumber,String Content,String type){ this._phoneNumber=PhoneNumber; this._content=Content; this._type=type; } //下单 public void PlaceOrder(){ System.out.println("Online orders:" + _content + "reminded way:" _ of the type); new new ShopOwner () doOrder (_phoneNumber, _content, _type,. The this ); } // phone reminding method @Override public void TelephoneReminderService (String the PhoneNumber, String Content) { System.out.println (the PhoneNumber + ":" + the Content); } // SMS alert method @Override public void MessageReminderService (the PhoneNumber String, String the Content) { } // knocking alert method @Override public void KnockDoorReminderService (String the Content ) { } }
3, the server platform
Package Penalty for Service; Import Interface.TakeOutServiceInterface; public class ShopOwner { public void doOrder (the PhoneNumber String, String Content, String Type, TakeOutServiceInterface hotelService) { System.out.println ( "stores to process orders, waiting ..." ); the try { . Thread.currentThread () SLEEP ( 3000 ); } the catch (InterruptedException E) { e.printStackTrace (); } System.out.println ( "reaches takeaway prompting the customer." ); IF (Type.equals ( ""! )) { Switch(Type) { Case "Telephone" : hotelService.TelephoneReminderService (the PhoneNumber, "Hello, hello, your takeaway has been served, please take it down." ); BREAK ; Case "message" : hotelService.MessageReminderService (the PhoneNumber, "You the takeaway has been served, please take it down. " ); BREAK ; Case " knock " : hotelService.KnockDoorReminderService ( " hello, your takeaway to " ); BREAK ; } } } }
4, the test
package Test; import Service.Guest; public class TestCallBack { public static void main(String[] args) { String PhoneNumber="Not_Copy"; String Content="老板来一份黄焖鸡米饭。"; String Type="电话"; Guest guest = new Guest(PhoneNumber,Content,Type); guest.PlaceOrder(); } }
5 results
Online orders: the boss of a yellow chicken stew with rice. Reminder: Phone
stores to process orders, waiting. . .
Takeaway arrives, remind customers.
Not_Copy: Hello, hello, your takeaway has been served, please take it down.
6, summed up
In the test object is just to call the guest orders PlaceOrder () this method, the server will remind TelephoneReminderService method according to Method in interface library callback object to a guest phone, provided that this method is registered in the callback interface for the job .
Note: This callback interface can also be used instead of an abstract class.