大中型项目之软件设计方法-设计模式-延迟加载技术(Lazy initialization)

  原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/bluelidy

 基于OO良好设计的系统,最终交互的原子是对象。对象在其被创建,直至销毁的过程中,其生命周期中所进行的各种状态的迁移,都是为系统中与之交互的其他对象服务的。鉴于此,在进行软件设计或软件的实现中合理地管理对象,对提高系统的性能、简化对象之间的协作逻辑大有裨益。

   在计算机编程领域,延迟加载技术是一种战术,其可以延迟对象的创建,延迟计算一些数据,等其他一些非常耗时和重要的执行过程,这些动作都可以在第一次需要使用他们的时候去创建他们。有些业务逻辑中,可能在某个边界条件下才需要这种对象或这种执行过程,那么在进行软件设计的时候就要考虑延迟加载技术了。

这项技术在具体实现的时候,通常由一个状态条件去判断该动作项是否会发生。在多线程中,这种标识需要同步。从软件设计的角度来说,延迟加载技术常常和工厂方法组合使用,主要基于以下几点思考:

  1. 采用工厂方法获取类的实例
  2. 把所有用到的实例存储到一个map中,下次使用的时候,可以根据传递的Key讯息来访问对象集合中对应的数据(和单例模式很相似)
  3. 然后就是运用延迟加载技术,在第一次用到的时候创建该对象或动作项。

 下面结合实际工程项目探讨以下该技术的应用场合和相应的方法。


参考:http://en.wikipedia.org/wiki/Lazy_initialization

示例1:一元非线性方程求根算法。

在数值分析时经常会用到求根的算法,由于场景不同,会根据具体情况选择最优的求解算法,有些求解算法在一次执行过程中可能会用到成千上万次,合理的管理算法对象,并对场景分层,在一定程度上可以提高程序的稳定行、执行效率、可维护性。下面结合一些算法简要展示具体用法,设计时候虽然违背了开放和封闭的原则,但是在算法有限的情形下无可厚非,在具体设计的时候,还要注意内存管理的策略。RootFindingAlgorithmDirector 负责算法对象的管理,实际交互最多的对象为具体的算法,可以向下进行转换。

参考代码如下:

#pragma once
//A root-finding algorithm is a numerical method, or algorithm, for finding a value x such that f(x) = 0, 
//for a given function f. Such an x is called a root of the function f.
enum RootFindingAlgorithmType
{
	RFAT_Unknown,
	RFAT_Bisection_Method,
	RFAT_Newton_Method,
	RFAT_Secant_Method
};
class AbstractAlgorithm
{
	typedef double(*Function_Ptr)(double);
public:
	bool getRoot(double rootValue);
	bool setFunction(Function_Ptr equation);
	RootFindingAlgorithmType getAlgorithmTypeInfo();
protected:
	Function_Ptr m_equation;
	RootFindingAlgorithmType typeId;
};
class BisectionMethodAlgorithm:public AbstractAlgorithm
{
public:

};
class NewtonMethodAlgorithm:public AbstractAlgorithm
{
public:
};
class SecantMethodAlgorithm:public AbstractAlgorithm
{
public:
};


class RootFindingAlgorithmDirector
{
public:
	 AbstractAlgorithm* getRootFindingAlgorithm( RootFindingAlgorithmType type);
	~RootFindingAlgorithmDirector();
private:
	 map<RootFindingAlgorithmType,AbstractAlgorithm*> types;
};

实现文件

#include "stdafx.h"
#include "RootFindingAlgorithm.h"

AbstractAlgorithm* RootFindingAlgorithmDirector::getRootFindingAlgorithm( RootFindingAlgorithmType type)
{
	map<RootFindingAlgorithmType,AbstractAlgorithm*>::const_iterator iter= types.find(type);
	AbstractAlgorithm* algorithm = NULL;
	if (iter != types.end())
	{
		return iter->second;
	}
	else
	{
		switch (type)
		{
		case RFAT_Bisection_Method:
			algorithm = new BisectionMethodAlgorithm;
			break;
		case RFAT_Newton_Method:
			algorithm = new NewtonMethodAlgorithm;
			break;
		case RFAT_Secant_Method:
			algorithm = new SecantMethodAlgorithm;
			break;
		default:
			break;
		}
	}
	return algorithm;
}
RootFindingAlgorithmDirector::~RootFindingAlgorithmDirector()
{
	map<RootFindingAlgorithmType,AbstractAlgorithm*>::const_iterator iter= types.begin();
	map<RootFindingAlgorithmType,AbstractAlgorithm*>::const_iterator iter_end= types.end();
	AbstractAlgorithm* algorithm = NULL;
	for (; iter !=iter_end; iter++)
	{
		algorithm = iter->second;
		if (NULL != algorithm)
		{
			delete algorithm;
			algorithm = NULL;
		}
	}
}
以上代码未做单元测试,仅供参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值