A few days ago, participated in the company's internal group of a technical exchange, mainly for "IOC and AOP", in the spirit of learning and Shizhi attitude and positive sharing, I combined a small story to the beginning of a shallow analysis of my eyes, "IOC past Life", in order to facilitate beginners can more intuitive to learn and understand ioc! Also for the purpose of the use.
(Although the story of the demand is a bit small, but viewers can in the mind as far as possible to enlarge him, imagine a large application system)
First, IOC Embryonic
1 , program V1.0
Say, many years ago UT asked for a request to provide a system in which a feature could send an e-mail to the company's employees on the occasion of the Lunar New Year holiday. In the mail to everyone to bless the Spring Festival, and inform the payment of a certain amount of holiday fees.
After analysis, it was decided that Zhang San, John Doe and Harry would be responsible for the development of this system.
Among them: Zhang San responsible for the development of logic control module Logiccontroller, which is simplified to UT.LogicController.exe, by the John Doe responsible for blessing message management Class (Greetmessageservice), and integrated into the component UT. MessageService.dll; Harry is responsible for the Mail function helper class (Emailhelper) and provides the component UT. Email.dll.
Class dependencies are as follows:
The core code of the Harry Mail function module is as follows:
public class emailhelper{public void Send (String message) { Console.Write ("frome Email:" + message);} }
The core code of the John Doe message Management module is as follows
public class greetmessageservice{ emailhelper greettool; Public Greetmessageservice () { Greettool = new Emailhelper (); } public void Greet (String message) { greettool.send (message);} }
The core code of the Zhang San Business Integration module is as follows:
String message = "Happy New Year!" Holiday fee 5000. "; Messageservice.greetmessageservice service = new Messageservice.greetmessageservice (); service. Greet (message);
Three people after one months of hard struggle, finally finished, the system also in the Spring Festival successfully sent greetings. Enterprises so care, to bring incomparable warmth to staff, so well received by all staff!
After the Spring Festival, the corresponding functions are also transplanted to "UT company" related to the "UT" and "UT real estate" similar applications, and in the subsequent "Yuanxiao", "Dragon Boat Festival", "Mid-Autumn Festival" and other festivals to be widely used.
Another approaching will be to ...
Seriously, the cost of holidays, sometimes may directly affect the entire holiday itinerary, thereby affecting the overall quality of the holiday, so the Department of Leadership attaches great importance. And the way of e-mail notification, in remote mountainous areas often because of the impact of the network environment can not be collected, many colleagues outside the Chinese New Year has been criticized. After the multi-textual research, we must use the current very mainstream telephone language broadcast way to inform.
As a Zhang San, John Doe and Harry were busy again. But John Doe is a bit of a headache, because his modules are now used not only in the UT company, but also in the UT Newsroom and UT properties. How to make the change here is minimal, it will cost a bit of brains. In order to achieve a better effect, John Doe decided to follow the following way to rectify.
① , the initial design scheme is as follows:
First, in order to make different "blessing ways" can be effectively replaced, decided to "interface-oriented" approach to the separation.
At the same time, let Emailhelper's email notification class and Telephonehelper's voice broadcast class implement this interface. The core code is as follows:
public interface isendable{ void Send (String message); public class emailhelper:isendable{public void Send (String message) { Console.Write ("frome email:" + mess age);} } public class telephonehelper:isendable{public void Send (String message) { Console.Write ("Frome Telephone: "+ message);} }
Furthermore, in order to facilitate the compatibility of new and old products, the controller is required to determine the current mode of communication, and parameters to the message Management module, the core code is as follows:
public enum sendtooltype{ Email, Telephone,}
"Remark": The above code is not an excellent design and will be removed in the subsequent optimization scheme.
public class greetmessageservice{ isendable greettool; Public Greetmessageservice (Sendtooltype sendtooltype) { if (Sendtooltype = = Sendtooltype.email) { Greettool = new UT. Emailv20.emailhelper (); } else if (Sendtooltype = = Sendtooltype.telephone) { Greettool = new UT. Telephonev20.telephonehelper (); } } public void Greet (String message) { greettool.send (message);} }
"Remark": The above code is not a good design and will be optimized in the subsequent optimization scheme.
Finally, the business integration module is adapted to the specific business requirements, with the following core code:
String message = "Happy New Year!" Holiday fee 5000. "; Greetmessageservice service = new Greetmessageservice (sendtool.telephone); service. Greet (message);
Note: Reproduced in: http://www.cnblogs.com/showjan/p/3950989.html There are more detailed
Spring Learning Summary (2)--spring IOC's past life