我写的设计模式的文章总结(4)

2019-03-22 17:12

public String name=\ public String name1=\ // public Book book;

public Book book=new Book(); public Zhangsan() {

this.name=\ this.book=new Book(); }

public Object clone() {

16

设计模式

try {

return super.clone(); }

catch(CloneNotSupportedException e) {

return null; } }

public Object deepClone() throws IOException,OptionalDataException,ClassNotFoundException {

ByteArrayOutputStream bo=new ByteArrayOutputStream(); ObjectOutputStream oo=new ObjectOutputStream(bo); oo.writeObject(this);

ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi=new ObjectInputStream(bi); return (oi.readObject()); }

public Book getBook() {

return book; }

public String getName() {

return name; } }

/**--------------------------------------------------------------------------------**/ import java.io.*;

import java.io.Serializable;

public class Zhangsan implements Cloneable,Serializable {

public String name=\ public String name1=\ // public Book book;

public Book book=new Book(); public Zhangsan() {

this.name=\ this.book=new Book(); }

public Object clone()

17

设计模式

{

try {return super.clone(); }

catch(CloneNotSupportedException e) {

return null; } }

public Object deepClone() IOException,OptionalDataException,ClassNotFoundException {

ByteArrayOutputStream bo=new ByteArrayOutputStream(); ObjectOutputStream oo=new ObjectOutputStream(bo); oo.writeObject(this);

ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi=new ObjectInputStream(bi); return (oi.readObject()); }

public Book getBook() {

return book; }

public String getName() {

return name; } }

/**--------------------------------------------------------------------------------**/ public class test {

public static void main(String args[]) {

Zhangsan person=new Zhangsan();

Zhangsan copy= ( Zhangsan) person.clone(); System.out.println(person.name); System.out.println(copy.name);

System.out.println(\ \ System.out.println(\of \

try {

Zhangsan deepcopy= (Zhangsan) person.deepClone(); System.out.println(\of

throws false??? false??? 18

设计模式

\ System.out.println(\of false??? \

System.out.println(\ \ System.out.println(\ \ System.out.println(person.book.pages);

System.out.println(\ \ System.out.println(deepcopy.book.pages);

System.out.println(\ \ } catch (Exception e) {

System.out.println(\ } } }

所谓clone有深clone和浅clone

博士使用的利用串行化来进行深clone,但是不能保证深clone得到的结果是正确的,这是因为有些不能串行化的,比如说,如果这个zhangsan类它继承了一个abstract class,或者implements 一些接口,而 abstract class或接口有常量等不能串行化的东西,深clone可能会失败(deepClone方法本身没有失败,可是要访问clone的对象的属性时可能会失败!)

8,composite模式理解

composite模式就是把一些具有相类似的接口的类组合起来,提供一致的访问接口

三个角色

1,抽象构件component 2,Leaf

3,树枝角色compostie

public interface Component{

public Composite getComposite(); public void mehod1(); }

import java.until.*;

public class Composite implements Component{ private Vector vector_com=new vector(); public Composite getComposite() {

return this;

19

设计模式

}

public void mehod1(){

System.out.println(\

Emumeration emumeration=vector_com.elements(); while(emumeration.hasMoreElements()){

((Component ) emumeration.nextElement()).method1(); } }

public void add(Component component) {

vector_com.add(component); }

public void remove(Component component) {

vector_com.removeElement(component); } }

public class Leaf implements Component {

public void method1() {

System.out.println(\}

public Composite getComposite() {

return null; } }

9,迭代子模式理解

/************************************************************************/

public abstract class Aggregate {

public Iterator createIterator() {

return null; } }

/********************************************************************

20

设计模式

****/

public interface Iterator { void first(); void next();

boolean isDone(); Object currentItem(); }

/************************************************************************/

public class ConcreteAggregate extends Aggregate{

private Object[] obj={\唐僧\孙悟空\猪八戒\沙僧\白龙马\妖怪\public Iterator createIterator() {

return new ConcreteIterator(this); }

public Object getElement(int index) {

if(index

public int size() {

return obj.length; } }

/************************************************************************/

public class ConcreteIterator implements Iterator {

private ConcreteAggregate agg; private int index=0; private int size=0;

public ConcreteIterator(ConcreteAggregate agg) {

this.agg=agg;

this.size=agg.size(); index=0; }

public void first(){ index=0; }

public void next(){

21

设计模式

if(index

index++; } }

public boolean isDone(){ return (index>=size); }

public Object currentItem(){ return agg.getElement(index); } }

/************************************************************************/

public class Client {

private Iterator it;

private Aggregate agg=new ConcreteAggregate(); public void operation() {

it=agg.createIterator(); while(!it.isDone()) {


我写的设计模式的文章总结(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:股权购买协议 中英文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: