300 likes | 1.18k Views
Programming Paradigms. Different paradigms Procedural paradigm, e.g. Pascal Basic Functional paradigm, e.g. Lisp Declarative paradigm, e.g. Prolog Object-Oriented paradigm, e.g. C++, Java Main advantage of object-oriented programming (OOP) is reuse. Object-Oriented Design (OOD).
E N D
Programming Paradigms • Different paradigms • Procedural paradigm, e.g. Pascal Basic • Functional paradigm, e.g. Lisp • Declarative paradigm, e.g. Prolog • Object-Oriented paradigm, e.g. C++, Java • Main advantage of object-oriented programming (OOP) is reuse.
Object-Oriented Design (OOD) • The first step of object-oriented programming (OOP) is OOD. • OOD will be written in UML (Unified Modeling Language) • Writing an object-oriented program: • Construct an OOD • Convert the OOD to a particular programming language
OOD Concepts • Objects • classes • instances of classes • Association • Inheritance • superclasses • subclasses
What is an object? • OOP involves defining a number of objects. • Objects consist of attributes and operations that can be applied to the data. • The attributes of an object can only be accessed via these operations. • The operations are used to access and update/change the data. • Objects are transient, i.e. the data and operations of an object only remain in memory for the duration the program is run.
student Object • Attributes • Name • Student number • Address • Matric points • Degree • Operations • Set name, student number, address, matric points or degree • Get name, student number, address, matric points or degree • Print student information
customer Object • Attributes • Name • Address • Phone • Balance • Operations • Set name, address, phone or balance • Get name, address, phone or balance • Update balance • Print customer details
Classes and Instances • A class defines the overall internal structure, i.e. attributes and operations, of the object. • Class definition • Attributes called data elements • Operations called methods • An instance of a class which has specific values for the different data elements. • One of the data elements my be used to identify each element uniquely, e.g. a student number.
Instance of the student class • Name: B. Green • Student Number: 203456789 • Address: WOB Residence • Matric Points: 45 • Degree: B.Sc
Encapsulation • Classes are said to exhibit “information hiding” due to the fact that the internal structure of the class is hidden. • The data elements of an instance of the class is manipulated be means of an interface, e.g. the Formatter and Keyboard classes. • This is referred to as encapsulation.
Class Diagrams • In UML class diagrams are used to represent classes and relationships between classes. • Each class is represented as a box divided into three parts specifying the: • Name of the class • Data elements of the class • Attributes of the class
The student class Class name Data elements Methods
Example 1 • Define the classes that you would use for the following system. Specify the data elements and methods of each class. You need to develop a system that keeps track of customers accounts. Information stored for each customer is the customer name, address and phone number. A customer may place one or more orders. Each order has an order number, date and amount and a description of the product to be shipped. Each product has a unique product number, description and price.
Example 2 • Define the classes that you would use for the following system. Specify the data elements and methods of each class. You are required to develop a GUI for an existing application. The main component of the GUI is a form. The form has a width, height, boarder style and background color. The user must be able to display or close a form. The user must also be able to minimize and maximize the form. The form contains labels and buttons. Labels have a size, shape, color and text. The programmer must be able to set the text, get the text, hide the label or display the label. Each button has a size, shape, colour, location and caption. The user should be able to click the button. A programmer must be able to hide, display, enable or disable the button.