Singleton Pattern Damian Gordon.

Slides:



Advertisements
Similar presentations
The Important Thing About By. The Important Thing About ******** The important thing about ***** is *****. It is true s/he can *****, *****, and *****.
Advertisements

Supporting Persistent Objects In Python Jeremy Hylton
Act I: Exposition where we meet our characters and the world they live in.
Rapid Web Development with Python/Django Julian Hill.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
A c i d s & B a s e s. A c i d - B a s e T h e o r i e s A r r h e n i u s B r o n s t e d - L o w r y L e w i s A r r h e n i u s B r o n s t e d - L.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
CS162 Week 2 Kyle Dewey. Overview Continuation of Scala Assignment 1 wrap-up Assignment 2a.
G make_counter params: body: count = 0 def counter():... my_counter params: body: count += 1... E1 count0 counter E2E2.
Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
Python classes: new and old. New and classic classes  With Python 2.2, classes and instances come in two flavors: old and new  New classes cleaned up.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
By the end of this session you should be able to...
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Iterators and Generators Thomas Wouters XS4ALL
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Sistem Operasi © Sekolah Tinggi Teknik Surabaya 1.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Python Object Model Sambasiva Suda PyCon India. Agenda Python Classic Objects Different types of Objects Relationships among objects Q & A.
Python: Linked Lists and Recursion Damian Gordon.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Python: Stacks and Queues (as a Linked List) Damian Gordon.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose.
Selection: CASE Statement Damian Gordon. Selection As well as the IF Statement, another form of SELECTION is the CASE statement.
Object Oriented Testing (Unit Testing)
Discussing Assignment 1
Reminder About Functions
Python: Recursion Damian Gordon.
Strings and Serialization
Recall: d is a divisor of n  n % d == 0
Sequences and Indexing
Modules and Packages Damian Gordon.
Common Design Patterns
Design Patterns C++ Java C#.
Python: Logic Gates Damian Gordon.
Design Patterns C++ Java C#.
Python: Advanced Sorting Algorithms
Linked Lists Damian Gordon.
Python Classes By Craig Pennell.
Boolean Logic Damian Gordon.
Queues: Implemented using Arrays
Basic Inheritance Damian Gordon.
Python: Algorithms Damian Gordon.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Stacks: Implemented using Linked Lists
CS 350 – Software Design Singleton – Chapter 21
Structured Programming
Circular Queues: Implemented using Arrays
Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display.
Test Automation For Web-Based Applications
Introduction to Computer Science
Python: Stacks and Queues (as an Array)
Python: Sorting – Selection Sort
Python classes: new and old
Some Common Issues: Common Algorithms
Access Control Damian Gordon.
Python classes: new and old
Queues: Implemented using Linked Lists
Python Classes In Pune.
Presentation transcript:

Singleton Pattern Damian Gordon

Singleton Pattern The singleton pattern is a design pattern which allows only one object based on a certain class to exist. One Object

Singleton Pattern class Singleton: IsSingleton = None def __new__(self): if self.IsSingleton == None self.IsSingleton = super(Singleton, self).__new__(self) # ENDIF; return self.IsSingleton # END __new__ # END Singleton.

Singleton Pattern In Python we use the _ _new_ _ class to help ensure there’s only one instance of a class. When the _ _new_ _ class is called, it typically creates a new instance of that class. When we override it, we first check if our singleton instance has been created; if not, we create it using a super() call.

Singleton Pattern class OneOnly: _singleton = None def __new__(cls, *args, **kwargs): if not cls._singleton: # THEN cls._singleton = super(OneOnly, cls ).__new__(cls, *args, **kwargs) # ENDIF; return cls._singleton # END __new__ # END OneOnly.

Singleton Pattern >>> o1 = OneOnly() class OneOnly: _singleton = None def __new__(cls, *args, **kwargs): if not cls._singleton: # THEN cls._singleton = super(OneOnly, cls ).__new__(cls, *args, **kwargs) # ENDIF; return cls._singleton # END __new__ # END OneOnly. >>> o1 = OneOnly() >>> o2 = OneOnly() >>> o1 == o2 True >>> o1 <__main__.OneOnly object at 0xb71c008c> >>> o2

etc.