设计模式学习与实践(十六)----不变模式(immutable)

来源:互联网 发布:淘宝上货到付款靠谱吗 编辑:程序博客网 时间:2024/06/07 20:37

1.1.1        描述

l         正规描述

一个对象的状态在对象被创建之后就不再变化,这就是所谓的不变模式。

l         形象描述

经过漫长的追求过程之后,MM终于答应嫁给我,条件是结婚后只能对她一个人好,不能变心。

1.1.2        类图&示意代码

public class Immutable(){

private Attribute attribute;

 

public Immutable(Attribute attribute){

    this.attribute = attribute;

}

 

public Attribute read(){

    return attribute;

}

 

public void Operation1(){

    //to do, do not modify attribute

}

}

 

public class Client(){

public void main(String args[]){

    Attribute attr = new Attribute();

    Immutable im = new Immutable(attr);

    System.out.println(im.read().toString());

    im.Operation1();

    System.out.println(im.read().toString());

}

}

 

1.1.3        举例

1.1.4        比较

原创粉丝点击