Prototype mode application - showing everyday life

A schema definition
specifies the kinds of objects to create with prototype instances, and creates new objects by duplicating these prototypes.

 

Example of the second mode
1. Analysis of
the mode We use the daily work scene to illustrate this mode.
2 Storyline Analysis Figure


 
3 Prototype Mode Static Modeling


 
4 Code Examples
4.1 Prototype Establishment

package com.prototype.pojo;

/**
 * Daily life
 *
 * @author
 *
 */
public class DayLife implements Cloneable {
	// Construction method
	public DayLife() {
		System.out.println("-- The constructor is executed! --");
	}

	// get up
	private String getUp;
	// take the bus
	private String byBus;
	// get off, buy breakfast
	private String getFood;
	// noon nap
	private String noon;
	// start work in the afternoon
	private String afternoonWork;
	// go home after get off work
	private String goHome;
	// evening 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 is the superclass? 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 {
	/**
	 * Produce DayLife objects
	 *
	 * @return
	 */
	public DayLife getNewInstance();
}

 4.3 Create a concrete factory that generates 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 for initialization
	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 at the same time, then assign it to the dayLife object instance, and return;
	 * If it is not null, use the clone method of dayLift to generate a new object and copy it to the dayLife object, then return
	 */
	@Override
	public DayLife getNewInstance() {
		// Determine if dayLife is null
		if (dayLife == null) {
			// if null
			// The output is the object produced with new. Note: new is only used once!
			System.out.println(" new DayLife !");
			// Set dayLife content
			dayLife = new DayLife();
			dayLife.setGetUp("7:00起床");
			dayLife.setByBus("Take the bus at 7:30");
			dayLife.setGetFood("Get off at the bus stop near the company at 8:30. When passing the breakfast car on the roadside, I will buy breakfast and bring it to the company");
			dayLife.setNoon("Have lunch at a small restaurant near the company, then take a nap in the office chair");
			dayLife.setAfternoonWork("Afternoon work started at 13:30");
			dayLife.setGoHome("Get off work on time at 17:30");
			dayLife.setNight("Night entertainment");
		} else {
			// if not null
			// The output is the object produced 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 presentation

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 the default content of DayLife
		lifeFactory.getNewInstance().print();

		// Get DayLife again, modify the content of getUp and output the content
		System.out.println("------------------------");
		DayLife dayLife = lifeFactory.getNewInstance();
		dayLife.setGetUp("Stay in bed in the morning! Wake up at 7::15!");
		dayLife.print();

		// Get DayLife again, modify the content of getUp and output the content
		// System.out.println("------------------------");
		// DayLife dayLife2 = lifeFactory.getNewInstance();
		// dayLife2.setGetUp("Stay in bed in the morning! Wake up at 7::30!");
		// dayLife2.print();
	}
}

 operation result

 new DayLife !
-- Constructor executed! -- Get up at
7:00, get up at
7:30, take the bus
at 8:30 and get off at the bus stop near the company. When passing the breakfast car on the roadside, I will buy breakfast and bring it to the company for
lunch at a small restaurant near the company. Take a nap on the chair in the office
13:30 Start the afternoon work
17:30 Get off work on time and have
fun in the evening
------------------------
 clone DayLife !
Stay in bed in the morning! Wake up at 7:15!
Take the bus at 7:30
and get off at the bus stop near the company at 8:30. When passing the breakfast car on the roadside, I will buy breakfast and bring it to the company for
lunch. I will solve it at a small restaurant near the company, and then take a nap on the seat in the office. A meeting
starts at 13:30 and starts the afternoon work
at 17:30 and gets off work on time for
leisure and entertainment in the evening

 

Three The design of this mode is just 1. The construction method of the object does not perform 2. Shallow copy and deep copy
when the object is cloned

 

Four use cases
1. The process of generating the pair is more complicated, and the initialization requires many resources.
2. When the framework prototype and the generated object are expected to be separated.
3. When the same object may be accessed by other callers at the same time

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640773&siteId=291194637