This article describes the use of prototype mode for Java to show daily life. Share it for your reference, as follows:
1. Pattern definition
Specify the type of objects to be created with prototype instances and create new objects by copying these prototypes.
2. Examples of the model
1 Pattern Analysis
We use the daily work situation to illustrate this model.
2 storyline analysis diagram
3 Prototype mode static modeling
4 Code Examples
4.1 Prototype creation
package com.prototype.pojo;/** * Daily life class* * @author * */public class DayLife implements Cloneable { // Construct method public DayLife() { System.out.println("-- The construction method is executed! --"); } // Get up private String getUp; // Take the bus private String byBus; // Get off and buy breakfast private String getFood; // Take a nap at noon; // Start work in the afternoon private String afternoonWork; // Go home from get off work private String goHome; // Night leisure private String night; public String getGetUp() { return getUp; } public void setGetUp(String getUp) { this.getUp = getUp; } public String getByBus() { return byBus; } public void setByBus(String byBus) { this.byBus = byBus; } public String getGetFood() { return getFood; } public void setGetFood(String getFood) { this.getFood = getFood; } public String getNoon() { return noon; } public void setNoon(String noon) { this.noon = noon; } public String getAfternoonWork() { return afternoonWork; } public void setAfternoonWork(String afternoonWork) { this.afternoonWork = afternoonWork; } public String getGoHome() { return goHome; } public void setGoHome(String goHome) { this.goHome = goHome; } public String getNight() { return night; } public void setNight(String night) { this.night = night; } /** * Print out daily life information*/ public void print() { System.out.println(this.getGetUp()); System.out.println(this.getByBus()); System.out.println(this.getGetFood()); System.out.println(this.getNoon()); System.out.println(this.getAfternoonWork()); System.out.println(this.getGoHome()); System.out.println(this.getNight()); } /** * clone method*/ @Override public DayLife clone() { try { // Call the clone method of the superclass (superclass? No class is integrated? Where does the superclass come from? Don't forget, all classes are subclasses of Object!) return (DayLife) super.clone(); } catch (Exception e) { } return null; }}4.2 Create an abstract factory that generates prototype objects
package com.prototype.factory;import com.prototype.pojo.DayLife;/** * Factory class* * @author * */public interface ILifeFactory { /** * Production DayLife object* * @return */ public DayLife getNewInstance();}4.3 Create a specific factory for generating prototype objects
package com.prototype.factory.impl;import com.prototype.factory.ILifeFactory;import com.prototype.pojo.DayLife;/** * Factory implementation class* * @author * */public class LifeFactoryImpl implements ILifeFactory { // DayLife object instance is used to initialize private static DayLife dayLife = null; /** * getNewInstance method implementation: * * First determine whether dayLife is null: * If it is null, use new to create a DayLife object, set the initial content, then assign it to the dayLife object instance, and then return; * If it is not null, use dayLift's clone method to generate a new object and copy it to the dayLife object, and then return */ @Override public DayLife getNewInstance() { // Determine whether dayLife is null if (dayLife == null) { // If null // The output is an object generated using new. Note: Use this new only once! System.out.println(" new DayLife !"); // Set the content of dayLife dayLife = new DayLife(); dayLife.setGetUp("Get get up at 7:00"); dayLife.setByBus("Take bus at 7:30"); dayLife.setGetFood("Getgetget get off at the bus stop near the company at 8:30, and when passing the breakfast car next to the road, you will buy breakfast and take it to the company together"); dayLife.setNoon("Lunch is solved in a small restaurant near the company, and then take a nap in the office seat"); dayLife.setAfternoonWork("13:30 started afternoon work"); dayLife.setGoHome("17:30 got off work on time"); dayLife.setNight("evening entertainment"); } else { // If not null // The output is an object generated using the clone method System.out.println("clone DayLife !"); // Assign the clone object to dayLife and return dayLife = dayLife.clone(); } return dayLife; }}4.4 Daily work scenario display
package com;import com.prototype.factory.ILifeFactory;import com.prototype.factory.impl.LifeFactoryImpl;import com.prototype.pojo.DayLife;/** * Main application* * @author * */public class Client { public static void main(String[] args) { // Create factory ILifeFactory lifeFactory = new LifeFactoryImpl(); // Print out DayLife default content lifeFactory.getNewInstance().print(); // Get DayLife again, modify getUp content and output content System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- dayLife2.setGetUp("I stayed in bed in the morning! I got up until 7::30!"); // dayLife2.print(); }}Running results
new DayLife!
-- The construction method has been executed! --
Get up at 7:00
Take the bus at 7:30
Get off at the bus stop near the company at 8:30. When passing by the breakfast car next to the road, you will buy breakfast and take it to the company for lunch. Then take a nap in the office seat.
The afternoon work started at 13:30
Get off work on time at 17:30 and have fun at night
----------------------------
clone DayLife!
Stay in bed in the morning! 7::15 before getting up!
Take the bus at 7:30
Get off at the bus stop near the company at 8:30. When passing by the breakfast car next to the road, you will buy breakfast and take it to the company for lunch. Then take a nap in the office seat.
The afternoon work started at 13:30
Get off work on time at 17:30 and have fun at night
3. The original design of this model
1 The object's constructor method is not executed when cloning an object
2 shallow copy and deep copy
4. Use occasions
1 The process of generating a counterpart is relatively complicated, and initialization requires many resources.
2 When you want the framework prototype and the generated object to be separated
3 When the same object may be accessed by other callers at the same time
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.