原型模式

原型设计模式UML类图

原型设计模式UML类图

原型设计模式java实现

/**
* Prototype.
*/
public interface Prototype {

Object copy();

}

/**
* Beast.
*/
public abstract class Beast implements Prototype {

public Beast() {
}

public Beast(Beast source) {
}

@Override
public abstract Beast copy();

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}

/**
* OrcBeast.
*/
public class OrcBeast extends Beast {

private String weapon;

public OrcBeast(String weapon) {
this.weapon = weapon;
}

public OrcBeast(OrcBeast orcBeast) {
super(orcBeast);
this.weapon = orcBeast.weapon;
}

@Override
public OrcBeast copy() {
return new OrcBeast(this);
}

@Override
public String toString() {
return "Orcish wolf attacks with " + weapon;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (OrcBeast) obj;
if (weapon == null) {
return other.weapon == null;
}
return weapon.equals(other.weapon);
}


}

/**
* ElfBeast.
*/
public class ElfBeast extends Beast {

private String helpType;

public ElfBeast(String helpType) {
this.helpType = helpType;
}

public ElfBeast(ElfBeast elfBeast) {
super(elfBeast);
this.helpType = elfBeast.helpType;
}

@Override
public ElfBeast copy() {
return new ElfBeast(this);
}

@Override
public String toString() {
return "Elven eagle helps in " + helpType;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (ElfBeast) obj;
if (helpType == null) {
return other.helpType == null;
}
return helpType.equals(other.helpType);
}

}

/**
* Warlord.
*/
public abstract class Warlord implements Prototype {

public Warlord() {
}

public Warlord(Warlord source) {
}

@Override
public abstract Warlord copy();

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}

/**
* OrcWarlord.
*/
public class OrcWarlord extends Warlord {

private String weapon;

public OrcWarlord(String weapon) {
this.weapon = weapon;
}

public OrcWarlord(OrcWarlord orcWarlord) {
super(orcWarlord);
this.weapon = orcWarlord.weapon;
}

@Override
public OrcWarlord copy() {
return new OrcWarlord(this);
}

@Override
public String toString() {
return "Orcish warlord attacks with " + weapon;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (OrcWarlord) obj;
if (weapon == null) {
return other.weapon == null;
}
return weapon.equals(other.weapon);
}
}

/**
* ElfWarlord.
*/
public class ElfWarlord extends Warlord {

private String helpType;

public ElfWarlord(String helpType) {
this.helpType = helpType;
}

public ElfWarlord(ElfWarlord elfWarlord) {
super(elfWarlord);
this.helpType = elfWarlord.helpType;
}

@Override
public ElfWarlord copy() {
return new ElfWarlord(this);
}

@Override
public String toString() {
return "Elven warlord helps in " + helpType;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (ElfWarlord) obj;
if (helpType == null) {
return other.helpType == null;
}
return helpType.equals(other.helpType);
}
}

/**
* Mage.
*/
public abstract class Mage implements Prototype {

public Mage() {
}

public Mage(Mage source) {
}

@Override
public abstract Mage copy();

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}

/**
* OrcMage.
*/
public class OrcMage extends Mage {

private String weapon;

public OrcMage(String weapon) {
this.weapon = weapon;
}

public OrcMage(OrcMage orcMage) {
super(orcMage);
this.weapon = orcMage.weapon;
}

@Override
public OrcMage copy() {
return new OrcMage(this);
}

@Override
public String toString() {
return "Orcish mage attacks with " + weapon;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (OrcMage) obj;
if (weapon == null) {
return other.weapon == null;
}
return weapon.equals(other.weapon);
}
}

/**
* ElfMage.
*/
public class ElfMage extends Mage {

private String helpType;

public ElfMage(String helpType) {
this.helpType = helpType;
}

public ElfMage(ElfMage elfMage) {
super(elfMage);
this.helpType = elfMage.helpType;
}

@Override
public ElfMage copy() {
return new ElfMage(this);
}

@Override
public String toString() {
return "Elven mage helps in " + helpType;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
var other = (ElfMage) obj;
if (helpType == null) {
return other.helpType == null;
}
return helpType.equals(other.helpType);
}
}

/**
* Interface for the factory class.
*/
public interface HeroFactory {

Mage createMage();

Warlord createWarlord();

Beast createBeast();

}

/**
* Concrete factory class.
*/
public class HeroFactoryImpl implements HeroFactory {

private Mage mage;
private Warlord warlord;
private Beast beast;

/**
* Constructor.
*/
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
this.mage = mage;
this.warlord = warlord;
this.beast = beast;
}

/**
* Create mage.
*/
public Mage createMage() {
return mage.copy();
}

/**
* Create warlord.
*/
public Warlord createWarlord() {
return warlord.copy();
}

/**
* Create beast.
*/
public Beast createBeast() {
return beast.copy();
}

}

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Prototype pattern is a creational design pattern in software development. It is used when the
* type of objects to create is determined by a prototypical instance, which is cloned to produce
* new objects. This pattern is used to: - avoid subclasses of an object creator in the client
* application, like the abstract factory pattern does. - avoid the inherent cost of creating a new
* object in the standard way (e.g., using the 'new' keyword)
*
* <p>In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by
* cloning the existing ones. The factory's prototype objects are given as constructor parameters.
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
var factory = new HeroFactoryImpl(
new ElfMage("cooking"),
new ElfWarlord("cleaning"),
new ElfBeast("protecting")
);
var mage = factory.createMage();
var warlord = factory.createWarlord();
var beast = factory.createBeast();
LOGGER.info(mage.toString());
LOGGER.info(warlord.toString());
LOGGER.info(beast.toString());

factory = new HeroFactoryImpl(
new OrcMage("axe"),
new OrcWarlord("sword"),
new OrcBeast("laser")
);
mage = factory.createMage();
warlord = factory.createWarlord();
beast = factory.createBeast();
LOGGER.info(mage.toString());
LOGGER.info(warlord.toString());
LOGGER.info(beast.toString());
}
}