Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (84.42 KB, 28 trang )
{
private:
string filename;
//
public:
int open() {/* */}
int close () {/* */}
};
Use Derivation Instead of Type-Fields
Suppose that you have to implement an internationalization helper class that manages the necessary parameters of
every natural language that is currently supported by a word processor. A naive implementation might rely on
type-fields to indicate the specific language that is currently being used (for example, the interface language in
which menus are displayed).
class Fonts {/* */};
class Internationalization
{
private:
Lang lg; //type field
FontResthisce fonts
public:
enum Lang {English, Hebrew, Danish}
Internationalization(Lang lang) : lg(lang) {};
Loadfonts(Lang lang);
};
Every modification in Internationalization affects all its users, even when they are not supposed to be
affected. When adding support for a new language, the users of the already-supported languages have to
recompile (or download, which is worse) the new version of the class. Moreover, as time goes by and support for
new languages is added, the class becomes bigger and more difficult to maintain, and it tends to contain more
bugs. A much better design approach is to use derivation instead of type-fields. For example
class Internationalization //now a base class
{