Introduction to the Object-Oriented Approach ppsx

24 340 1
  • Loading ...
    Loading ...
    Loading ...

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Tài liệu liên quan

Thông tin tài liệu

Ngày đăng: 01/08/2014, 09:21

Introduction to the Object-Oriented Approach Chapter 1 Object-Oriented Programming (OOP) is one of the most popular methodologies in software development. It offers a powerful model for creating computer programs. It speeds the program development process, improves maintenance, and enhances reusability of programs. This chapter introduces object-oriented methodology and discusses the concepts of objects, classes, messages, and methods. It provides an overview of the phases involved in object-oriented analysis and design. This chapter also explains how to define classes in C#. In this chapter, you will learn to:  Explain features of the object-oriented methodology  Describe the phases of the object-oriented methodology  Define classes in C# Objectives Introduction to the Object-Oriented Approach 1.3 ¤NIIT Object-orientation is a software development methodology that is based on modeling a real-world system. An object is the core concept involved in object orientation. An object is the representation of a real-word entity or concept. For example, an employee, a window, a car, or a bird can be modeled as objects. You can think of an object-oriented model as a collection of objects and their inter-relationships. Object-orientation is a type of methodology used for building software applications. An object-oriented program consists of classes, object, and methods. If you were asked to create a classroom, you will start by creating a room and defining its boundaries and features. Next, you will put together all the components, such as chairs, tables, and books. Similarly, if you want to create an aircraft or a high-rise building, you can do so by putting together several components or parts. These components or parts are analogous to objects in the object-oriented methodology. The object-oriented methodology in software development revolves around a single concept called the object. Software is developed by breaking the application into component objects. These objects interact with each other when the whole application is put together. An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object. Objects An object literally means a ‘material thing’ that is capable of being presented to the senses. For our purpose, an object is a tangible entity that may exhibit some well-defined behavior. For example, let us consider a tennis ball:  A tennis ball is a tangible entity, with a visible boundary.  A tennis ball has a specific defined purpose (such as bouncing).  You can direct a specific action towards a tennis ball by hitting it with a racquet or by tossing it. But the definition of an object is not limited to merely something that can be seen, held, and touched, such as a tennis ball or a car. For the purpose of software development, the definition of an object needs refinement. For example, consider the Acme Nut and Bolt Company. An organization does not have a visible boundary, unlike a tennis ball. While it Object-Oriented Methodology The Foundation of Object-Orientation 1.4 Introduction to the Object-Oriented Approach ¤NIIT does not possess a physical boundary, it does have a conceptual boundary. Like all organizations, it has a specific defined purpose, and one can direct a specific action towards it. Thus, the Acme Nut and Bolt Company is an object. State, Behavior, and Identity of an Object According to Grady Booch, a renowned software architect, an object has the following characteristics:  It has a state.  It may display behavior.  It has a unique identity. The state of an object is indicated by a set of attributes and their values. For example, a chemical can be characterized by its temperature, color, and density. The behavior of a chemical refers to the change of its attributes over a period of time. A chemical also has state, such as solid, liquid, or gas. The preceding paragraph explains the first two points of the definition of an object by Grady Booch “An object has state, exhibits some well-defined behavior…”. Now, you will examine the next part of the definition - “…and has a unique identity.” Each object has a unique identity, and the identity of an object distinguishes the object from all other objects. For example, you can take a car as an object. It can have states such as moving and stationary. It can accelerate, decelerate, turn right, or turn left, which is its behavior. The car also has an identity, such as a unique registration number. The following figure depicts the characteristics of a car. An Object Has State, Behavior, and Identity Two objects may have the same behavior and state, but they will never have the same identity. The identity of an object does not change in its lifetime. For example, two tennis balls may have the same color, be made of the same material, have the same weight and the same circumference, and display the same behavior. However, they will have distinct State: Stationary Behavior: Accelerate Identity: Registration Number (XY245) Introduction to the Object-Oriented Approach 1.5 ¤NIIT Just a minute: identities (for example, one ball will have the factory seal number ‘A189735’ and the other ‘S660794’). Identify the possible states of the following objects: 1. A cell phone 2. A stereo Answer: 1. States of a cell phone: Off, Ring, Vibrate, and Call 2. States of a stereo: Play, Pause, Rewind, and Forward Classes Look at the world around you. It is full of objects of various shapes, sizes, colors, and behavior. For example, the earth is inhabited by millions of animals. A zoologist cannot do any meaningful study of these objects without organizing this vast variety of animals found in the world in a logical manner. Thus, the zoologists have classified animals into kingdoms, genus, families, and species. All animals and birds, which are essentially objects, can be classified on the basis of their common attributes. For example, the peacock, the sparrow, and the kingfisher are all birds. All of them share characteristics that are common to the family of birds. All of them lay eggs, are covered with feathers, have hollow bone structures, and have the ability to fly. Therefore, they share structural and behavioral similarities and belong to the class called Birds. 1.6 Introduction to the Object-Oriented Approach ¤NIIT This is shown in the following figure. The Class - Birds From the preceding example, you may define a class as a declaration, a template, or a blueprint that can be used to classify objects. Similarly, Book is a class and ‘Gone with the Wind’ and ‘Farewell to Arms’ are the objects of this class. Consider a video game that involves two players, Tom and Mark, who fight with each other. Tom chooses a shotgun and Mark chooses a pistol as their weapons in the game. If you need to create an object model for this game, you can identify a total of four objects. They are Tom, Mark, the shotgun, and the pistol. The object shotgun belongs to the class Firearms. It has certain attributes that are common with the pistol. However a shotgun is a unique object. Both the shotgun and the pistol are examples or instances of Firearms. Therefore, the shotgun and pistol are objects of the same class, as shown in the following figure. Classes and Objects Messages and Methods Objects do not exist in isolation. They interact with other objects. These interactions take place through messages. Grady Booch has defined behavior as follows: “Behavior is how an object acts and reacts, in terms of its state changes and message passing.” In the example of a video game, every time Tom attacks, Mark either attacks or defends himself. Class: Firearms Object: Pistol Object: Shotgun Birds Peacock KingfisherSparrow Introduction to the Object-Oriented Approach 1.7 ¤NIIT Just a minute: When Tom attacks Mark, Mark receives a message and reacts to that message. The reaction can be exhibiting a particular behavior such as running, hiding, dodging, or fighting back. For example, when Tom attacks, Mark receives a message “Being Attacked”. Mark responds to this message by hiding. In this situation, hiding is a behavior exhibited by Mark. Therefore, behavior (also called method in object-oriented methodology) is simply a set of actions taken by the receiving object in response to a message. Dr. James and Mr. Hyde went to the railway station to book tickets for 3rd December. At the railway station, they requested the clerk at the ticket counter to book two tickets for the Flying Express in the first class. Identify the following: 1. The possible receiver of the message in this situation. 2. The possible method that the receiver can use. Answer: 1. The receiver of the message in this case will be the clerk at the ticket counter. 2. The clerk will check if two tickets are available on the requested train in the desired class and for the desired date. If the tickets are available, the clerk will enter the details (name, age, departure date, and seat), confirm the reservation, and collect the required fare. 1.8 Introduction to the Object-Oriented Approach ¤NIIT As discussed earlier, an object has a state, behavior, and identity. An object is reusable. Therefore, an object exists as a stand-alone entity that can be used in any relevant context. For example, if you have a chair, you can use the same chair in an office, in a classroom, or in a garden. An existing object can be used to create a similar object. For example, you have a simple wooden chair and you need to add a headrest to it. You do not need to create a new chair because you can easily add a headrest to the existing chair. This is known as resilience to change. Realistic Modeling Because you live in a world of objects, it logically follows that the object-oriented approach models the real world more accurately. The object-oriented approach allows you to identify entities as objects having attributes and behavior. Attributes and behavior typically depict how the object acts and reacts. For example, the car is an object belonging to the class Vehicle. The car has attributes such as speed, color, and power. It displays behavior such as being stationary, moving slowly, or accelerating. Reusability In the software industry, using existing classes or objects from other applications saves resources spent in recreating the classes from scratch. Consider the following analogy: Carcare is a leading car manufacturing company. Carcare manufactures two-seater cars that are very popular among the consumers aged between 20 and 24. The company has decided to enter the market of family segment market and has decided to manufacture a four-seater model. Carcare can either design a new car for the family segment or convert the two-seater model to a four-seater model. The cost involved in designing a new model is very high as compared to the cost involved in converting the two-seater model to a four-seater model. Characteristics of the Object-Oriented Approach Introduction to the Object-Oriented Approach 1.9 ¤NIIT Just a minute: Therefore, the management and the design team may decide to convert the two-seater model into a family segment model, as shown in the following figure. Example of Reusability The preceding scenario depicts the concept of reusability that is supported by the object-oriented approach. The process of creating a new class by adding some features to an existing class is known as inheritance. The benefit of reusability translates to savings in time and effort, which in turn results in cost benefits. State whether the following situations demonstrate reusability: 1. Recycling paper 2. Pump reusability (same pump is used in a well and in a fuel station) Answer: 1. IT does not represent reusability because the unusable paper is destroyed before paper is recycled for use. The unusable paper loses its identity and cannot be considered the same as recycled paper. 2. It represents reusability because a pump can be used for suction of water as well as petrol. It is not necessary to use the same pump in both the cases. Two separate machines can be used because both belong to the ‘Pump’ class. Two-seater Car Attributes Behavior Four-seater Car Two-seater Car : Attributes Two-seater Car: Behavior New Attributes New Behavior 1.10 Introduction to the Object-Oriented Approach ¤NIIT Resilience to Change The object-oriented approach allows systems to evolve. When a change is suggested, the old system need not be completely abandoned. Consider the example of JoyToys, Inc., is a company that manufactures toys for children in the age group of 1 to 12. Its car toys are popular with children because of their attractive colors, shape, and sound. For a couple of years, the company had no cause for complain about the design of the toy car. However, due to the advancement in technology and increasing competition, the designers now want to stop manufacturing the old car because the market requirements have changed. They want the company to manufacture a car that has flashing lights and is remotely controlled. In the object-oriented system, this requirement does not mean that the new car needs to be built from scratch. The new features can be easily incorporated in the old toy car without modifying the color, shape, and sound of the old toy car, as shown in the following figure. Illustration of Resilience to Change Resilience to change also results in easier maintenance. This feature of object-oriented methodology is known as extensibility. The ability of a class to inherit features from another class also makes object-oriented programs more extensible. For the same reason, even during construction, parts of the system under development can be refined without any major changes to other parts. Existence as Different Forms Using the object-oriented approach, objects can be made to respond differently to the same message. The response is decided based on the information or parameters provided with the message. Old Toy Car Color Shape Sound New Toy Car Color Shape Sound Remote Control Flash Lights [...]... reality.”- Mellor The analysis or the Object-Oriented Analysis (OOA) phase considers the system as a solution to a problem in its environment or domain Broadly, analysis is the phase where users and developers of the system get together and arrive at a common understanding of 1.12 Introduction to the Object-Oriented Approach NIIT the system One of the end products of the analysis phase is the specification... assigned the task of creating a software module that accepts and displays customer details such as name, age, and phone number Identify the class that you will create and the methods of the class Answer: As per the problem statement, the class required is: Customer The class should have the methods to: Accept customer details Display customer details 1.14 Introduction to the Object-Oriented Approach. .. refer to the subsequent chapters If a car collides against an object, the behavior of the car after the collision will depend on the speed of the car and nature of the object that hit the car For example, if the car collides with another car at high speed, both the cars will be smashed and the drivers might get injured On the other hand, if a car collides with a street light at a slow speed, the impact... design phase, the developers of the system document their understanding of the system Design generates the blueprint of the system that is to be implemented The first step in creating an Object-Oriented Design (OOD) is the identification of classes and their inter-relationships Work in the design phase of software development is comparable to the work of an architect The architect will need to create a... ability to react differently based on the information associated with the message is known as polymorphism NIIT Introduction to the Object-Oriented Approach 1.11 Phases of Object-Oriented Methodology Before the actual development of any product, important steps such as analysis and design need to be performed The following phases are involved in the object-oriented methodology: The Analysis phase The Design... Console.WriteLine() if the statement using System is included as the first line of code The following code is an example of Console.WriteLine(): Console.WriteLine("Hello World \n"); The preceding code will display on the screen Hello World 1.18 Introduction to the Object-Oriented Approach NIIT The Escape Characters To display special characters such as the New line character or the backspace character, you need to use... behavior together Reusability Resilience to change Existence as different forms A model of a system is built in the stages of analysis and design The purpose of the model is to help developers understand the reality that they are trying to imitate In C#, a class is created by using the keyword class It is identified by a name called the class name The Console.WriteLine() method is used to display text on the. .. special meaning Here, the class keyword defines the class Hello The braces, known as delimiters, are used to indicate the start and end of a class body Example: class Hello { } The Class Name The class keyword is followed by the name of the class In the preceding example, Hello is the name of the class defined by using the class keyword When you create classes, you must consider the following naming... which is the first function to be executed? a Main() b main() c Console.WriteLine() d void Accept_bike_details() 5 Which of the following is used to denote a newline character? a \b b \n c \v d /n NIIT Introduction to the Object-Oriented Approach 1.21 Summary In this chapter, you learned that: According to the object-oriented approach, systems consist of component objects that interact with each other... that belongs to the System namespace A namespace is a collection of classes The System namespace contains the method WriteLine(), which displays the enclosed text on the screen The Console class has other methods, which are used for various input/output operations The character (.) is used to access the function, WriteLine(), which is coded in the Console class of the System namespace The preceding . per the problem statement, the class required is:  Customer The class should have the methods to:  Accept customer details  Display customer details Introduction to the Object-Oriented Approach. two-seater model to a four-seater model. Characteristics of the Object-Oriented Approach Introduction to the Object-Oriented Approach 1.9 ¤NIIT Just a minute: Therefore, the management and the design. New Behavior 1.10 Introduction to the Object-Oriented Approach ¤NIIT Resilience to Change The object-oriented approach allows systems to evolve. When a change is suggested, the old system need
- Xem thêm -

Xem thêm: Introduction to the Object-Oriented Approach ppsx, Introduction to the Object-Oriented Approach ppsx, Introduction to the Object-Oriented Approach ppsx

Từ khóa liên quan