设计模式
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