Java design pattern-Template Method)

Source: Internet
Author: User

 Package COM. factory. commonality; <br/>/** <br/> * an example of going to a bank loan. The bank must check your background information before making a loan: bank account, credit card, existing loan, stock ...... <br/> * because each bank checks the information, the template method mode can be used here. <Br/> */<br/> public class templatemethod {<br/> Public static void main (string [] ARGs) {<br/> chinaconstructionbankloanapp Client1 = new chinaconstructionbankloanapp (<br/> "Judy"); <br/> system. out. println ("/ncheck client" + client1.getname (); <br/> client1.check (); <br/> everbrightbankloanapp Client2 = new everbrightbankloanapp ("mark "); <br/> system. out. println ("/ncheck client" + client2.getname (); <Br/> client2.check (); <br/>}< br/> abstract class checkbackground {</P> <p> // The following five methods are template methods, it must be implemented by sub-classes. <Br/> public abstract void checkbank (); <br/> public abstract void checkcredit (); <br/> public abstract void checkloan (); <br/> public abstract void checkstock (); <br/> public abstract void checkincome (); <br/> // call the preceding five methods <br/> Public void check () {<br/> checkbank (); <br/> checkcredit (); <br/> checkloan (); <br/> checkstock (); <br/> checkincome (); <br/>}< br/> // China Construction Bank. <Br/> class chinaconstructionbankloanapp extends checkbackground {<br/> private string name; <br/> Public chinaconstructionbankloanapp (string name) {<br/> This. name = Name; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> // check the background information of CCB. <Br/> Public void checkbank () {<br/> system. out. println ("Check bank... "); <br/>}< br/> Public void checkcredit () {<br/> system. out. println ("check credit... "); <br/>}< br/> Public void checkloan () {<br/> system. out. println ("check other loan... "); <br/>}< br/> Public void checkstock () {<br/> system. out. println ("check stock values... "); <br/>}< br/> Public void checkincome () {<br/> system. out. println ("Che CK family income... "); <br/>}< br/> // Everbright. <Br/> class everbrightbankloanapp extends checkbackground {<br/> private string name; <br/> Public everbrightbankloanapp (string name) {<br/> This. name = Name; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> // checks the background information. <Br/> Public void checkbank () {<br/> system. out. println ("Check bank... "); <br/>}< br/> Public void checkcredit () {<br/> system. out. println ("check credit... "); <br/>}< br/> Public void checkloan () {<br/> system. out. println ("check other loan... "); <br/>}< br/> Public void checkstock () {<br/> system. out. println ("check stock values... "); <br/>}< br/> Public void checkincome () {<br/> system. out. println ("check family income... "); <br/>}< br/>

 

Running result:

Check client Judy
Check bank...
Check credit...
Check other loan...
Check stock values...
Check family income...

Check client Mark
Check bank...
Check credit...
Check other loan...
Check stock values...
Check family income...

 

If you don't talk about it much, the program will read it several times ~~~ Pai_^

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.