image-20200723163713661.png
    使用原型实例来指定要创建对象的类型,通过复制这个原型来创建新对象。

    1. public interface Prototype{
    2. Prototype Clone();
    3. }
    4. public class ConcretePrototype extends Prototype{
    5. private String filed;
    6. public ConcretePrototype(String filed){
    7. this.filed = filed;
    8. }
    9. @Override
    10. Prototype Clone(){
    11. return new ConcretePrototype(filed)
    12. }
    13. @Override
    14. public String toString(){
    15. return filed;
    16. }
    17. }
    18. public class client(){
    19. public static void main(String[] args){
    20. Prototype prototype = new ConcretePrototype("abc");
    21. Prototype clone = prototype.clone();
    22. System.out.print(clone);
    23. }
    24. }

    结果:

    1. abc