100 likes | 200 Vues
CTEC2902 Advanced Programming. Designing Your Own Classes. Designing Your Own Classes. The story so far… You know how to Use ready-made classes in your code Write class code So, you can now start designing your own Let us carry on… . Designing Any Class - Four Steps.
E N D
CTEC2902Advanced Programming Designing Your Own Classes Designing Classes
Designing Your Own Classes • The story so far… • You know how to • Use ready-made classes in your code • Write class code • So, you can now start designing your own • Let us carry on… Designing Classes
Designing Any Class - Four Steps Service (orFunctionality Requirement) Document A summary of the service to be provided by the class. (0 or more properties & methods - How long is a piece of string?) Namespace: Class name: Inherits: Designing Classes
Designing Your Own Classes • Property or Method? • If simple access to data is required, • with simple validation/formatting, use property • If complex processing is required, use method • Use the Functionality Requirement Document • Nouns indicate properties • Verbs indicate methods Sometimes, either will do. Go with personal preference Designing Classes
Designing BankAccount Class Service Manages different types of bank accounts and keeps track of the current balance. The account can be “Black”, “Orange”, or “Red”, depending on the balance. Positive balance earns interest. Namespace: nsBankOps Class name: clsAccount Inherits: none Designing Classes
Designing AcademicModule Class Service Manages an academic module. Each module is managed by its leader; is 15 or 30 credits; and is run at a specific level of study. Assessment is by coursework and examination. Namespace: nsDMU Class name: clsModule Inherits: None Designing Classes
Designing Encyclopaedia Class Service Manages an encyclopaedia. The user submits a word and its explanation is returned. The user may add new words or remove existing words. Namespace: nsEnglish Class name: clsEncyclopedia Inherits: None Designing Classes
Exercise Design a class to represent a student Namespace: nsDMU Class name: clsStudent Inherits: None Exercise Design a class to represent a city Namespace: nsUK Class name: clsCity Inherits: None Designing Classes
Designing Your Own Classes • Summary • Your design must end with the class API, specifying • Service provided by the class • Properties – name, type, data limits, etc. • Methods – name, type, parameters, functionality • Constructors – parameters, functionality Designing Classes
Designing Your Own Classes • Warning and Advice • Designing a class is difficult • Implementing a class is easy • So, spend time on the design; • try out “what if” scenarios Designing Classes