Design Mode-fast food simplified solution-[prototype mode]

Source: Internet
Author: User

Examples of prototype application scenarios]

For example, when you are playing a game called "need for speed", each player has its own account to identify the player, and each player can own the same vehicle, so that each player has the same car, for example, if you have an Audi A6, your wife will tell you that I want to launch the Audi A6, which is the same as my neighbor. Okay, now you have an Audi A6 in your house, if you drive your car to work every day, you can apply the prototype in this case. The prototype is very simple :) It is the copy Copy of the class.

I think the above example is inappropriate. It is introduced from other articles.

Resource Optimization scenarios
Class initialization needs to digest a lot of resources, including data and hardware resources.

Performance and security requirements
To generate an object through new requires tedious data preparation or access permissions, you can use the prototype mode.

Scenario of multiple modifier of an object
When an object needs to be accessed by other objects and each caller may need to modify its value, you can consider copying multiple objects in prototype mode for the caller to use.

Prototype]

Type: Creation Mode

Use a prototype instance to specify the object type and copy the prototype to create a new object.

Prototype mode UML diagram]

Prototype mode-Java code implementation]

Interface for creating a racing car:

Package car_interface;
Public interface car_interface {
Public void start ();
Public void stop ();
}

Implementation of New Audi cars:

Package car_imple;
Import car_fittings.car_tyre;
Import car_interface.car_interface;
Public class audi_imple implements car_interface, cloneable {
Private car_tyre car_tyre_ref;
Public void start (){
System. Out. println ("Audi A6 started ");

}

Public void stop (){
System. Out. println ("Audi A6 stopped ");

}

Public car_tyre getcar_tyre_ref (){
Return car_tyre_ref;
}

Public void setcar_tyre_ref (car_tyre car_tyre_ref ){
This. car_tyre_ref = car_tyre_ref;
}

@ Override
Public object clone () throws clonenotsupportedexception {
Super. Clone ();
Audi_imple = new audi_imple ();
Audi_imple.setcar_tyre_ref (New car_tyre ());

Return audi_imple;
}
}

In the implementation class of Audi cars, it should be noted that the clone method of the original protected type should be changed to public, so that the public can be made public, called, and the secret can be made public.

New accessories and tires for Audi Cars

Package car_fittings;

Public class car_tyre {

Private string name = "German manufacturer original tires ";

Public String getname (){
Return name;
}
}

Create a client running class:

Package run_main;

Import car_fittings.car_tyre;
Import car_imple.audi_imple;
Import car_interface.car_interface;

Public class run_main {

Public static void main (string [] ARGs ){

Try {
Audi_imple car_ref_my = new audi_imple ();
Car_ref_my.setcar_tyre_ref (New car_tyre ());
System. Out. println ("the parameter of my Audi car is:" + car_ref_my );
System. Out. println ("the tire parameters of my Audi car are:" + car_ref_my.getcar_tyre_ref ());

Audi_imple car_ref_other = (audi_imple) car_ref_my.clone ();
System. Out. println ("the parameter of another Audi is:" + car_ref_other );
System. Out. println ("Other Audi's tire parameters are :"
+ Car_ref_other.getcar_tyre_ref ());

} Catch (clonenotsupportedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

The program running result is as follows:

The parameter of my Audi car is: car_imple.audi_imple @ 9cab16
The tire parameters of my Audi car are: car_fittings.car_tyre @ 1a46e30
Other Audi's parameters are: car_imple.audi_imple @ 3e25a5
Other Audi's tire parameters are: car_fittings.car_tyre @ 19821f

According to the printed results, my car, my tires, and other people's cars and tires are different objects, but the "type" is the same: Audi cars, the original tires. In this example, "deep copy/deep copy" in the "prototype" is also implemented, that is, there is an object "tire" in the car class, for details about the concept of deep copy/deep copy, refer to other materials. The prototype mode is usually used when copying objects. In general cases, the new mode is used to re-create an object and copy attributes again. The code is highly repetitive, the appearance of the prototype mode avoids New Hard operations.

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.