`
liuxiang123
  • 浏览: 29661 次
  • 性别: Icon_minigender_1
  • 来自: 四川
社区版块
存档分类
最新评论

js面向对象之继承练习 (模拟java策略模式)

阅读更多

js面向对象之继承学习  模拟策略模式

 

function CacheStrategy(){
	this.getCache = function(CacheService){alert("cacheStrategy getCache function!");};
	this.reloadCache = function(CacheService){alert("cacheStrategy reloadCache function!");};
}

function MemCache(){
	this.getCache = function(CacheService){
		/**
		缓存数据
		**/
		alert(CacheService.getData());
		alert("memCache getCache function!");
	};
	this.reloadCache=function(CacheService){
		alert("memCache reloadCache function!");
	}
}
MemCache.prototype = new CacheStrategy();
function OsCache(){
		this.getCache = function(CacheService){
		/**
		缓存数据
		**/
		alert(CacheService.getData());
		alert("osCache getCache function!");
	};
	this.reloadCache=function(CacheService){
		alert("OsCache reloadCache function!");
	}
}
OsCache.prototype = new CacheStrategy();
function CacheService(){
	this.getCache = function(CacheStrategy){};
	this.getData = function(){alert("cacheService getData function !");};
	this.getCacheKey = function(){};
	this.useCache = function(){};
	this.reloadCache = function(){};
}

function AbstractCache(){
	this.cacheKey = this.autoCacheKey();
	this.useCache = true;
	this.cacheStrategy = new MemCache();
}
AbstractCache.prototype = new CacheService();
AbstractCache.prototype.autoCacheKey = function(){
	return "123";
}
AbstractCache.prototype.setCacheKey = function(_cacheKey){
	this.cacheKey = _cacheKey;
}
AbstractCache.prototype.getCacheKey = function(){
	return this.cacheKey;
}
AbstractCache.prototype.setUseCache = function(_useCache){
	this.useCache = _useCache;
}
AbstractCache.prototype.getUseCache = function(){
	return this.useCache;
}
AbstractCache.prototype.setCacheStrategy = function(_cacheStrategy){
	this.cacheStrategy = _cacheStrategy;
}
AbstractCache.prototype.getCache = function(){
	return this.cacheStrategy.getCache(this);
}
AbstractCache.prototype.reloadCache = function(){
	return this.cacheStrategy.reloadCache(this);
}
function ResourceCache(){
}
ResourceCache.prototype = new AbstractCache();

ResourceCache.prototype.getData = function(){
	return "data:11111111111111111111";	
}

 

html测试页面

 

<html>
<head>
<script type="text/javascript" src="classtest.js" ></script>
<title>js类测试</title>	
</head>
<script>
	var abstractCache = new ResourceCache();
	//abstractCache.setCacheStrategy(new OsCache());
	abstractCache.getCache();
	abstractCache.setCacheKey(456);
	alert(abstractCache.getCacheKey());
</script>
<body>
</body>
</html>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics