Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the java factory mode

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write java factory mode". In daily operation, I believe many people have doubts about how to write java factory mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to write java factory mode". Next, please follow the editor to study!

Everyone has heard of the story of Nu Kui mending the sky. If we don't talk about it today, it is not the work of "making people" to say that Nu WA created human beings. This word has been abused by modern people. The story is that after Nu WA mended the sky, she went down to the earthly world to have a look, wow, the scenery is too beautiful, the sky is blue, the water is clear, the air is fresh and beautiful, and then she becomes a little lonely after a long time. Without animals, all these things are static. What should we do?

Don't forget that it is an immortal, and there is nothing impossible, so Nu WA set up a gossip stove (technical term: building a factory) and began to build people. the specific process is as follows: first kneaded in mud, then baked in the gossip furnace, and then thrown to the ground to grow, but accidents always happen:

The first baked clay figurines, Zizizz ~ ~, felt that they should be ripe. Throwing them to the ground, biu~, a white man was born, not cooked!

The second clay figurine was not cooked last time. This time, I roasted it a little longer and threw it on the ground. Hey, it's overcooked, black man!

The third clay figurine, Zi, roasted while watching, Hey, just in time, Perfect! Excellent product, yellow race! [remarks: RB does not belong to this category]

This process is quite interesting, first take a look at the class diagram: (previously, brothers suggested adding class diagrams and source files on the forum, and later models will be added, and the previous ones will be added one by one, in order to make everyone look comfortable and happy. I want it, just like watching pornographic novels, goals, goals, whether they can be achieved depends on the confidence you give me.)

Then we will use the program to express this process, first of all, define a general name of human beings:

