Lecture 4: templates + singletons

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Visibility Larman, Chapter 19 (with ideas from George Blank of NJIT) CSE432 Object Oriented Software Engineering.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Access to Names Namespaces, Scopes, Access privileges.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
CSE 3341/655; Part 3 35 This is all very wrong! What would the SW1/SW2 (RESOLVE) people say if they saw this? Problem: Lack of data abstraction; What would.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Python C API overview References:
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
C++ Template.
Topics: Templates Exceptions
Topic: Classes and Objects
From C to C++.
Chapter 16 UML Class Diagrams.
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Type Traits By Reggie Meisler.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Exceptions, Templates, and the Standard Template Library (STL)
C Programming Tutorial – Part I
A Lecture for the c++ Course
Object Oriented Programming using Java - Class Instance Variables
Inheritance II CMSC 202.
From C to C++.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Lecture 20 – April 2, 2002 Bond Objects.
Namespaces, Scopes, Access privileges
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
PRG 421 Education for Service-- snaptutorial.com.
PRG 421 Education for Service-- snaptutorial.com.
PRG 421Competitive Success/tutorialrank.com
Lecturer: Mukhtar Mohamed Ali “Hakaale”
PH page GoF Singleton p Emanuel Ekstrom.
Lecture 11 C Parameters Richard Gesick.
Overview of Memory Layout in C++
Beans and object editor
Lecture 16 - Interfaces Professor Adams.
when it needs to be done at compile-time
Simple Classes in Java CSCI 392 Classes – Part 1.
CISC/CMPE320 - Prof. McLeod
CS 350 – Software Design Singleton – Chapter 21
Suggested self-checks: Section 7.11 #1-11
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Object oriented programming (OOP) Lecture No. 6
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
5. Strategy, Singleton Patterns
Product Training Program
SPL – PS1 Introduction to C++.
SPL – PS2 C++ Memory Handling.
CMSC 202 Lesson 17 Inheritance II.
Threads and concurrency / Safety
Presentation transcript:

Lecture 4: templates + singletons

templates overview Very similar to java generics public class Foo<E> { … } Foo<String> x; Can be applied to: functions template<class T> void func(T val) { … } classes template<class T> class Foo { // Use T anywhere in here. } individual methods of a non-templatized class { template <class T> void abc(T val) { }

subtleties In C++ Template specialization: Created by compiler when referenced Must be in a .h file (no .cpp files!) Template specialization: Useful when some types behave differently than the norm. This can go in a .cpp file (if you just need it one place) template<> void func<std::string>(std::string val) { // special version of template for strings! }

subtleties, cont. You can specialize individual methods of a templatized class template<> void Foo<std::string>::method() { // Change just this method for strings }

static attributes of t-classes template <class T> class Foo { static T* svar; }; This variable must be defined / initialized. Usually done in a .cpp w/ a template-specialization template<> std::string * Foo<std::string>::svar = NULL;

singletons First (of several) design patterns Goals: Ensure only one of them Controlled global access Many trolls disparage this…but at least understand it.

Example // My preferred method class FooSing { protected: static FooSing * the_only_one; // init this elsewhere (.cpp) public: FooSing(int x) // Make sure no others! the_only_one = this; } ~FooSing() { the_only_one = NULL; } static FooSing * get_singleton_ptr() return the_only_one; }; // Discuss the alternate method (and why it won’t work here…)

Your goal Make a templatized-singleton class!