面向对象的设计与开发

每个类的定义都以关键字类开头,后面跟着类名,后面跟着一对花括号,里面包含有类的属性与方法的定义。 类名可以是任何非PHP保留字的合法标签。一个合法类名以字母或下划线开头,后面跟着若干字母,数字或下划线。以正则表达式表示为:[a-zA-Z_ \ x7f- \ xff] [A-ZA-Z0-9_\ x7f-\ XFF] *。一个类可以包含有属于自己的常量,变量(称为“属性”)以及函数(称为“方法”)。
展开查看详情

1.Object-Oriented Design and Development Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-255 Storrs, CT 06269-2155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 Copyright © 2000 by S. Demurjian, Storrs, CT.

2.Abstraction

3.Encapsulation

4.What is a Class?

5.Object-Oriented Decomposition Decompose Problem into Agents which Perform Operations Emphasize Agents that Cause Actions Agents Comprised of Two Parts Hidden Implementation : Data and Operations only Available to Agent Public Interface : Operations Available to Clients of Agent An Agent Can Only be Modified by Operations Defined in either the Hidden Implementation or Public Interface Object-Oriented Paradigm

6.Class The Type of an Agent Describes the Behavior Object The Instance of a Class Represents Actual Data Manipulated by Agents Maintains the State of Object Method Operation Defined on a Class Operates on ALL Instances of the Class Message Indicates that an Object’s Method Invoked Core Object-Oriented Concepts

7.What is a Class ? class name attributes: operations: external entities things occurrences roles organizational units places structures A class is a generalized description of a collection of similar objects. Object is an instance of a class. Class exports: -Operations used to manipulate instances -May export attributes.

8.Two Representations of a Class

9.Operations (or Methods or Services) Defined on a Class An executable procedure that is a part of a class and operates on the data attributes defined as part of the class A method operates on all instances of a class A method is invoked via message passing

10.Messages Messages - the means by which objects invoke each other’s methods.

11.An Example Employee Class Class Employee { } //Hidden Implementation Private: //Instance Vars char[30] name; float salary; //Public Interface Public: void print_name(); void print_salary(); void update_salary(float i); Employee(char *n, float s); Main() { //Declare Objects Employee emp1(Steve,100.0); Employee emp2(Lois, 120.0); //Pass Messages //Invoke Methods emp1.print_name(); emp1.print_salary(); emp2.update_salary(10); emp2.print_name(); emp2.print_salary(); } What’s Output of Main()? Steve 100.0 Lois 130.0 Conclusion: Each Object (emp1,emp2) has Own Independent State that is Accessible via Shared Public Interface of Class

12.Background and Motivation Transitional Design & Silver Bullet (Brooks) Law of Demeter/Responsibility-Driven Design Object-Oriented Design Issues The High-Tech Supermarket System (HTSS) Choosing Objects and Classes Inheritance and Overloading Polymorphism/Dynamic Binding Generic: A Type Parameterizable Class “Software Design,” and “Object-Oriented Design”, Chapter 108 and 109, The Computer Science & Engineering Handbook , Tucker (ed.), CRC Press, 2 nd Edition, 2002. Object-Oriented Concepts

13.Objects vs. ADTs Objects Extend ADTs as follows: Message Passing: Object Activation by a Request (Method Call) to Perform an Action Inheritance: Sharing of Code Among Objects Polymorphism: Shared Code Behaves Differently Based on Object and Context ADT/OO Benefits : Support Creation of Reusable Software Components Creation and Testing in Isolation! SEs View Problem at Higher-Level of Abstraction

14.Objects vs. ADTs Objects Extend ADTs as follows: Message Passing: Object Activation by a Request (Method Call) to Perform an Action Inheritance: Sharing of Code Among Objects Polymorphism: Shared Code Behaves Differently Based on Object and Context ADT/OO Benefits : Support Creation of Reusable Software Components Creation and Testing in Isolation! SEs View Problem at Higher-Level of Abstraction

15.Choosing Objects and Classes Employee class Private data: Name Address SSN Salary Public interface: Create_employee () Give_Raise_Amount (Amount) Change_Address ( New_Addr ) Based on an information perspective, focusing on the idea that to track Employees a set of standard data and operations are needed

16.Choosing Objects and Classes ATM_log class: Private data: Acct_name PIN_number Public interface: Check_database (Name) Verify_PIN (PIN) Log_on_Actions ( Name,PIN ) Reject() Embodies the functions that take place to authenticate an individual to an ATM session Even with a functional view, information is needed to capture user input for verifying status

