Template mode of Java mode--abstract class

Source: Internet
Author: User

Template Design Patterns (templates)

Abstract classaction{//define a behavior class     Public Static FinalString work = "Work";  Public Static FinalString sleep = "Sleep";  Public Static FinalString EAT = "EAT";  Public Abstract voideat ();  Public Abstract voidsleep ();  Public Abstract voidWork ();  Public voidcommand (String str) {Switch(str) { Case"Work":                 This. Work ();  Break;  Case"Sleep":                 This. Sleep ();  Break;  Case"Eat":                 This. Eat ();  Break; }    }}classPeopleextendsaction{ Public voidsleep () {System.out.println ("People are sleeping."); }     Public voideat () {System.out.println ("People are eating."); }     Public voidWork () {System.out.println ("People are working"); }}classPigextendsaction{ Public voidsleep () {System.out.println ("The Pig is sleeping."); }     Public voideat () {System.out.println ("Pigs are eating."); }     Public voidWork () {}}classRobotextendsaction{ Public voidsleep () {} Public voideat () {System.out.println ("The robot is replenishing energy."); }     Public voidWork () {System.out.println ("The robot is working"); }} Public classtest{ Public Static voidMain (String args[]) {Fun (Newpeople ()); Fun (NewRobot ()); }     Public Static voidFun (Action Act) {Act.command (ACT).        work); Act.command (Act.        SLEEP); Act.command (Act.    EAT); }}

Template mode of Java mode--abstract class

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.