zoukankan      html  css  js  c++  java
  • java的原始模型(Prototype)模式(一) 基本原理

           前几天在学习javascript的面向对象编程,像我这种习惯于java式面向对象的人,在刚接触javascript时,还真是非常不适应,比如一个典型对象的结构:

    //构造函数
    function Complex(real ,imaginary){
    this.x = real;
    this.y = imaginary;

    }

    //实例方法
    Complex.prototype.toString = function(){
    return "{"+this.x +","+this.y+"}";
    }

    Complex.prototype.add
    = function(that){
    return new Complex(this.x+that.x,this.y+that.y);

    }

    //类方法
    Complex.sum = function(a,b){
    return new Complex((a.x+b.x),(a.y+b.y))
    }

    //类属性
    Complex.ZERO = new Complex(0,0);

        它跟传统的java有着很大的不同,特别是prototype 这个关键字显得特别突兀,后来仔细研究了《java与模式》一书,发现这其实是模式中的原始模式(Prototype模式),它的基础思想就是“通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象”,它用到的方法是Object类中的clone方法(Java中的所有类都是从java.lang.Object类继承)。具体方法为:

    protected Object clone()

      而且要实现安全地使用复制,类必须实现Cloneable接口。而该接口只有一个作用,就是在运行时通知jvm可以安全地在这个类上使用clone()方法,通过调用这个clone()可以得到一个对象的复制。由于Object对象本身并不是实现Cloneable接口。因此所有没有实现Cloneable接口的类在调用clone()时会抛出CloneNotSupportedException 异常。用以下例说明

    原型对象:

    public class PandaToClone implements Cloneable {
    private int height,weight,age;

    public PandaToClone(int height, int weight) {
    super();
    this.age = 0;
    this.height = height;
    this.weight = weight;
    }

    public int getHeight() {
    return height;
    }

    public void setHeight(int height) {
    this.height = height;
    }

    public int getWeight() {
    return weight;
    }

    public void setWeight(int weight) {
    this.weight = weight;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
    /****
    * 原始模式最重要的一个函数
    */
    public Object clone(){
    //创建一个本类的对象,不能够返回给调用者
    PandaToClone temp = new PandaToClone(height,weight);
    temp.setAge(age);
    return (Object)temp;
    }

    public String toString(){
    return "{weight:"+weight+",height:"+height+",age:"+age+"}";
    }

    }

    调用主类:

    public class Client {
    private static PandaToClone thisPanda,thatPanda;

    public static void main(String[] args){
    thisPanda
    = new PandaToClone(100,200);
    thisPanda.setAge(
    0);

    thatPanda
    = (PandaToClone)thisPanda.clone();
    System.out.println(
    "thispanda:"+thisPanda);
    System.out.println(
    "thtapanda:"+thatPanda);
    }

    }

    执行结果:

    thispanda:{weight:200,height:100,age:0}
    thtapanda:{weight:
    200,height:100,age:0}

        特别注意PandaToClone 类clone()这个函数的写法和Client 主函数的强制类型转换。 

          一般而言,clone()方法满足以下的描述:

          1.  对任何对象x,都有:x.clone() != x 。换言之,克隆对象与原对象不是同一个对象。

          2.  对任何的对象x。都有:x.clone().getClass() = x.getClass(),克隆对象与原对象的类型一样。

  • 相关阅读:
    牛客 158F 青蛙 (贪心)
    牛客 158D a-贝利福斯数
    长沙理工大学第十二届ACM大赛-重现赛 大家一起来数二叉树吧 (组合计数)
    美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)
    美团2017年CodeM大赛-初赛A轮 C合并回文子串
    活动安排问题
    0和5
    1489 蜥蜴和地下室
    1067 Bash游戏 V2
    1062 序列中最大的数
  • 原文地址:https://www.cnblogs.com/lengyuhong/p/2127957.html
Copyright © 2011-2022 走看看