基于抽象类实现的原型模式
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package top.fjy8018.designpattern.pattern.creational.prototype.abstractprototype;
|
||||
|
||||
/**
|
||||
* 使用抽象类或者接口实现克隆接口可使得其所有子类都有克隆能力
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-25 22:36
|
||||
*/
|
||||
public abstract class AbstractPig implements Cloneable {
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package top.fjy8018.designpattern.pattern.creational.prototype.abstractprototype;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-25 22:37
|
||||
*/
|
||||
public class Pig extends AbstractPig {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package top.fjy8018.designpattern.pattern.creational.prototype.abstractprototype;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Slf4j
|
||||
class PigTest {
|
||||
|
||||
@Test
|
||||
void cloneTest() throws Exception {
|
||||
Pig p1 = new Pig();
|
||||
Pig p2 = (Pig) p1.clone();
|
||||
log.info(Integer.toHexString(p1.hashCode()));
|
||||
log.info(Integer.toHexString(p2.hashCode()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user