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

初学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
{

	@Override
	public void add(component c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(component c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void operation() {
		// TODO Auto-generated method stub
		System.out.println("leaf operation...");
	}
	
}

class composite extends component
{
	private ArrayList<component> _list = new ArrayList<component>();
	@Override
	public void add(component c) {
		// TODO Auto-generated method stub
		_list.add(c);
	}

	@Override
	public void remove(component c) {
		// TODO Auto-generated method stub
		_list.remove(c);
	}

	@Override
	public void operation() {
		// TODO Auto-generated method stub
		for(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值