组合模式 Jave与C++实现及比较

来源:互联网 发布:收单数据保密协议 编辑:程序博客网 时间:2024/05/23 00:22

初学Java,觉得不关注资源管理,总让人惴惴不安呢。

组合模式可以让程序员面向接口,通过统一的方法对树和节点进行编程,降低了树状结构的使用难度。

package javaApp;import java.util.*;abstract class component{public abstract void add(component c);public abstract void remove(component c);public abstract void operation();}class leaf extends component{@Overridepublic void add(component c) {// TODO Auto-generated method stub}@Overridepublic void remove(component c) {// TODO Auto-generated method stub}@Overridepublic void operation() {// TODO Auto-generated method stubSystem.out.println("leaf operation...");}}class composite extends component{private ArrayList<component> _list = new ArrayList<component>();@Overridepublic void add(component c) {// TODO Auto-generated method stub_list.add(c);}@Overridepublic void remove(component c) {// TODO Auto-generated method stub_list.remove(c);}@Overridepublic void operation() {// TODO Auto-generated method stubfor(component obj : _list)obj.operation();}}public class compTest {public static void main(String[] args){component l0 = new leaf();component l1 = new leaf();component l2 = new leaf();component c0 = new composite();component c1 = new composite();c0.add(l0);c0.add(l1);c0.add(l2);c1.add(l0);c1.add(l1);c0.add(c1);c0.operation();System.out.println("-------------");c0.remove(l0);c0.remove(l1);c0.operation();}}

C++实现的方法,因为参见了几篇C++都没考虑资源管理,导致泄漏,重新用Boost::shared_ptr包装一下:

#ifndef COMPONENT_H#define COMPONENT_H#include <list>#include <boost/shared_ptr.hpp>using namespace std;using namespace boost;class component{public:    typedef boost::shared_ptr<component> SharedComp;        virtual void add (SharedComp c) = 0;    virtual void remove (SharedComp c) = 0;    virtual void action ( ) = 0;    virtual ~component ( ) {};};class leaf : public component{public:    virtual void add (SharedComp c);    virtual void remove (SharedComp c);    virtual void action ( );};class composite : public component{public:    typedef std::list<SharedComp> MyType;    virtual void add (SharedComp c);    virtual void remove (SharedComp c);    virtual void action ( );    ~composite ( );private:    MyType _list;};#endif

// component.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <boost/bind.hpp>#include <boost/foreach.hpp>#include "component.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){    component::SharedComp L0 (new leaf());    component::SharedComp L1 (new leaf());    //l->action ( );    component::SharedComp c (new composite);    component::SharedComp c2 (new composite);    c->add (L0);    c->add (L0);    c->add (L0);    //c->action ( );    c2->add (L1);    c2->add (c);    c2->action ( );    std::cout << std::endl;    c2->remove (c);    c2->action ( );return 0;}void leaf::add (SharedComp c){    throw std::exception ("The method or operation is not implemented.");}void leaf::remove (SharedComp c){    throw std::exception ("The method or operation is not implemented.");}void leaf::action ( ){    std::cout << "void leaf::action ( )" << std::endl;}//--------------------------------------------------void composite::add (SharedComp c){    if (c.get() != this)        _list.push_back (c);}void composite::remove (SharedComp c){//     MyType::iterator it = std::find_if (_list.begin ( ), _list.end ( ), boost::bind (&SharedComp::get, _1) == c);//     if (it != _list.end ( ))//         _list.erase (it);    _list.remove_if (boost::bind (&SharedComp::get, _1) == boost::bind(&SharedComp::get,c));}void composite::action ( ){//     BOOST_FOREACH (SharedComp& c, _list)//         c->action ( );    std::for_each (_list.begin ( ), _list.end ( ), boost::bind(&component::action,_1));}composite::~composite ( ){}



0 0