17.Choosing Objects and Classes ATM_User: Private data: Action Balance WD_Amt Public interface: Log_on_Steps() Acct_WithD() Check_Balance(Number) Deposit_Check() User interface by capturing the different interactions between the ATM and the user

18.Choosing Objects and Classes Start from the problem specification Objects include physical entities as well as concepts such as seating arrangement, payment schedule Not all classes maybe explicit in the problem statement and may rely on domain knowledge for identification Objects frequently correspond to nouns in the statement

19.Choosing Objects and Classes An appointments system that will allow telephone callers to book an appointment with a doctor. The caller will specify the day and the time when he wishes to be seen by a doctor. Tentative classes could be: Appointment, system, telephone, caller, doctor, day, time Patient - from domain knowledge Caller and patient express the same information but one is more meaningful in the context.

20.Choosing Objects and Classes Identifying operations: Attributes Events in the scenarios Real-world behavior Attributes: Need operations to set and read attributes Events in the scenarios: A scenario consists of interactions (events exchanged) that have to take place among the objects to achieve the functionality. Identify common and rare scenarios. Events passed to and from the objects implies operation on the object or message from it.

21.Choosing Objects and Classes Real world can also suggest the operations needed to support a class : Useful in broadening the scope of the class beyond its immediate application. Operations should not overlap each other: Use simple operations to create more complex activities e.g an edit operation could consist of copy, delete and insert. Number of operations that have access to the data should be reduced to a minimum. Operations may refer to verbs in the problem description

22.Inheritance – Example #1 A vehicle registration system maintains registration information about all types of vehicles owned by individuals. These include the ones that run on land, sail in water and fly in the air. The vehicles that run on the land include cars and trucks, the vehicles that sail in the water include sailboats, ships, motorboats and yatchs, and the vehicles that fly in the air include airplane and helicopters. Construct an inheritance hierarchy for the vehicle registration system.

23.Inheritance – Example #2 Consider a shipping cost calculation system that calculates shipping costs for a batch of packages. Packages can be shipped via three methods, namely, US Mail, FedEx or UPS. For each of these three methods, the customer has a choice of using either standard or priority shipping. The calculation of the shipping costs differs depending on which method is used. Construct an inheritance hierarchy for the shipping cost system.

24.Inheritance – Example #2 Consider a shipping cost calculation system that calculates shipping costs for a batch of packages. Packages can be shipped via three methods, namely, US Mail, FedEx or UPS. For each of these three methods, the customer has a choice of using either standard or priority shipping. The calculation of the shipping costs differs depending on which method is used. Construct an inheritance hierarchy for the shipping cost system.

25.The High-Tech Supermarket System (HTSS) Description of Capabilities Modules and ADTs for HTSS Categories of Classes Common Design Flaws Object-Oriented Design Issues

26.High-Tech Supermarket System (HTSS) Automate the Functions and Actions Cashiers and Inventory Updates User Friendly Grocery Item Locator Fast-Track Deli Orderer Inventory Control User System Interfaces Cash Register/UPC Scanner GUI for Inventory Control Shopper Interfaces Locator and Orderer Deli Interface for Deli Workers We’ll Introduce and Utilize Throughout Course

27.The HTSS Software Architecture IC IC CR CR CR CR IL IL IL SDO SDO EDO EDO Order Payment Item ItemDB Local Server Non-Local Client Int. Inventory Control ItemDB Global Server OrderDB SupplierDB CreditCardDB ATM-BanKDB IL: Item Locator CR: Cash Register IC: Invent. Control DO: Deli Orderer for Shopper/Employee

28.Categories of Classes Data Managers - Maintain Data/State Information Contains Functionality for Application class Item { private: // Private Data int UPC; char* Name; int InStock, OnShelf, ROLimit; float RetailCost; public: // Public Methods Item(int code, char* str, int st1, int st2, int st3, float cost); void CreateNewItem(); int GetUPC(); char* GetName(); int GetQuantity(); int CheckReorderStatus(); void PrintItem(); void UpdatePrice(float new_value); };

29.Categories of Classes Data Sinks/Data Sources - Produce/Process Data Generated on Demand or Maintained class ItemDB {private: int Num_Items; int Curr_Item; Item* AllItems[Max_Items]; int FindFirstItem(); int FindNextItem(); int FindItemUPC(int code); int FindItemName(char* name); public: ItemDB(); // Constructor void InsertNewItem(Item* new_one); void DeleteExistingItem(int code); void FindDisplayItemUPC(int code); void FindDisplayItemName(char* name); void PrintAllItems(); };