Java Prototyping mode (prototype mode)

Source: Internet
Author: User

Prototype Schema Definition: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.

The prototype mode allows an object to create another customizable object without having to know the details of how to create it, by passing a prototype object to the object that is being created, which creates the object by requesting the prototype object to copy themselves to implement the creation. How to use the prototype mode because the Clone () method is provided in Java to implement the cloning of the object, so the prototype pattern implementation suddenly becomes very simple. Take the spoon as an example: Public abstract class Abstractspoon implements cloneable{
String Spoonname;
public void Setspoonname (String spoonname) {this.spoonname = Spoonname;}
Public String Getspoonname () {return this.spoonname;}
Public Object Clone () {
Object object = null;
try {
Object = Super.clone ();
} catch (Clonenotsupportedexception exception) {
System.err.println ("Abstractspoon is not cloneable");
}
return object;
}
} has two specific implementations (CONCRETEPROTOTYPE):
    • public class Soupspoon extends abstractspoon{
      Public Soupspoon () {
      Setspoonname ("Soup Spoon");
      }
      }
    • public class Saladspoon extends abstractspoon{
      Public Saladspoon () {
      Setspoonname ("Salad Spoon");
      }
      }

Calling prototype mode is simple:
    • Abstractspoon spoon = new Soupspoon ();
    • Abstractspoon spoon = new Saladspoon ();

Of course, you can also combine Factory mode to create Abstractspoon instances.

In Java, the prototype pattern becomes the use of the Clone () method, because Java's pure object-oriented nature makes it natural to use design patterns in Java, both of which are almost seamless. This is reflected in many patterns, such as interator traversal mode.
Prototype modeOverview
    Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
Applicability
    1. When a system should be independent of its product creation, composition and representation.    2. When the class to instantiate is specified at run time, for example, by dynamic loading.    3. To avoid creating a factory class hierarchy that is parallel to the product class hierarchy.    4. When an instance of a class can have only one of several different state combinations.    building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state.
participants
    1. Prototype       declares a clone of its own interface.    2. Concreteprototype       implements the operation of a clone itself.    3.       The Client makes a prototype clone itself to create a new object.
class DiagramExamplePrototype
public class Prototype implements cloneable {    private String name;        public void SetName (String name) {        this.name = name;    }        Public String GetName () {        return this.name;    }    Public Object Clone () {        try {            return super.clone ();        } catch (Exception e) {            e.printstacktrace ();            return null;}}    }
Concreteprototype
public class Concreteprototype extends Prototype {public    concreteprototype (String name) {        setName (name);    }}
Client
public class Test {public    static void Main (string[] args) {        Prototype pro = new Concreteprototype ("Prototype");        Prototype Pro2 = (Prototype) pro.clone ();        System.out.println (Pro.getname ());        System.out.println (Pro2.getname ());}    }
result
Prototypeprototype

Java Prototyping mode (prototype mode)

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.