原型模式替代基类的静态函数违背ocr原则去dynamic_cast分支而拷贝对象的法子
2013-08-05 11:11
239 查看
原型模式取代 基类的静态函数违背ocr原则去dynamic_cast分支拷贝对象
假设这么一个需求, 有一个容器要存储很多类的指针;容器可以拷贝。
见过一个人写的代码:
基类采用了静态函数来完成 派生类的克隆。
伪代码如下:
//Sample.h
class Derived1;
class Derived2;
class Sample
{
protected:
enum eType{ type1 ,type2 };
int m_nType;
protected:
Sample(void);
virtual ~Sample(void);
Sample(const Sample& rhs);
Sample& operator = (const Sample& rhs);
protected:
virtual int GetType() const =0 ;
public:
static Sample* Clone(Sample const& rhs);
};
class Derived1: public Sample
{
public:
Derived1():Sample()
{
m_nType = type1;
}
public:
virtual int GetType() const
{
return m_nType;
}
};
class Derived2: public Sample
{
public:
Derived2():Sample()
{
m_nType = type2;
}
private:
virtual int GetType() const
{
return m_nType;
}
};
//Sample.cpp
Sample::Sample(void)
{
}
Sample::~Sample(void)
{
}
Sample* Sample::Clone( const Sample& rhs )
{
Sample const * prhs = & rhs;
Sample * phrs2 = const_cast< Sample* > (prhs);
if( phrs2 == NULL)
return NULL;
switch( prhs->GetType() )
{
case type1:
{
Derived1* pD1 = NULL;
pD1 = dynamic_cast<Derived1*> (phrs2);
return pD1;
}
break;
case type2:
{
Derived2* pD1 = NULL;
pD1 = dynamic_cast<Derived2*> (phrs2);
return pD1;
}
default:
return NULL;
}
}
违背ocr原则, 显得别扭。 因为static函数没有指针,所以只能够通过参数传进来一个待拷贝的对象.
这个问题用原型模式解决.
//Sample.h
class Derived1;
class Derived2;
class Sample
{
protected:
enum eType{ type1 ,type2 };
int m_nType;
protected:
Sample(void);
virtual ~Sample(void);
Sample(const Sample& rhs);
Sample& operator = (const Sample& rhs);
public:
virtual int GetType() const = 0;
virtual Sample* Clone( ) const = 0 ; //新版本
};
class Derived1: public Sample
{
public:
Derived1():Sample()
{
m_nType = type1;
}
Derived1& operator = (const Derived1& rhs);
public:
inline virtual int GetType() const
{
return m_nType;
}
virtual Sample* Clone( ) const ;
};
class Derived2: public Sample
{
public:
Derived2():Sample()
{
m_nType = type2;
}
Derived1& operator = (const Derived1& rhs);
public:
inline virtual int GetType() const
{
return m_nType;
}
virtual Sample* Clone( ) const ;
};
//Sample.cpp
Sample::Sample(void)
{
}
Sample::~Sample(void)
{
}
Sample::Sample(const Sample& rhs)
{
}
Sample* Derived1::Clone( ) const //新版本
{
return new Derived1(*this);
}
Sample* Derived2::Clone( ) const //新版本
{
return new Derived2(*this);
}
原型模式好的文章:原型模式
再附一文章,是: 虚拟拷贝技术 (这里的虚拷贝函数采用的技术依然是原型模式)
假设这么一个需求, 有一个容器要存储很多类的指针;容器可以拷贝。
见过一个人写的代码:
基类采用了静态函数来完成 派生类的克隆。
伪代码如下:
//Sample.h
class Derived1;
class Derived2;
class Sample
{
protected:
enum eType{ type1 ,type2 };
int m_nType;
protected:
Sample(void);
virtual ~Sample(void);
Sample(const Sample& rhs);
Sample& operator = (const Sample& rhs);
protected:
virtual int GetType() const =0 ;
public:
static Sample* Clone(Sample const& rhs);
};
class Derived1: public Sample
{
public:
Derived1():Sample()
{
m_nType = type1;
}
public:
virtual int GetType() const
{
return m_nType;
}
};
class Derived2: public Sample
{
public:
Derived2():Sample()
{
m_nType = type2;
}
private:
virtual int GetType() const
{
return m_nType;
}
};
//Sample.cpp
Sample::Sample(void)
{
}
Sample::~Sample(void)
{
}
Sample* Sample::Clone( const Sample& rhs )
{
Sample const * prhs = & rhs;
Sample * phrs2 = const_cast< Sample* > (prhs);
if( phrs2 == NULL)
return NULL;
switch( prhs->GetType() )
{
case type1:
{
Derived1* pD1 = NULL;
pD1 = dynamic_cast<Derived1*> (phrs2);
return pD1;
}
break;
case type2:
{
Derived2* pD1 = NULL;
pD1 = dynamic_cast<Derived2*> (phrs2);
return pD1;
}
default:
return NULL;
}
}
违背ocr原则, 显得别扭。 因为static函数没有指针,所以只能够通过参数传进来一个待拷贝的对象.
这个问题用原型模式解决.
//Sample.h
class Derived1;
class Derived2;
class Sample
{
protected:
enum eType{ type1 ,type2 };
int m_nType;
protected:
Sample(void);
virtual ~Sample(void);
Sample(const Sample& rhs);
Sample& operator = (const Sample& rhs);
public:
virtual int GetType() const = 0;
virtual Sample* Clone( ) const = 0 ; //新版本
};
class Derived1: public Sample
{
public:
Derived1():Sample()
{
m_nType = type1;
}
Derived1& operator = (const Derived1& rhs);
public:
inline virtual int GetType() const
{
return m_nType;
}
virtual Sample* Clone( ) const ;
};
class Derived2: public Sample
{
public:
Derived2():Sample()
{
m_nType = type2;
}
Derived1& operator = (const Derived1& rhs);
public:
inline virtual int GetType() const
{
return m_nType;
}
virtual Sample* Clone( ) const ;
};
//Sample.cpp
Sample::Sample(void)
{
}
Sample::~Sample(void)
{
}
Sample::Sample(const Sample& rhs)
{
}
Sample* Derived1::Clone( ) const //新版本
{
return new Derived1(*this);
}
Sample* Derived2::Clone( ) const //新版本
{
return new Derived2(*this);
}
原型模式好的文章:原型模式
再附一文章,是: 虚拟拷贝技术 (这里的虚拷贝函数采用的技术依然是原型模式)
相关文章推荐
- 创建对象_原型(Prototype)模式_深拷贝
- 设计模式(Design Patterns)-可复用面向对象软件的基础 05:原型模式(Prototype)和Java中的深、浅拷贝
- 分析一下 原型模式的 UML 类图 。 复制对象, 深浅拷贝 月经贴 ,请回避
- 原型模式-对象拷贝[转]
- 原型模式 -- 拷贝当前对象的副本
- Java设计模式(四):原型模式深拷贝的两种实现方式,以及和new对象的性能测试对比
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝
- 设计模式——创建型模式之借助原型模式(Prototype Pattern)创建保护性拷贝对象(四)
- 面向对象设计模式与原则
- 面向对象设计原则及设计模式概要
- 关于js中构造函数与原型对象相结合的模式的疑惑与想法
- 对象的克隆——原型模式(三):浅克隆,深克隆
- Javascript之创建对象(原型模式)
- “我要金手指”——由模式谈对象对象的基本原则之依赖颠倒原则
- JS中使用动态原型模式、寄生构造函数模式、稳妥构造函数模式创建对象
- 1 面向对象设计模式与原则
- 面向对象设计模式与原则0
- 对象的克隆——原型模式(四)
- 原型设计模式prototype-构造js自定义对象
- C#面向对象设计模式纵横谈(6):Prototype 原型(创建型模式)