Chasing MM and design mode (Factory code)

xiaoxiao2021-03-06  15

Factory- chasing MM can not eat, please eat, McDonald's chicken wings and KFC's chicken wings are things that MM love to eat, although the taste is different, no matter you go to McDonald's or KFC, only to say "to the waiter" to four chicken wings " Ok. McDonald's and KFC is the production of Factory, which is a code I wrote, a code for factory // chicken wings, chickenwing.java, Public Abstract class chicken,} // McChumnwing.java) Public class mcchickenwing Extends ChickenWing {} // KFC type wings arm (KfcChickenWing.java) public class KfcChickenWing extends ChickenWing {} // McDonald factory public class McFactory {public ChickenWing getChickenWing () {System.out.println ( "This is a Mcdonalds ChickenWing!" Return new mcchickenwing ();}} // kfc factory public class kfcfactory {public chickenwing getchickenwing () {system.out.println ("this is a kfc chicken"; return new kfcchickenwing ();}} // test java file public abstract class test {public static void main (String [] args) {// new a Mc FactoryMcFactory objMcFactory = new McFactory (); // new a Kfc FactoryKfcFactory objKfcFactory = new KfcFactory (); // if you and PLMM in McDonalds, She Want to Eat Chickenwingsystem.out.println ("You and PLMM in McDonalds, She Want to Eat Chickenwing ...."); System.out.Print ("Chickenwing Have Be Sent to you:"); Objmcfactory.getchickenwing (); // if you and plm in kfc, s He Want to Eat Chickenwingsystem.Oat.Println ("You and PLMM in Kfc, She Want to Eat Chickenwing ...."); System.out.Print ("ChickenWing Have Be Sent to you:"); ObjkfcFactory.getchickenwing ( );}} Output: You and PLMM in McDonalds, She Want To Eat Chickenwing .... Chickenwing Have Be Sent to you: this is a mcdonalds chickenwing! You and PLMM in Kfc, She Want to Eat Chickenwing .... Chickenwing Have Be Sent to you: this is a kfc chicken wings! Factory model: Customer class and factory class separate. Consumers need some kind of product at any time, just request the factory. Consumers can accept new products without modification. The disadvantage is that when the product is modified, the factory class must make a corresponding revision.

转载请注明原文地址:https://www.9cbs.com/read-46371.html

New Post(0)