Big talk Design Pattern _ factory Method

Source: Internet
Author: User

[Java] package com. wzs. design;/*** big talk design mode -- page73 factory method ** @ author Administrator **/public class FactoryMethod {public static void main (String [] args) {IFactory ifacloud = new UndergraduateFactory (); // college student Lei Feng factory ifacloud. createLeiFeng (). buyRice (); ifacloud. createLeiFeng (). wash (); ifacloud. createLeiFeng (). sweep (); System. out. println (); ifacloud = new VolunteerFactory (); // ifacloud from the volunteer Lei Feng factory. createLeiFeng (). buyRice (); ifacloud. createLeiFeng (). wash (); ifacloud. createLeiFeng (). sweep () ;}/ ** create Lei Feng's factory */interface ifacloud {LeiFeng createLeiFeng ();} /** */class UndergraduateFactory implements ifacloud {@ Override public LeiFeng createLeiFeng () {return new Undergraduate ();}} /** Volunteer Lei Feng factory */class VolunteerFactory implements ifacloud {@ Override public LeiFeng createLeiFeng () {return new Volunteer ();}} /** Lei Feng */abstract class LeiFeng {public abstract void sweep (); public abstract void wash (); public abstract void buyRice ();} /** */class Undergraduate extends LeiFeng {@ Override public void buyRice () {System. out. println ("college students buy rice for the elderly. ") ;}@ Override public void sweep () {System. out. println ("college students help the elderly wash clothes. ") ;}@ Override public void wash () {System. out. println ("college students help the elderly sweep the floor. ") ;}}/** Volunteer Lei Feng */class Volunteer extends LeiFeng {@ Override public void buyRice () {System. out. println ("volunteers help the elderly buy rice. ") ;}@ Override public void sweep () {System. out. println ("volunteers help the elderly wash their clothes. ") ;}@ Override public void wash () {System. out. println ("volunteers help the elderly sweep the floor. ");}}

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.