Prototype model-java design mode 5

Source: Internet
Author: User

Prototype mode: You can copy one or more Prototype classes and modify or fill in specific content to obtain the target class. If the target class is similar to the prototype class or they belong to a type group (especially from a data set object), the prototype mode can be used.
For example, the contestant list applet creates a Vector object for the TimeSwimData class list, and then copies and modifies the Vector values of the AgeSwimData class and sex1_data class objects, to obtain different objects.
Its UML class diagram is:

Program parsing:
1. Declare the jsondata class in the main program class javasinfo class. The aggregmer Class Object references the subclass of jsondata. In its constructor:
Java code
Public region Info ()
{
Super ("Prototype example ");
Sdata = new TimeSwimData ("d:/mongomers.txt ");
SetGUI ();
LoadswList (sdata, swList );
}
 
In the TimeSwimmData constructor:
Java code
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;"> public TimeSwimData (String filename)
{
String s = "";
Radimers = new Vector ();
InputFile f = new InputFile (filename );
S = f. readLine ();
While (s! = Null ){
Extends MERs. addElement (new Sort mer (s ));
S = f. readLine ();
}
F. close ();
} </Span>
2. The important deepClone () method declared in the common plain Data Abstract class allows the subclass to call it to obtain the independent TimeSwimmData class Vector data.
Java code
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;"> public writable data deepClone ()
{
Try {
ByteArrayOutputStream B = new ByteArrayOutputStream ();
ObjectOutputStream out = new ObjectOutputStream (B );
 
Out. writeObject (this); </span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; color: # ff0000; font-size: small; "> // stream replication is performed on the current object, which changes the current object. </Span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;">
ByteArrayInputStream bIn = new ByteArrayInputStream (B. toByteArray ());
ObjectInputStream oi = new ObjectInputStream (bIn );
 
Return (Response Data) oi. readObject ());
}
Catch (Exception e ){
System. out. println ("exception:" + e. getMessage ());
E. printStackTrace ();
Return null;
}
} </Span>

3. The subclass sorts (modifies) The obtained Vector data to obtain the new Vector data so that its object has new features. For example, the sexdeskdata class first obtains the above-mentioned object with the constructor method, and then obtains the new object with the sort () method:
Constructor. The sd parameter is the object created by TimeSwimData:
Java code
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;"> public sex1_data (invalid data sd ){
Export data = sd. deepClone (); </span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; color: # ff0000; font-size: small; "> // get the object after deep copy of the stream. </Span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;">
Female = true;
} </Span>
Sort () method:
Java code
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;"> public void sort (){
SexData = new Vector (); </span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; color: # ff0000; font-size: small;"> // calls outside the object, so that the Vector attribute of the object remains the changed value here. </Span>
 
 
 
 
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;">
For (int I = 0; I <partition data. size (); I ++ ){
Timer mer sw = program data. getSwimmer (I );
If (sw. isFemale () = female ){
SexData. addElement (sw );
}
}
} </Span>
In this way, the sexdeskdata class object that changes the original object Vector data is obtained, and the prototype mode is implemented.

Extension: 1. Copy form. The above method using the ByteArrayOutputStream class is very effective when the prototype data is complex. Of course, you can also use the re-declaration of an array or vector to retrieve and assign values to the original data one by one, then place it in the constructor method of the target class to implement deep copy (this is also a simple and commonly used method for copying data members). For example, the corresponding method is not implemented in replicated data, in the TimeSwimData subclass, this constructor is used to copy itself to other subclasses to call its constructor to create an independent Vector:
Java code
<Span style = "font-family: arial, helvetica, sans-serif; font-size: small;"> public TimeSwimData (Vector sw ){
// This constructor copies vector as clone
Radimers = new Vector ();
For (int I = 0; I <sw. size (); I ++ ){
Radimers. add (sw. elementAt (I ));
}
} </Span>
2. when you click a button to generate the corresponding class decision point, the decision point can be a class registry or prototype manager, manage Sub-classes that have similar independent data members (but also manage multiple object groups). It views these feature data (you can choose to create the most appropriate class at runtime, this is combined with the Builder mode.
3. It can be combined with the factory model to make each specific class completely different from other available specific classes.

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.