1 / 92

CIS225 – Advanced Java

CIS225 – Advanced Java. Introduction to Object-Oriented Analysis and Design. Introduction. This presentation introduces objects, classes, class inheritance and interfaces.

Télécharger la présentation

CIS225 – Advanced Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS225 – Advanced Java Introduction to Object-Oriented Analysis and Design CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  2. Introduction This presentation introduces objects, classes, class inheritance and interfaces. You will learn the concepts of object-oriented programming. Heavy emphasis is placed on the development of software systems using the Object-Oriented Paradigm CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  3. Introduction The key to object-oriented programming is to model the application in terms of cooperative objects that reflect reality. You design classes that describe the behavior of these objects. Carefully designed classes are the key to a successful application. There are many levels of abstractions in system design: method abstraction, class abstraction, encapsulation, etc CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  4. Overview • The Object-Oriented Paradigm • Managing Complexity with Abstraction • Building a Specification Model • Finding Objects and Identifying Responsibilities • Specifying Static and Dynamic Behavior • Introduction to the Unified Modeling Language • Introduction to Design Patterns • Summary CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  5. The Object-Oriented Paradigm is defined as a “set of theories, standards, and methods that together represent a way of organizing knowledge CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  6. Benefits of O-O Technology Reduces the cost of software development: Provides the software developer with real- world, programmable components Provides the capability to share and reuse code with O-O techniques that reduce time to develop applications For example: to create a button, you simply use the JButton class to create the instance of JButton - don’t reinvent the wheel CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  7. Benefits of O-O Technology cont. Reduces the cost of software development: Provides the capability to localize and minimize the effects of modifications through programming abstraction mechanisms for faster enhancement development and provides more reliable and robust software Manages complexity allow developers to address more difficult applications CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  8. Managing Complexity with Abstraction • History • In the early 1960’s, developers had two major constraints : • Performance limited in processor speed the average desktop computer of today is faster than the largest machines of the60s • Using less core(memory) and storage capacity-virtual memory had not been invented. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  9. HistoryHistory(cont.) The programming language that we use directly influence the way that we view (model) reality. In the 1970s, we used the imperative programming paradigm for modeling reality- C, Pascal, and PL/1 – the structured method high degree of coupling, and poor cohesion In the 1980s, we used SQL and 4GL with relational databases. We used a data-modeling paradigm entity-relationship diagrams CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  10. History(cont.) In the 1990s, we used C++, Smalltalk, and Objective C -object-oriented (O-O) paradigm for modeling reality- The object-orientation paradigm using these languages had many limitations – high cohesion, tight coupling Java now provides us with a refined version of the O-O paradigm. We have added a few new keywords and additional features and data types. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  11. History(cont.) Most importantly, programmers now have add a modeling mechanism to view and model reality. Think what it means to compute How we organize our information inside a computer system How we describe our view (model) of reality CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  12. The Object-Oriented Paradigm cont. • Classes and Objects • Object-Oriented Principles • OO Model of Computation CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  13. Classes • A class can be viewed from four different perspectives: • Modeling • Design • Implementation • Compilation CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  14. Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. .A program is defined by using one or more classes. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  15. Classes - From a modeling perspective • a class is a template for a category and defines: • Characteristics (attributes) - data variables • Services (behaviors) - methods • Its interface is the protocols used to access an object’s services and its internal state • Its implementation are the internal procedures for providing services • Rules and Policies – constraints that are applied • Relationships – peer-to-peer, hierarchical, generalization/specialization CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  16. Classes- From a design perspective • a class is a special kind of object • It is a collection of all the objects instantiated from it • It is used to create and destroy objects that belong to its collection • It is used to hold common data and provide for group services such as: • Find an instance • Keep averages • Hold shared information CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  17. Classes -From an implementation perspective • a class is a “global” object with class data members and class services • Applications can access the class services by using the class’ name • An application can create objects by using its constructor to “instantiate” an object • A class is used to implement the category concept in the Java programming language CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  18. Classes -From a compiler’s perspective • the class is a programmer’s defined data type • It is a mechanism for defining and creating runtime objects CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  19. The 13 O-O Basic Principles • Encapsulation The object contains both the data and the code to manipulate the data. A black box provides encapsulation the hiding of unimportant details • Information Hiding The object that contains the data defines the services available to other objects and hides the data and implementation details • MessagePassing • An object may communicate with another object only by exchanging messages (parameters) CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  20. The 13 O-O Basic Principles(cont.) 4. Late Binding • The specific receiver of any given message is not known until runtime. So the determination of which method to invoke cannot be made until run time. • The virtual machine, not the compiler, selects the appropriate method. • Late binding enables us to model a real behavior of the world – Example: when a class is scheduled, it is unknown who will attend or how many students. You only know once the class begins. Gen/Spec with polymorphism (#8) principle CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  21. The 13 O-O Basic Principles(cont.) 5. Delegation • Work is passed via message-passing from one object (client) to another object (agent) until it finally reaches the object that has both the data and code to perform the work – • can delegate the authority to get the work done; cannot delegate the responsibility -an example of information hiding principle • Delegation is sometimes called the perfect bureaucratic principle CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  22. The 13 O-O Basic Principles(cont.) 6. Class/Instance/Object Categorizing helps us to organize the complex world in which we live. All objects are instances of a class and can be created or deleted at runtime How an object provides a service is determine by the class of which the object is an instance Thus, all objects of the same class, use the same method (code) in response to a specific service request CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  23. The 13 O-O Principles cont. 7. Generalization/Specialization without Polymorphism • Classes can be organized by using a hierarchical inheritance structure • In the structure, the specialized class (subclass) inherits the state (data fields), the behavior (set of methods), and the relationships from the superclass • An abstract superclass is a class that is used to create only subclasses: therefore there are no direct instances of that class. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  24. Abstract Class (contd.) Abstract classes are like regular classes with data and methods, but you cannot create instances (objects) of abstract classes using the new operator. An abstract method cannot be placed in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  25. Abstract Class (contd.) A classes that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. A subclass can be abstract even if its superclass is concrete. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  26. The 13 O-O Principles cont. 8. Generalization/Specialization with Polymorphism • The subclass inherits the attributes, relationships, prototype, and methods from the superclass that is higher in the tree. • Subclasses can create their own method to override behavior from the superclass to provide the same service. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  27. The 13 O-O Principles 9. Relationships • Associations and aggregation are used to capture the collaboration between objects necessary to provide a service to a client - called a link • Types of relationships include: association, aggregation, composition, link, generalization, specialization will discuss later with examples CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  28. The 13 O-O Principles cont The instanceof operator can help us learn the class of an object. Student x = new UndergraduateStudent(); if(x instanceof UndergraduateStudent ){ System.out.println(“Mr. X is an undergraduate student”); }else{ System.out.println(“Mr. X is a graduate student”); } CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  29. The 13 O-O Principles cont 10. Interface/Instance/Object • All objects that implement an interface are instances of that interface • Interfaces define types • Instances of an interface cannot be created (instantiated) or destroyed (deleted) as an instance interface. • Each must be created or destroyed as an instance of the class to which it is a member. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  30. The 13 O-O Principles cont 11. Generalization/Specialization of Interfaces • Interfaces can be organized by using a hierarchical inheritance structure • The specialized interface inherits the service protocol from the generalized interfaces that are higher in the (tree) structure. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  31. The 13 O-O Principles cont 12. Reflection • Each object knows detailed information about the classes and interfaces of which it is an instance • This means that an application can at runtime acquire detail information about the object from the object itself. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  32. The 13 O-O Principles cont 12. Reflection cont’d. ways: 1. Determine the class of the object 2. Get info about a class’s modifiers, member variables, methods, constructors, and superclass 3. Find out what constants and method declarations belong to an interface 4. Create an instance of a class whose name is not known until runtime CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  33. The 13 O-O Principles cont 12. Reflection cont’d. ways: 5. Get and set the value of an object’s field , even if the field name is not known to your program until run time. 6. Invoke a method on an object, even if the method is not known until runtime. 7. Create a new array, whose size and component type are not known until runtime, and then modify the array’s components. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  34. The 13 O-O Principles cont 12. Reflection cont’d. To make your programs easier to debug and maintain do not use Method objects. Instead use interfaces and then implement them in the class that performs the needed action CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  35. The 13 O-O Principles cont 13. Multithreading • Each object can have concurrent execution paths This means that an object can handle multiple events (or service requests) in a concurrent manner) CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  36. Refactoring Martin Fowler coined this term for restructuring code in a Disciplined way –make small transformation to your code Example (From): To: CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  37. O-O Paradigm -Objects • An object is the most fundamental concept/mechanism of O-O programming • There are three different ways of looking at objects: • Application modeling view • Design modeling view • Formal viewpoint CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  38. Objects - From an application modeling view • An object has the following components: • Characteristics – internal information that describes the object (attribute name and value) • Services (behaviors)– a collection of services that has some semantic meaning sometimes called a protocol a. interface (prototype internal information) b. implementation (method) 3. Unique Identifier – Each object is given a unique ID at creation and cannot be changed. Usually the machine id number plus the time in milliseconds - CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  39. Objects - From an application modeling view cont. • An object has the following components: 4. Rules and Policies – establish constraints which are applied to the object 5. Relationships – how objects stand in relations with other objects peer-to-peer or hierarchical CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  40. Objects • From a design modeling view an object: • Is an example (instance) of a category (class) form a hierarchy of gen/spec • May be an example of a type (interface) A type is a collection of service declaration (service name, arguments, return type and exception thrown) with no implementation code 3. May be a “metaclass” object- may have attributes, services, object relationships, and rules; however may not be in a gen/spec relationship CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  41. Objects • From a design modeling view an object cont. : 4. Is created by a category (class) object 5. Can communicate either synchronous (method invocation) or asynchronous (event generation) with other objects 6. Can be “persistently” associated with another object in either a peer-to-peer or hierarchical relationship CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  42. Objects • From a formal perspective an object is defined by: • Its responsibilities – the value that it adds to the system • Its rule set – -Attribute Assertions: Range, Enumeration, and Type constraints - Operation Assertions: Pre- and Post- and invariance conditions - Class invariance: A logical statement(s) about the object that must be true at all times CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  43. Objects cont. • From a formal perspective an object is defined by: • Its rule set(cont’d): - inference engines –resolve multiple inheritance conflicts • Its classification –must be in some category (class) even if it is a category of one object • Its type(s). An object may implement the services of any specific type w/o being constrained it its implementation of the services • Its relationships with other objects peer-to-peer or hierarchical CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  44. OO Model of Computation • The problem-solving view of OO programming is different from the pigeonhole model used with imperative programming • OO use terms like object, class, and services instead of terms like assignments, variables or memoryaddresses CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  45. OO Model of Computation • OO uses a community of helpers that work together to solve the problem • OO programming uses a simulation model of computation • We define objects in the problem domain that will help us solve the problem • We define how these objects interact with one another and then set them in motion CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  46. Managing Complexity with Abstraction CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  47. Managing Complexity with Abstraction • Complex Systems • Abstraction Mechanisms • Service Activation Abstractions • Process Control Abstractions • Relationships • Rules CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  48. Complex Systems • Much of the complexity of imperative programming stems from the high degree of coupling and poor cohesion makes the systems inflexible and unmaintainable • Coupling – the dependence of one piece of code on either another section of code and/or some data storage CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  49. Complex Systems • Cohesion – how well a set of code and its associated data fit together • A class should describe a single entity, and all the class operations (methods) should fit together to support a coherent purpose • We can use a class for students. We should not include student and staff in the same class because students and staff have different entities. CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

  50. Complex Systems Consistency • Follow standard Java programming style and naming conventions. • Choose informative names for classes, data fields, and methods • A popular style is to place the data declaration before the constructor, and place constructors before methods. See UML Class and Object diagram.doc in folder UML Folder CIS225 - Advanced Java Richard C. Lee and William M. Tepfenhart

More Related