Programming to Interfaces

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Advanced Object-Oriented Programming Features
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
OOP Languages: Java vs C++
Chapter 12: Adding Functionality to Your Classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Appendix 1 - Packages Jim Fawcett copyright (c)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
C# for C++ Programmers 1.
CSE791 - Distributed Objects, Spring 2002
Abstract Factory Pattern
CSE775 - Distributed Objects, Spring 2006
Names and Attributes Names are a key programming language feature
Jim Fawcett CSE775 – Distributed Objects Spring 2009
CSE687 – Object Oriented Design
Introduction to C++ Systems Programming.
A First C++ Class – a Circle
Chapter 9 - Programming to Interfaces
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Templates in C++.
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
Overview of C++ Overloading
Array-Based Implementations
Practical Session 4 Rule of 5 R-value reference Unique pointer
Overriding Methods & Class Hierarchies
Parasol Lab, Texas A&M University
Object-Oriented Programming
Overview of C++ Polymorphism
Dependency Inversion principle
Chapter 8 - Design Strategies
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2006
Object Oriented Programming
SPL – PS4 C++ Advanced OOP.
Chapter 5 Classes.
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Programming to Interfaces Jim Fawcett Copyright © 2004

Chapter 9 - Programming to Interfaces What is an Interface? class fileInfo { friend class navig; public: fileInfo(); fileInfo(const fileInfo &fi); fileInfo(const std::string &path); ~fileInfo(); bool firstFile(const std::string &filePattern); bool nextFile(); void closeFile(); // some members deleted for brevity std::string date() const; std::string time() const; std::string attributes() const; bool isArchive() const; bool isCompressed() const; bool isDirectory() const; // members deleted std::string getPath(void); void setPath(const std::string &s); private: // private members deleted }; First answer: public members of a class Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces What is an Interface? class str { private: char *array; int len, max; public: str(int n = 10); // void and size ctor str(const str &s); // copy ctor str(const char *s); // promotion ctor ~str(); // dtor str& operator=(const str &s); // assignment operator char& operator[](int n); // index operator char operator[](int n) const; // index operator for const str void operator+=(char ch); // append char void operator+=(const str &s); // append str s int size() const; // return number of chars void flush(); // clear string contents }; std::ostream& operator<<(std::ostream& out, const str &s); Second answer: public members of a class plus global functions packaged with the class. Packaged means in the same header file and in the same namespace. Interface Principle: “For a class X, all functions, including [global] functions, that both: Mention X Are supplied with X Are logically part of X, because they form part of the interface of X.” Herb Sutter, Exceptional C++, Addison-Wesley, 2000 Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Koenig Lookup This second definition is consistent with Koenig Lookup: Koenig Lookup: If you supply a function argument of class type, then to find the function name the compiler is required to look not just in the local or surrounding scopes, but also in the namespace that contains the argument’s type. [paraphrased from Sutter, ibid] For this reason, for the string class, packaged in namespace std, which has an operator<< we can say: std::cout << myString; instead of: std::operator<<(std::cout, myString); Without Koenig lookup we are forced to use functional notation rather than operator notation to indicate that << is in the std namespace. Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces What is an Interface? Third Answer: Abstract class with no data members, no constructor and at least one pure virtual function. An abstract class has the same role as a C# or Java interface. It provides a means to use an implementation class but only binds to the abstraction provided by the interface. class ITest { public: virtual ~ITest() {} static ITest* CreateTestImpl(); virtual std::string ident()=0; virtual void addString(const std::string &str)=0; virtual std::string getString()=0; }; Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Note For the remainder of this presentation, we will be using the third definition: An Interface is an abstract class with no data members and no constructors. Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Design Layers It is very typical that a software design maps onto the three layers shown on the next slide. Top down design determines a policy layer and top-level partitions of the implementation layer – very little OOD at this level. Classes and class relationships dominate the implementation layer. Polymorphism is an important tool to minimize coupling between components of this layer and with the utility layer. Class encapsulation of data and operating system services, using bottoms up design, determines the utility layer. This decomposition has the advantages that: The policy layer is responsible for satisfying the application’s requirements. The implementation layer partitions the program’s responsibilities into manageable chunks. The utility layer is a rich source of reusable code. It provides a lot of small simple services used to compose the implementation. Chapter 9 - Programming to Interfaces

Program Layers – Typical Decomposition Chapter 9 - Programming to Interfaces

Problems with Layering There is one large disadvantage to this “vertical” layering: Each layer is dependent on the layer below. Policies need to create the objects that populate the implementation. But, in order to create these objects, the policy layer must include header files of the implementation modules and so depend on the implementation of those classes. But, the implementation is very volatile during development. Fixing design flaws and latent errors can introduce changes into both the policy and utility layers. The same comments apply to the implementation/utility layering. The need to include headers of unstable code means that the includer is also unstable and changes migrate throughtout the system – a very distressing situation for large systems. Chapter 9 - Programming to Interfaces

Programming to Interfaces If we introduce interfaces which we strive to make stable then changes in lower layers don’t affect the design of the upper layer. That layer is simply recompiled without change when the implementation (not the interface) is changed. An interface is an abstract base class that has no implementation. Thus it has no latent errors or performance problems to fix. As long as the layer above only uses that interface, changes in the implementation of interface functions don’t break the client’s code. Note that those implementations are provided by classes that derive from the interface. Upper layers still have to include the lower layer’s header files in order to create the objects in that layer. This means that the upper layer must be recompiled whenever the lower layer’s implementation changes, because the size of the objects are very likely to change. Chapter 9 - Programming to Interfaces

Binding to Interfaces, not Implementation Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Isolating Layers But, with a little extra work, we can do better. By providing object factories to build all objects in an implementation, clients that program only to interfaces no longer need to include header files of the imple-mentation, they just include header files of the factories, as shown on the next page. The result of this architecture is that: The policy layer depends only on the implementation interface and object factories, neither of which are likely to change. The implementation layer depends only on its own interface and implementation and on the interface and object factories of the utility layer. The utility layer depends only on its own interface and implementation. When a change is made to the implementation, we now find that we need not recompile the policy layer. Chapter 9 - Programming to Interfaces

Using Object Factories to Isolate Layers Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Example Application We will illustrate these ideas with a little prototype code. The example has three implementation layer classes Widget1, 2, and 3. The implementation layer provides an IWidget interface that establishes a protocol for clients to use when interacting with the implementation. The policy layer, Client, simply instantiates the Widget objects, using an object factory provided by the Widget project. It then proceeds to use them by calling a function of the public interface, IWidget, on each one. Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Effect of Changes Changes to Interface provided by component Changes to Implementation of component Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces Changes to Interface Changing the signature of a method: Breaks the design of every client using the method. Changing the order of methods: Forces recompilation of every client Adding a new method: May force recompilation of every client Chapter 9 - Programming to Interfaces

Changes to Implementation If client has reference to concrete class: Forces recompilation, may break client’s design. If client has reference to interface but creates implementing component: Forces recompilation. If client has reference to interface and uses a factory to create implementing component: Factory must be recompiled, clients simply relink. If the factory holds pointers to static creational functions even factory does not have to be rebuilt. If component is built as a DLL, provides an interface and a pluggable factory: New library is copied over the existing library. Nothing needs to be done to client. Chapter 9 - Programming to Interfaces

Chapter 9 - Programming to Interfaces End of Presentation Chapter 9 - Programming to Interfaces