Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | In this lecture we continue our basic coverage fundamental topics in object-oriented programming. First we contrast objects and values, as defined by classes and structs. Next, we study organizations of C# programs. In the final part we introduce the ideas of object-oriented design patterns. |
Reference Types |
Reference Types Slide Annotated slide Contents Index References Textbook | Classes are reference types in C#. It is therefore important to understand this concept. |
|
|
|
Illustration of variables of reference types Slide Annotated slide Contents Index References Textbook | On this page we explain what happens in the assignment p1 = p2, where both p1 and p2 are variables of compatible reference types. See the similar example for value types on a later page. |
Variables of reference types. The situation before the assignment p1 = p2. |
Program: The variables p1 and p2 refer to two points. p1 is assigned to p2. |
|
Variables of reference types. The situation after the assignment p1 = p2. |
|
Overview of reference types in C# Slide Annotated slide Contents Index References Textbook | There are other reference types than classes. This page provides an overview of the reference types in C#. |
|
|
|
Comparing and copying objects via references Slide Annotated slide Contents Index References Textbook | This page introduces three types of equality: reference equality, shallow equality, and deep equality. A similar categorization is used for copying. |
|
|
|
|
Equality in C# Slide Annotated slide Contents Index References Textbook | There are several different equality operations in C#. This page provides an overview. |
|
|
|
Exercise 4.2. Equality of value types and reference types | Take a close look at the following program which uses the == operator on integers.
Predict the result of the program. Compile and run the program, and explain the results. Were your predictions correct? |
Value Types |
Value types Slide Annotated slide Contents Index References Textbook | Value types stand as a contrast to reference types. There are dramatic difference between the use of value types and reference types in C#. |
|
|
|
Illustration of variables of value types Slide Annotated slide Contents Index References Textbook | This page illustrates assignment on value types, in the same way as done for reference types on an earlier page. |
|
Variables of value types. The situation before the assignment p1 = p2. |
Program: The variables p1 and p2 refer to two points. p1 is assigned to p2. |
|
Variables of value types. The situation after the assignment p1 = p2. |
|
Structs in C# Slide Annotated slide Contents Index References Textbook | Structs in C# are value types. On this page we show two concrete programming examples of structs. |
|
Program: Struct Point. |
|
Program: Struct Card. |
|
Program: A client of struct Card. |
|
|
|
Structs and Initialization Slide Annotated slide Contents Index References Textbook | Structs have constructors in the same way as classes. There are, however, a few special rules of constructors in structs. These special rules are described on this page. |
|
Program: Fields in structs cannot have initializers. |
|
Program: An explicit parameterless constructor is not allowed. |
|
|
|
Structs versus classes Slide Annotated slide Contents Index References Textbook | Here we present a back-to-back comparison of classes and structs. |
|
|
|
Examples of mutable structs in C# Slide Annotated slide Contents Index References Textbook | The purpose of this page is to point out the misfit between value types (C# structs) and mutability. This page should be used as motivation for the solutions on the following page. |
|
|
Program: The struct Point - mutable. |
|
Program: Moving a point by mutation. |
|
Program: Output from the application. |
|
Program: The struct Point - mutable, where move returns a Point. |
|
Program: Application the struct Point - Cascaded moving. |
|
Program: Output from the application. |
|
Examples of immutable structs in C# Slide Annotated slide Contents Index References Textbook | Immutable structs fits nicely with value semantics. This is illustrated with the continuation of the example from the previous page. |
|
Program: The struct Point - immutable. |
|
Program: Application the struct Point - immutable. |
|
Program: Output from the application. |
|
|
Exercise 4.3. Are playing cards and dice immutable? | Evaluate and discuss the classes Die and Card with respect to mutability. Make sure that you understand what mutability means relative to the concrete code. Explain it to your fellow programmers! More specific, can you argue for or against the claim that a Die instance/value should be mutable? And similarly, can you argue for or against the claim that a Card instance/value should be mutable? Why is it natural to use structs for immutable objects and classes for mutable objects? Please compare your findings with the remarks in 'the solution' when it is released. |
Boxing and Unboxing Slide Annotated slide Contents Index References Textbook | Boxing is a bridge from value types to reference types. |
|
The concept boxing: Boxing involves a wrapping of a value in an object of the class Object | ||
The concept unboxing: Unboxing involves an unwrapping of a value from an object |
|
Program: A program that illustrates boxing and unboxing. |
|
Program: Output of the boxing and unboxing program. |
|
Nullable types Slide Annotated slide Contents Index References Textbook | Nullable types fuse an extra null value into existing value types. |
|
Program: An integer sequence with Min and Max operations - a naive attempt. |
|
Program: An integer sequence with Min and Max operations - with int?. |
|
Program: A client of IntSequence. |
|
Program: Program output. |
|
|
Organization of C# Programs |
Program Organization Slide Annotated slide Contents Index References Textbook |
|
|
Examples of Program Organization Slide Annotated slide Contents Index References Textbook |
Program: A single class in the anonymous default namespace. |
|
Program: Compilation of a single class in the anonymous default namespace. |
|
Program: Two namespaces and a nested namespace with classes. |
|
Program: A client of classes in different namespaces. |
|
Program: An equivalent client of classes in different namespaces - no using clauses. |
|
Program: Compilation of the namespaces and client. |
|
Program: Nested namespaces with classes. |
|
Program: Equivalent program with nested namespaces - no physical nesting. |
|
Program: A client of classes in nested namespaces. |
|
Program: Compilation of the namespaces and client. |
|
Program: Part one of namespace Intro with the classes A and B. |
|
Program: Part two of namespace Intro with the class C. |
|
Program: A client class that uses the classes in namespace Intro. |
|
Program: Possible compilation of namespace Intro and class Client. |
|
Namespaces and Visibility Slide Annotated slide Contents Index References Textbook |
|
|
|
Namespaces and Assemblies Slide Annotated slide Contents Index References Textbook |
|
|
Patterns and Techniques |
Design Patterns Slide Annotated slide Contents Index References Textbook |
The concept design pattern: A pattern is a named nugget of instructive information that captures the essential structure and insight of a successful family of proven solutions to a recurring problem that arises within a certain context and system of forces [Brad Appleton] |
|
|
Object-oriented Design Patterns Slide Annotated slide Contents Index References Textbook |
|
|
|
|
The Singleton pattern Slide Annotated slide Contents Index References Textbook |
|
Program: A template of a singleton class. |
|
Program: A singleton Die class. |
|
Program: Application of the singleton Die class. |
|
Program: Output of singleton Die application. |
|
|
A Singleton Random Class Slide Annotated slide Contents Index References |
|
|
Program: A singleton Random class. |
|
Program: A Die class that uses singleton Random. |
|
Program: A Die application class. |
|
Program: Output from the Die application class. |
|
|
|
Factory methods Slide Annotated slide Contents Index References Textbook |
|
|
|
Examples of Static Factory Methods Slide Annotated slide Contents Index References Textbook |
|
Program: A clumsy attempt with two overloaded constructors. |
|
Program: A better solution with static factory methods. |
|
|
Program: A clumsy attempt with two overloaded constructors and an illegal constructor. |
|
Program: A better solution with static factory methods. |
|
|
Privacy Leaks Slide Annotated slide Contents Index References Textbook |
|
Program: A Mutable Date class. |
|
Program: A Person class that can return its private date of birth. |
|
Program: A client of the Person which modifies the returned birth Date. |
|
Program: The output of the Person client program. |
|
Exercise 4.6. Privacy Leaks | The starting point of this exercise is the observations about privacy leaks on the accompanying slide. Make sure that you understand the problem. Test-drive the program (together with its dependent Person class and Date class) on your own computer. If you have not already done so, read the section about privacy leaks in the textbook! Find a good solution to the problem, program it, and test your solution. Discuss to which degree you will expect that this problem to occur in everyday programming situations. |
Exercise 4.6. Mutable and immutable Point objects with Move methods | On the slides about mutable and immutable structs we have illustrated two versions of struct Point with two different implementations of the Move method. In the first struct, Move mutates the x and y coordinates and it returns this- the current object. The second struct is immutable and Move returns a new instance of Point which is moved relative to the receiver of the Move message. For both versions of Point we use the same client class, which sends Move messages to different points. In the lecture - and in the textbook - the differences between the first struct and the second struct has been explained relative to the client class. Be sure that you understand the difference! Now, you are asked to implement version 1 and version 2 of Point from above with classes instead of structs. Compile and run each of them relative to the given client class. In each of the two cases explain the behavior of the Move methods in the classes. In each of the two cases, explain how many instances of class Point there is created. |
Exercise 4.6. Pyramid BankAccounts | This exercise can be seen as a continuation of the bank account exercise in which we supplied a bank account with a backup account. The primary purpose of the exercises is to train recursive object-oriented programming. We organize bank accounts in a tree structure (called a pyramid) in which each bank account may have two sons which are also bank accounts. The money in the account are somehow distributed on the accounts of the pyramid. Program a version of class BankAccount with an owner, a balance, and possible references to two additional bank accounts: leftAccount and rightAccount. A skeleton of this class is provided together with a client program. You can assume that an account is either a leaf or the account has two sons.
|
Chapter 4: Reference types, Value types, and Patterns
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:14:05