77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-03-22 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

设计模式

is a Tree\

this.rightChild.getTreeInfo(); }

if(this.rightChild==null) {

System.out.println(getName()+\ }

//System.out.println(getName()+\高度 是 \ }

public int getLength() { if(this.leftChild==null) { if(this.rightChild==null) return 1; else

return this.rightChild.getLength()+1; } else {

if(this.rightChild==null) {

return this.leftChild.getLength()+1; } else {

if((this.leftChild.getLength())>=(this.rightChild.getLength())) return this.leftChild.getLength()+1; else

return this.rightChild.getLength()+1; } } }

public static void main(String[] args) { } }

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

package binarytree;

public class Leaf extends Component{ private String name;

private Component leaf=null; public Leaf(String name) { this.name=name; }

public Component addLeftChild(Component leftChild){

32

设计模式

return this; }

public Component addRightChild(Component rightChild){ return this; }

public Component addChild(Component leftChild,Component rightChild){ return this; }

public String getName(){ return name; }

public int getLength() { return 1; }

public static void main(String[] args) { // Leaf leaf1 = new Leaf(); } }

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

package binarytree;

public class Test {

public Test() { }

public static void main(String[] args) { Component tree=new Tree(\

Component leaf_child=new Leaf(\ Component right_child=new Leaf(\ tree=tree.addChild(leaf_child,right_child); //tree=tree.addRightChild(right_child); tree.getTreeInfo();

Component tree1=new Tree(\ tree1.addChild(tree,leaf_child); tree1.getTreeInfo();

Component tree2=new Tree(\ tree2.addChild(tree,null); tree2.getTreeInfo();

Component tree4=new Tree(\ tree4.addChild(null,tree); tree4.getTreeInfo();

System.out.println(tree4.getName()+\ \

33

设计模式

} }

14, Mediator 模式理解

用于一系列关于对象交互行为,用一个中介对象

public interface Mediator{ }

public class ConcreateMediator implements Midiator{ private Member m1=new ConcreateMember1(); private Member m2=new ConcreateMember2(); public void operation{

//比如说把m1的成员变量和m2的成员变量互换 } }

public class Member{

private Mediator mediator; public Mddiator getMediator() {

return mediator; }

public void setMediator(Mediator mediator)

this.mediator=mediator; }

public class ConcreateMember1 extends Member{ }

public class ConcreateMember2 extends Member{ }

/******************************/ GOF经典赏析 模式与多态

Factory Method实现了创建的多态 Prototype实现了拷贝的多态

34

设计模式

。Builder实现了创建过程的多态

/******************************/ GOF片段赏析

1,Decrator 模式不同于Adapter模式,因为装饰仅改变对象的职责而不改变它的接口,而适配器将给对象一个全新的接口。

2,Composite模式:可以将装饰视为一个退化的、仅有一个组件的组合。然而,装饰仅给对象添加一些额外的职责---它的目的不是在于对象聚集

3,Strategy模式:用一个装饰你可以改变对象的外壳,而Strategy模式使得你可以改变对象的内核,这是改变对象的两种途径。

/*******************************/ 15.singleton 模式该错

以下说法有什么错误??

Singleton模式:

Singleton模式主要作用是保证在Java应用程序中,一个Class只有一个实例存在。

一般有三种方法:

1 定义一个类,它的构造函数为private的,所有方法为static的。如java.lang.Math

其他类对它的引用全部是通过类名直接引用。例如: public final class Math { /**

* Don't let anyone instantiate this class. */

private Math() {}

public static int round(float a) { return (int)floor(a + 0.5f); } ... }

2 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类初始化时

实例话,通过一个public的getInstance方法获取对它的引用,继而调用其中的方法。例如:

public class Runtime {

35

设计模式

private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() { return currentRuntime; } ... }

3 定义一个类,它的构造函数为private的,它有一个static的private的boolean变量,用于表示

是否有实例存在。例如:

class PrintSpooler {

//this is a prototype for a printer-spooler class //such that only one instance can ever exist static boolean

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库我写的设计模式的文章总结(7)在线全文阅读。

我写的设计模式的文章总结(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/537887.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: