b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

解释器设计模式_lua解释器交互模式_解释器模式c语言(9)

电脑杂谈  发布时间:2017-02-22 18:11:38  来源:网络整理

需要扩展一个类的功能。

动态的为一个对象增加功能,而且还能动态撤销。解释器设计模式(继承不能做到这一点,继承的功能是静态的,不能动态增删)

装饰器不想讲。大名鼎鼎的I/O字符字节流源码,大家自己看API和源代码。

个人觉得字节字符流的很多类是没有必要的或者可以大量合并的。为了兼容以前的接口或者需要增加某些小功能,而增加了过多的装饰类,让Java的I/O模块看起来十分臃肿。(哈哈,一不小心批评了大神的API)

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次

简单来说,就是类拥有自己的实例

下面是一个BinarySearchTree的例子,每个节点分别拥有自己的左右子节点

public class BinarySearchTree<T extends Comparable<? super T>> {

    private BinaryNode<T> root;  //root节点

    public BinarySearchTree() {
        this.root = null;
    }

    public void makeEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public boolean contain(T x) {
        return contain(x, root);
    }

    public T findMin() {
        if (isEmpty()) throw new IllegalArgumentException();
        return findMin(root).element;
    }

    public T findMax() {
        if (isEmpty()) throw new IllegalArgumentException();
        return findMax(root).element;
    }

    public void insert(T x) {
        root = insert(x, root);
    }

    public void remove(T x) {
        root = remove(x, root);
    }

    /**
     * Internal method to find an item in a subtree
     *
     * @param x is item to search for
     * @param t is the node that roots the subtree
     * @return node containing the mached item
     */
    private boolean contain(T x, BinaryNode<T> t) {
        if (t == null) {
            return false;
        }

        int compareResult = x.compareTo(t.element);
        if (compareResult < 0) {
            return contain(x, t.left);
        } else if (compareResult > 0) {
            return contain(x, t.right);
        } else {
            return true;
        }
    }

    /**
     * Internal method to find the smallest item in the subtree
     *
     * @param t the node that roots the subtree
     * @return the smallest item
     */
    private BinaryNode<T> findMin(BinaryNode<T> t) {
        if (t == null) {
            return null;
        } else if (t.left == null) {
            return t;
        } else {
            return findMin(t.left);
        }
    }

    /**
     * Internal method to find the largest item in the subtree
     *
     * @param t the node that roots the subtree
     * @return the largest item
     */
    private BinaryNode<T> findMax(BinaryNode<T> t) {
        if (t != null) {
            while (t.right != null) {
                t = t.right;
            }
        }
        return t;
    }

    /**
     * Internal method to insert into the subtree
     *
     * @param x the item to insert
     * @param t the node that roots the subtree
     * @return the new root of the subtree
     */
    private BinaryNode<T> insert(T x, BinaryNode<T> t) {
        if (t == null) {
            return new BinaryNode<T>(x, null, null);
        }

        int compareResult = x.compareTo(t.element);

        if (compareResult < 0) {
            t.left = insert(x, t.left);
        } else if (compareResult > 0) {
            t.right = insert(x, t.right);
        }

        return t;
    }

    /**
     * Internal method to remove from a subtree
     *
     * @param x the item to remove
     * @param t the node that roots the subtree
     * @return the new root of the subtree
     */
    private BinaryNode<T> remove(T x, BinaryNode<T> t) {
        if (t == null) {
            return t;
        }

        int compareResult = x.compareTo(t.element);

        if (compareResult < 0) {
            t.left = remove(x, t.left);
        } else if (compareResult > 0) {
            t.right = remove(x, t.right);
        } else if (t.left != null && t.right != null) {
            t.element = findMin(t.right).element;
            t.right = remove(t.element, t.right);
        } else {
            t = t.left != null ? t.left : t.right;
        }
        return t;
    }

    /**
     * 查找二叉树节点类
     *
     * @param <T>
     */
    private static class BinaryNode<T> {
        private T element;
        private BinaryNode<T> left;
        private BinaryNode<T> right;

        public BinaryNode(T element) {
            this(element, null, null);
        }

        public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
            this.element = element;
            this.left = left;
            this.right = right;
        }
    }
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33899-9.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 王播
      王播

      他美爹都没法说这句话

    热点图片
    拼命载入中...