Package com.cbf4life;/*** defines a general term for human beings * / public interface Human {/ / first define what is human / / people are happy and can laugh, originally intended to use smile to express, think about laugh is more appropriate, for a long time did not laugh Public void laugh (); / / humans still cry, representing pain public void cry (); / / humans can speak public void talk ();} then define a specific race: package com.cbf4life;/*** yellow race, this translation is not accurate, we will click * / public class YellowHuman implements Human {public void cry () {System.out.println ("yellow race can cry") } public void laugh () {System.out.println ("Yellow people will laugh, happy!") ;} public void talk () {System.out.println ("Yellow people can talk, usually double bytes");}}

White race:

Package com.cbf4life;/*** white race * / public class WhiteHuman implements Human {public void cry () {System.out.println ("white people cry");} public void laugh () {System.out.println ("white people laugh, laughter of aggression");} public void talk () {System.out.println ("white people can talk, usually but single byte!") ;}}

By the same token, black people, like here, are not defined.

Now that the race is defined, let's define the eight diagrams stove:

Package com.cbf4life;import java.util.List;import java.util.Random;/*** today tells the story of the creation of a human being by a woman. The outline of the story goes like this: * A long time ago, Pangu opened up the world and used his body to create the sun, moon and stars, mountains and trees, and the world was prosperous * One day, Nu WA walked down the boundary, hey! Too lonely too lonely there is nothing that can laugh cry or talk * what should I do? Don't worry, Nuwa, immortal, make it, then pinch the mud and bake it in the gossip stove (later this became the treasure of Taibai Venus), so there are people: * We show the process of producing people with Java program: * / public class HumanFactory {/ / set an oven, mud is stuffed in, and people come out. This is too advanced public static Human createHuman (Class c) {Human human=null. / / define a type of human try {human = (Human) Class.forName (c.getName ()). NewInstance (); / / generate a race} catch (InstantiationException e) {/ / if you don't talk about personal color, you can't bake it, you can't bake it white or black, you can't bake System.out.println until you speak ("ethnic color must be specified") } catch (IllegalAccessException e) {/ / there is a problem with the definition of race, so it can't be baked, this is. System.out.println ("ethnic definition is wrong!") ;} catch (ClassNotFoundException e) {/ / you say whatever you want, where can I make it for you?! System.out.println ("asshole, the race you specified can't be found!") ;} return human;}}

Then we will declare Nu WA.

Package com.cbf4life;/*** first defines Nuwa. This is really a god * / public class NvWa {public static void main (String [] args) {/ / Nuwa's first creation, experimental nature, less production, lack of heat, defective product System.out.println ("- the first batch of people are like this: White people -") Human whiteHuman = HumanFactory.createHuman (WhiteHuman.class); whiteHuman.cry (); whiteHuman.laugh (); whiteHuman.talk () / / Nu WA made a baby for the second time, and then produced another defective product, black System.out.println ("\ n\ n-the second batch of people are like this: black -"); Human blackHuman = HumanFactory.createHuman (BlackHuman.class); blackHuman.cry (); blackHuman.laugh () BlackHuman.talk (); / / the third group of people, this time the fire is just right, the yellow people (do not write about the yellow people, so as not to cause ambiguity), note: RB people do not belong to this list of System.out.println ("\ n\ n-the third batch of people created is like this: yellow people -") Human yellowHuman = HumanFactory.createHuman (YellowHuman.class); yellowHuman.cry (); yellowHuman.laugh (); yellowHuman.talk ()}

In this way, the world will be lively, and there will be people, but it is too tired to create like this, and the immortals will be tired, so what should we do? The immortals thought: I stuffed in a mass of mud and randomly came out a group of people, regardless of whether they were black, white or yellow, as long as they were human (you see, the immortals are lazy, not to mention us), first modify the class diagram:

Then let's take a look at our program modification. We first modify HumanFactory.java and add the createHuman () method:

Package com.cbf4life;import java.util.List;import java.util.Random; public class HumanFactory {/ / set an oven, mud is stuffed in and people come out. This is too advanced. Public static Human createHuman (Class c) {Human human=null; / / defines a type of human try {human= (Human) Class.forName (c.getName ()). NewInstance () / / generate a race} catch (InstantiationException e) {/ / if you don't say personal color, you can't bake it, you have to be white and black, you can't bake System.out.println until you speak ("you must specify the color of the race");} catch (IllegalAccessException e) {/ / there is a problem with the definition of race, then you can't bake it. System.out.println ("ethnic definition is wrong!") ;} catch (ClassNotFoundException e) {/ / you say whatever you want, where can I make it for you?! System.out.println ("asshole, the race you specified can't be found!") ;} return human;} / / Nu WA got angry and stuffed a lump of mud into the gossip stove, ah, public static Human createHuman () {Human human=null; / / define a type of human / / first of all, how many implementation classes and how many individual species List concreteHumanList = ClassUtils.getAllClassByInterface (Human.class). / / defines how many races / / eight diagrams stoves start to burn whoever they want Random random = new Random (); int rand = random.nextInt (concreteHumanList.size ()); human = createHuman (concreteHumanList.get (rand)); return human;}}

Then look at how Nu WA does it:

Public class NvWa {public static void main (String [] args) {/ / Nu WA created for the first time, experimental nature, less production, insufficient heat, defective product System.out.println ("- the first batch of people were created like this: White -"); Human whiteHuman = HumanFactory.createHuman (WhiteHuman.class) WhiteHuman.cry (); whiteHuman.laugh (); whiteHuman.talk (); / / Nu WA made a second human being, the hot spot, and then produced a defective product, black System.out.println ("the second batch of people created is like this: black -") Human blackHuman = HumanFactory.createHuman (BlackHuman.class); blackHuman.cry (); blackHuman.laugh (); blackHuman.talk () / / the third group of people, this time the fire is just right, the yellow people (do not write the yellow people, so as not to cause ambiguity), remarks: RB people do not belong to this list of System.out.println ("\ n\ n-the third batch of people created is like this: yellow people -"); Human yellowHuman = HumanFactory.createHuman (YellowHuman.class) YellowHuman.cry (); yellowHuman.laugh (); yellowHuman.talk (); / / Nu WA is irritated. It's what race you love. Burn for.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report