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

2019-03-22 17:12

设计模式

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


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

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

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

马上注册会员

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