Tham khảo tài liệu 'ansi/iso c++ professional programmer's handbook phần 5', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | ANSI ISO C Professional Programmer s Handbook - Chapter 5 - Object-Oriented Programming and Design 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 private FontResthisce fonts public Internationalization virtual int Loadfonts virtual void SetDirectionality class English public Internationalization public English Loadfonts fonts TimesNewRoman SetDirectionality do nothing default left to right file D Cool Stuff old ftp 1 1 ch05 18 von 29 14 46 09 ANSI ISO C Professional Programmer s Handbook - Chapter 5 - Object-Oriented Programming and Design class Hebrew public Internationalization public Hebrew Loadfonts fonts David SetDirectionality directionality right_to_left Derivation simplifies class structure and localizes the changes that are associated with a specific language to its corresponding class without affecting .