Summary

This document provides a detailed overview of method combination concepts in programming, focusing on the implementation of method combination in C#. It explains different types of method combination, including imperative and declarative patterns, and discusses their applications.

Full Transcript

Method Combination Method Combination We sometimes talk about method combination when two or more methods of the same name Op cooperate in solving a given problem Programmatic (imperative) control of the combination of Op methods Superclass controlled: The Op method in...

Method Combination Method Combination We sometimes talk about method combination when two or more methods of the same name Op cooperate in solving a given problem Programmatic (imperative) control of the combination of Op methods Superclass controlled: The Op method in class A controls the activation of Op in class B Subclass controlled: The Op method in class B controls the activation of Op in class A Imperative method combination An overall (declarative) pattern controls the mutual cooperation among Op methods A.Op does not call B.Op - B.Op does not call A.Op. A separate abstraction controls how Op methods in different classes are combined Declarative method combination C# supports subclass controlled, imperative method combination via use of the notation base.Op(...) Parameter Variance How do the parameters of Op in class A and B vary in relation the variation of A and B A aref; B bref = new B(); S sref = new S(); aref = bref; // aref is of static type A and dynamic type B aref.Op(sref); // B.Op is called with an S-object as parameter. // What if an operation from T is activated on the S-object? It turns out that parameter variance is not really a relevant topic in C#... Covariance and Contravariance Covariance: The parameters S and T vary the same way as A and B Contravariance: The parameters S and T vary the opposite way as A and B Abstract Classes - Sealed Classes Abstract Classes Abstract classes are used for concepts that we cannot or will not implement in full An abstract class is a class with one details or more abstract operations An abstract operation is specially marked operation with a name and with formal parameters, but without a body An abstract class may announce a number of abstract operations, which must be supplied in subclasses cannot be instantiated is intended to be completed/finished in a subclass Abstract classes and abstract methods in C# An abstract Stack without data representation, but with some implemented operations Detailed rules - some are surprising Abstract classes can be derived from a non-abstract class do not need not to have abstract members can have constructors Abstract methods are implicitly virtual Abstract Properties Properties and indexers may be abstract in the same way as methods Sealed Classes and Sealed Methods A sealed class C prevents the use of C as base class of other classes Sealed class Cannot be inherited by other classes Seals all virtual methods in the class Sealed method Cannot be redefined and overridden in a subclass The modifier sealed must be used together with override A sealed, overridden method prevents additional overriding Interfaces An interface corresponds to a fully abstract class. No matters of substance is found in the interface, just declarations of intent An interface describes signatures of operations, but it does not implement any of them Interfaces Classes and structs can implement one or more interfaces An interface can be used as a type, just like classes Variables and parameters can be declared of interface types Interfaces can be organized in multiple inheritance hierarchies Interfaces in C# Both classes, structs and interfaces can implement one or more interfaces Interfaces can contain signatures of methods, properties, indexers, and events modifiers interface interface-name : base-interfaces { method-descriptions property-descriptions indexer-descriptions event-descriptions } return-type method-name(formal-parameter-list); return-type property-name{ get; set; } return-type this[formal-parameter-list]{ get; set; Examples of Interfaces Two or more unrelated classes can be used together if they implement the same interface In the example above, the GameObject could as well have been implemented as an abstract superclass Interfaces from the C# Libraries The C# library contains a number of important interfaces which are used frequently in many C# programs IComparable An interface that prescribes a CompareTo method Used to support general sorting and searching methods IEnumerable An interface that prescribes a method for accessing an enumerator IEnumerator An interface that prescribes methods for traversal of data collections Supports the underlying machinery of the foreach control structure IDisposable An interface that prescribes a Dispose method Used for deletion of resources that cannot be deleted by the garbage collector Supports the C# using control structure ICloneable An interface that prescribes a Clone method IFormattable An interface that prescribes an extended ToString method Sample use of IComparable Object of classes that implement IComparable can be sorted by a method such as Array.Sort Sample use of IEnumerator and IEnumerable The interface IEnumerator in System.Collections is used as the basis for iteration with foreach The interface IEnumerable prescribes an operation GetEnumerator that returns an enumerator of type IEnumerator Sample use of IFormattable The ToString method prescribed by IFormattable takes two parameters which allow finer control of the string generation than the parameterless method ToString from class Object. A reproduction of the interface IFormattable. The ToString method of IFormattable: The first parameter is typically a single letter formatting string, and the other is an IFormatProvider The IformatProvider can provide culture sensible information. ToString from Object typically calls ToString(null, null) Explicit Interface Member Implementations If a member of an interface collides with a member of a class, the member of the interface can be implemented as an explicit interface member Explicit interface members can also be used to implement several interfaces with colliding members Patterns and Techniques The Composite design pattern A Composite design pattern composes objects into tree structures. Clients operate uniformly on leaves and composite nodes The tree structure may be non-mutable and built via constructors Alternatively, the tree structure may be mutable, and built via Add and Remove operations A Composite Example: Music Elements A music element can either be a note, a pause, a sequential or a parallel collection of music elements A music element has operations such as Duration, Transpose, and Linearize An application of Music Elements An application of some MusicElement objects. Implementation of MusicElement classes The classes in the MusicElement composite A Composite example: IntSequence An integer sequence can either be a singular integer, an interval, or a composite sequence An integer sequence has the operations Min, Max, and GetEnumerator A Composite example: IntSequence application An IntSequence Composite An application of IntSequence objects. Output from the IntSequence application. Implementation of the IntSequence classes The classes in the IntSequence composite design pattern The abstract class IntSequence. The class IntInterval. The class IntSingular. The class IntCompSeq. A Composite Example: A GUI The classes of a Graphical User Interface (GUI) make up a Composite design pattern A program that builds a sample composite graphical user interface. A Composite Example: A GUI The object structure of the sample graphical user interface A Composite Example: A GUI An small extract of the Windows Form Component class structure We recognize the structure of a Composite design pattern Cloning Cloning creates a copy of an existing object Object cloning Shallow cloning: Instance variables of value type: Copied bit-by-bit Instance variables of reference types: The reference is copied The object pointed at by the reference is not copied Deep cloning: Like shallow cloning But objects referred by references are copied recursively Cloning in C# Internally, C# supports shallow cloning of every object. Externally, it must be decided on a class-by-class basis if cloning is supported. Cloning in C# Shallow cloning is facilitated by the protected method MemberwiseClone in System.Object A class C that allows clients to clone instances of C should implement the interface ICloneable The fragile base class problem If all methods are virtual it is possible to introduce erroneous dynamic bindings This can happen if a new method in a superclass is given the same name as a dangerous method in a subclass The Visitor design pattern Visitor organizes operations on Composites which need to traverse the Component nodes The natural object-oriented solution: One method per operation per Component class The Visitor solution All methods that pertain to a given operation are refactored and encapsulated in its own class Natural object-oriented IntSequence traversals We will study Min, Max, and Sum traversals of integer sequences Integer sequences are represented as a Composite (trees) of intervals, singulars and composite sequences Towards a Visitor solution The transition from natural object-oriented traversals to Visitor Steps: A Visitor interface and three concrete Visitor classes are defined The Intsequence classes are refactored - the traversal methods are moved to the visitor classes Accept methods are defined in the IntSequence classes. Accept takes a Visitor as parameter Accept passes this to the visitor, which in turn may activate Accept on components Try the accompanying SVG animation Visitors - Pros and Cons There are both advantages and disadvantages of Visitor ❖ Consequences of using a Visitor 1. A new kind of traversal can be added without affecting the classes of the Composite 2. A Visitor encapsulates all methods related to a particular traversal 3. State related to a traversal can - in a natural way - be represented in the Visitor 4. If a new class is added to the Composite all Visitor classes are affected 5. The indirect recursion that involves Accept and the Visit methods is more complex than the direct recursion in the natural object-oriented solution

Use Quizgecko on...
Browser
Browser