3D游戏编程:高级C++技术

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Closed for modification
Source code of such module is inviolate
Two opposing features?
Game Programming II
Solution
Abstraction is the Key !!!
Create fixed abstractions presenting an unbounded group of possible behaviors
Extend modules behavior by adding new code instead of changing old code that already works
Game Programming II
Features
Open for extension
Behavior of module can be extended Make module behave in new and different ways as requirements change or to meet new needs
New requirement: car’s velocity should not be larger than some maximum value
Modify your code everywhere to adapt the new requirement It is really painful, I mean it!
Game Programming II
Take A Closer Look
Conform to open-closed principle
If we want to add a new type of shape
Add a new derivative of class Shape DrawAllShapes() does not need to change
Game Programming II
Take A Closer Look
Work perfectly if there are only two types of shape Are we satisfied? Not conform to open-closed principle
Game Programming II
Example
enum ShapeType {circle, square}; struct Shape { ShapeType itsType; }; struct Circle { ShapeType itsType; double itsRadius; Point itsCenter; }; struct Square { ShapeType itsType; double itsSide; Point itsTopLeft; }; void DrawSquare(struct Square*) void DrawCircle(struct Circle*); typedef struct Shape *ShapePointer; void DrawAllShapes(ShapePointer list[], int n) { int i; for (i=0; i<n; i++) { struct Shape* s = list[i]; switch (s->itsType) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; } } }
Your weapon: code refactor Start with naï ve design but keep refactoring code once open-closed principle is broke
Game Programming II
Member Variables
A well-known OOD rule: all member variables should be declared private, rather than public or protected From the view of open-closed principle
We expect that methods of class are not closed to changes in member variables of that class We do expect that any other class, including subclasses are closed against changes to those variables
Abstractions are abstract base class Unbounded behaviors is represented by derivative classes Handle changes by adding new code without changing existing code
e decide that all Circles should be drawn before any Squares?
Game Programming II
Welcome to Real World
No program can be 100% closed
Game Programming II
Example: Event Log
Process of recording events, with an automated computer program Provide an audit trail that can be used to understand activity of system and to diagnose problems Requirements
Sum i; for (int+= i = 0; i < n; ++i)
{ } return result; } }
}
result = result + array[i];
for (int i = 0; i < n; ++i) { result = result + array[i]; } return result;
It is not closed against new kinds of shape DrawAllShapes() has to be modified for any new type of shape
Game Programming II
Better Solution
class Shape { public: virtual void Draw() const = 0; //pure virtual function }; class Square : public Shape { public: virtual void Draw() const; }; class Circle : public Shape { public: virtual void Draw() const; }; void DrawAllShapes(Set<Shape*>& list) { for (Iterator<Shape*>i(list); i; i++) (*i)->Draw(); }
Game Programming II
How to Handle Change?
All systems change during their life cycles How can we create designs that are stable in face of change? Nightmare: a single change to a program results in a cascade of changes to dependent modules
Game Programming II
Example
Initial requirement: car should have velocity property
class Car { public: double m_Velocity; }; void func1() void func2() { { … … Car MyCar; Car MyCar; MyCar.m_Velocity = 100.0; MyCar.m_Velocity = 200.0; … … } }
No matter how “closed” a module is, there always be some kind of change against which it is not closed Closure must be strategic Experienced designer can make sure that open-closed principle is invoked for the most probable changes What if I am a newbie?
今年中秋节恰逢农历八月十五黄昏的傍晚我和弟弟弟弟也和我我俩站在院子中央抬着头仰脖看那天上的月亮我说
Advanced C++ Programming
Yanci Zhang
Game Programming II
Do You Really Know Coding?
轻轻的我走了,正如我轻轻的来; 我轻轻的招手,作别 今年中秋节,恰逢农历八月十五,黄昏的傍晚,我和 在一个伸不见五指的晚上,池塘的蝌蚪在晒太阳。 西天的云彩。 弟弟,弟弟也和我,我俩站在院子中央,抬着头,仰 我跌了一跤,好痛,觉得很伤心,露出黯然神伤的 脖看那天上的月亮,我说:“看 波光里的艳影,在我 哪,这边的月亮升起 那河畔的金柳,是夕阳中的新娘; 神情。 来了!”弟弟说:“看哪,那边的月亮也升起来了!” 的心头荡漾。 老师有一张爪子脸。 这时,在院子里的全家人,也包括爸爸、妈妈、哥哥、 软泥上的青荇,油油的在水底招摇; 姐姐、弟弟、妹妹几乎全都异口同 在康河的柔波里, 声地一起喊道: 我甘心做一条水草! for (int i=0; i<100; i++) “看哪,东边的月亮真的升起来了! ” { int sum(int* array, int n) float sum(float* array, int n) 那榆荫下的一潭,不是清泉, 是天上虹揉碎在浮藻间, { int Sum = 0; { 沉淀着彩虹似的梦。 int result = 0; float result = 0;
Multiple levels: fatal error, normal error, information… May have multiple output targets: screen, file… warning,
Game Programming II
Initial Solution
Bad design, fragile, rigid, unpredictable, unreusable
Game Programming II
Open-Closed Principle
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification You should design modules that never change!!! When requirements change
相关文档
最新文档