In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to realize the java prototype pattern". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the java prototype pattern" can help you solve the problem.
The prototype pattern belongs to the object creation mode. Indicate the type of all created objects by giving a prototype object, and then create more objects of the same type by copying the prototype object. In short: copy and paste.
Introduction
Intention: specify the type of object to be created with prototype instances, and create new objects by copying these prototypes.
Main solution: build and delete prototypes at run time.
When to use:
1. When a system should be created, constructed and represented independently of its products.
2. When the class to be instantiated is specified at run time, for example, through dynamic loading.
3. To avoid creating a factory class hierarchy parallel to the product class hierarchy.
4. When an instance of a class can only have one of several different combinations of states. It may be more convenient to build a corresponding number of prototypes and clone them than to instantiate the class manually with the appropriate state each time.
How to solve it: use an existing prototype object to quickly generate the same instance as the prototype object.
Key code:
1. Realize the cloning operation, inherit Cloneable in JAVA and rewrite clone (). In .NET, you can use the MemberwiseClone () method of the Object class to achieve shallow copy of the object or serialization to achieve deep copy.
2. The prototype pattern is also used to isolate the coupling relationship between users of class objects and specific types (volatile classes). It also requires these "volatile classes" to have a stable interface.
Application example:
1. Cell division.
2. The Object clone () method in JAVA.
Advantages:
1. Performance improvement.
2. Evade the constraints of the constructor.
Disadvantages:
1. Equipped with cloning methods, you need to consider the function of the class as a whole, which is not very difficult for new classes, but not necessarily easy for existing classes, especially when a class references an indirect object that does not support serialization, or when the reference contains a circular structure.
2. The Cloneable interface must be implemented.
Use the scene:
1. Resource optimization scenario.
2. Class initialization needs to digest a lot of resources, including data, hardware resources, and so on.
3. Scenarios of performance and security requirements.
4. Generating an object through new requires very tedious data preparation or access permissions, then you can use the prototype pattern.
5. A scene with multiple modifiers for one object.
6. When an object needs to be provided for other object access, and each caller may need to modify its value, you can consider using prototype mode to copy multiple objects for caller use.
7. In a real project, the prototype pattern rarely appears alone, usually with the factory method pattern, creating an object through the clone method and then providing it to the caller by the factory method. The prototype pattern has been integrated with Java, and you can use it at your fingertips.
Note: unlike constructing a new object by instantiating a class, the prototype pattern generates a new object by copying an existing object. Shallow copy implements Cloneable, rewrite, and deep copy by implementing Serializable to read the binary stream.
Realize
We will create an abstract class Shape and an entity class that extends the Shape class. The next step is to define the class ShapeCache, which stores shape objects in a Hashtable and returns their clones when requested.
PrototypePatternDemo, our demo class uses the ShapeCache class to get the Shape object.
Step 1
Create an abstract class that implements the Cloneable interface.
Shape.javapublic abstract class Shape implements Cloneable {private String id; protected String type; abstract void draw (); public String getType () {return type;} public String getId () {return id;} public void setId (String id) {this.id = id;} public Object clone () {Object clone = null; try {clone = super.clone ();} catch (CloneNotSupportedException e) {e.printStackTrace () } return clone;}} step 2
Create an entity class that extends the abstract class above.
Rectangle.javapublic class Rectangle extends Shape {public Rectangle () {type = "Rectangle";} @ Override public void draw () {System.out.println ("Inside Rectangle::draw () method.");}} Square.javapublic class Square extends Shape {public Square () {type = "Square";} @ Override public void draw () {System.out.println ("Inside Square::draw () method.") }} Circle.javapublic class Circle extends Shape {public Circle () {type = "Circle";} @ Override public void draw () {System.out.println ("Inside Circle::draw () method.");}} step 3
Create a class, get entity classes from the database, and store them in a Hashtable.
ShapeCache.javaimport java.util.Hashtable;public class ShapeCache {private static Hashtable shapeMap = new Hashtable (); public static Shape getShape (String shapeId) {Shape cachedShape = shapeMap.get (shapeId); return (Shape) cachedShape.clone ();} / / run a database query for each shape and create the shape / / shapeMap.put (shapeKey, shape) / for example, we want to add three shapes public static void loadCache () {Circle circle = new Circle (); circle.setId ("1"); shapeMap.put (circle.getId (), circle); Square square = new Square (); square.setId ("2"); shapeMap.put (square.getId (), square); Rectangle rectangle = new Rectangle (); rectangle.setId ("3") ShapeMap.put (rectangle.getId (), rectangle);}} step 4
PrototypePatternDemo uses the ShapeCache class to obtain clones of shapes stored in Hashtable.
PrototypePatternDemo.javapublic class PrototypePatternDemo {public static void main (String [] args) {ShapeCache.loadCache (); Shape clonedShape = (Shape) ShapeCache.getShape ("1"); System.out.println ("Shape:" + clonedShape.getType ()); Shape clonedShape2 = (Shape) ShapeCache.getShape ("2"); System.out.println ("Shape:" + clonedShape2.getType ()); Shape clonedShape3 = (Shape) ShapeCache.getShape ("3") System.out.println ("Shape:" + clonedShape3.getType ());}} step 5
Execute the program and output the result:
Shape: CircleShape: SquareShape: Rectangle's content on "how to implement the java prototype pattern" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.