Copying homework is the fastest way to success , These people are good at using the resources around them to complete their tasks .
They are good at standing on the shoulders of giants and picking fruit , What you pick must be the reddest and biggest fruit .
Of course, there are various types of copying homework , There are some things you can do but don't , There are some things you can learn without learning , Some can't learn at all .
In fact, few people can't learn at all , So basically, the people who copy their homework are lazy . But laziness is an indispensable feature to promote the progress of knowledge .
He can't be said to be good or bad , He's just a way .
We won't talk about philosophy today , We only talk about technology .
We use a design pattern to simulate the copying operation —— Prototype mode .
<> one , Initial prototype mode
In the last article, we talked about the meta model , The prototype model mentioned in this article can be compared with the meta model , Shared meta mode is to create objects for everyone to use together , The prototype pattern is for everyone to find the same object .
For example, you have an object , Her tenderness , Intellectual , generous , Filial piety , Then your friend came , Say I use your girlfriend , You must have a big mouth . But your friend said , Does your girlfriend have a sister , younger sister , elder female cousin , Cousin or something , Introduce me to someone who is as good as her character , You must be complacent at this time , And try to find someone for your friend .
This is the difference between Heyuan moss and the prototype model , Enjoy the yuan is public , Prototype is replication ( Don't be a gangster here , I'm talking about finding objects, not copying in the real sense , Unless you clone people with target cells , This means based on the original object , Find the same object , Used to simulate replication )
Prototype mode (Prototype Pattern) Create duplicate objects using , At the same time, it can ensure the performance . It belongs to creation mode .
Generally, if the process of creating objects consumes more resources or time , We will use this design pattern .
I read a lot on the Internet ,Java If it is implemented, it needs to inherit Cloneable Interface , I personally understand , Yes, but not necessary .
<> two , Graphic prototype model
Here are two examples of objects :
Small chess : good , tender , versatile
Little purple : generous , Intellectual , Filial piety
Yes, of course , It takes time to find two such objects , So if we want to find it again , We can say , I'm looking for someone like little Qi , This is much more convenient .
be careful : When looking for someone We only copy height , weight , hobby ( This is a reference type Attention ) But do not copy the name ( I did it on purpose )
<> three , pile up
Girl
package prototype; import java.util.List; /** * @author Day and night programming of Muzi */ public class
Girl { // name String name; // height cm int height; // weight kg int weight; // hobby List<
String> natures; // clone public Girl cloneGirl(){ // No initialization If you copy I'll give you a lonely null //
I wanted to write it abstract , But the back needs new Gilr() So there is no abstraction return null; } @Override public String
toString() { return "Girl{" + "name='" + name + '\'' + ", height=" + height +
", weight=" + weight + ", natures=" + natures + '}'; } }
GirlLikeXiaoQi
package prototype; import java.util.ArrayList; import java.util.Arrays; /** *
@author Day and night programming of Muzi */ public class GirlLikeXiaoQi extends Girl{ // Initializes all properties of Xiaoqi in the constructor
public GirlLikeXiaoQi(){ // It's time-consuming here It will take years this.name = " Xiao Qi "; this.height = 165;
this.weight = 60; this.natures = Arrays.asList(" good "," tender "," versatile "); } @Override
public Girl cloneGirl() { Girl girl = new Girl(); // Name not copied // Copy height girl.height =
this.height; // Duplicate weight girl.weight = this.weight; // Copy Character // So you're wrong //
girl.natures = this.natures; // Need this That is, copy the elements inside girl.natures = new ArrayList<>(
this.natures); return girl; } }
GirlLikeXiaoZi
package prototype; import java.util.ArrayList; import java.util.Arrays; /** *
@author Day and night programming of Muzi */ public class GirlLikeXiaoZi extends Girl{ // Initializes all properties of Xiaoqi in the constructor
public GirlLikeXiaoZi(){ // It's time-consuming here It will take years this.name = " Little purple "; this.height = 175;
this.weight = 70; this.natures = Arrays.asList(" generous "," Intellectual "," Filial piety "); } @Override
public Girl cloneGirl() { Girl girl = new Girl(); // Name not copied // Copy height girl.height =
this.height; // Duplicate weight girl.weight = this.weight; // Copy Character // So you're wrong //
girl.natures = this.natures; // Need this That is, copy the elements inside girl.natures = new ArrayList<>(
this.natures); return girl; } }
GirlCache
package prototype; import java.util.HashMap; /** * @author Day and night programming of Muzi */ public
class GirlCache { // Store all known objects static HashMap<String, Girl> girls; static { girls
= new HashMap<>(); girls.put(" Xiao Qi ", new GirlLikeXiaoQi()); girls.put(" Little purple ", new
GirlLikeXiaoZi()); } // Look for image xxx Same object static Girl getGirl(String name){ if (girls.
containsKey(name)) { // have Copy one for you return girls.get(name).cloneGirl(); } else {
// No, rolling return null; } } }
Test
package prototype; /** * @author Day and night programming of Muzi */ public class Test { public static
void main(String[] args) { // Take a look at Xiao Qi Girl xiaoqi = GirlCache.girls.get(" Xiao Qi ");
System.out.println(xiaoqi); // Find an object like Xiaoqi Girl likeXiaoQi = GirlCache.getGirl(
" Xiao Qi "); // Can see Except the name Everything else is the same System.out.println(likeXiaoQi); // The name becomes cousin Xiaoqi , Character adds a sense of humor
Do anything It doesn't affect Xiaoqi likeXiaoQi.name = " Cousin Xiaoqi "; likeXiaoQi.natures.add(" humor "); System.
out.println(likeXiaoQi); // See if Xiaoqi has changed No change Xiao Qi introduced you to her cousin , About you and her cousin , It's none of Qi's business , //
You can't always follow Xiao Qi , That's sick System.out.println(xiaoqi); // At this time, Xiao Liang took a look Is it so convenient I'm looking for it, too I'm looking for someone like ya ya
Girl likeyaya = GirlCache.girls.get(" Ya ya "); // Can see The small bright object is null That is, there is no such thing as Yaya
Will give you a lonely return System.out.println(" Small bright object :"+likeyaya); // Then Xiao Ming came Said I wanted something like little purple
Xiao Zi sounds like his idol Deng Ziqi Girl likexiaozi = GirlCache.getGirl(" Little purple "); // Xiao Ming finds the right person
likexiaozi.name = " Purple chess "; likexiaozi.natures.add(" I love singing "); System.out.println(
" Xiaoming object :"+likexiaozi); } }
Output results :
<> four , Nag
In the future, people will say what you eat and what I eat , I'll buy what you buy , I'll do whatever you say
You can answer him : Prototype dog
He is based on you , Copy it into his own , It's not much different from copying homework .
Technology
Daily Recommendation