Lecture 14 Singleton pattern Fiaspel class diagram, sample code Course analysis.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Lecture 13 UML diagrams state-chart diagrams Design Pattern.
OOMPA Lecture 10 Design Patterns Singleton Factory Method
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.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Design Patterns.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Software Components Creational Patterns.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Game Programming 07 OGRE3D Mesh in Action 2010 년 2 학기 디지털콘텐츠전공.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
Salman Marvasti Sharif University of Technology Winter 2015.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Design Patterns Introduction
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns: Brief Examples
Classes (Part 1) Lecture 3
Inheritance ITI1121 Nour El Kadri.
7. Inheritance and Polymorphism
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Agenda Warmup AP Exam Review: Litvin A2
CS Week 14 Jim Williams, PhD.
Can perform actions and provide communication
Software Engineering Lecture 7 - Design Patterns
Can perform actions and provide communication
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Design Patterns - Creational Patterns
Singleton Pattern Pattern Name: Singleton Pattern Context
Software Design Lecture : 12.
Can perform actions and provide communication
CISC/CMPE320 - Prof. McLeod
CS 350 – Software Design Singleton – Chapter 21
CIS 199 Final Review.
Design pattern Lecture 6.
Creational Patterns.
Lecture 10 Concepts of Programming Languages
Software Design Lecture : 39.
Presentation transcript:

Lecture 14 Singleton pattern Fiaspel class diagram, sample code Course analysis

Singleton Pattern The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. Examples: There can be many printers in a system but there should only be one printer spooler. There should be only one instance of a WindowManager (GrainWindowingSystem). There should be only one instance of a filesystem.

Singleton Pattern How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but does not keep you from instantiating multiple objects. A better solution is to make the class itself responsible for keeping track of its sole instance. The class ensures that no other instance can be created (by intercepting requests to create new objects) and it provides a way to access the instance.

Singleton Pattern Use the Singleton pattern when There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Singleton Structure Singleton static Instance() SingletonOperation() GetSingletonData() static uniqueinstance singletonData return uniqueinstance

Singleton Particpants Singleton Defines an Instance operation that lets clients access its unique instance. Instance is a class operation (static member function in C++) May be responsible for creating its own unique instance Client Acesses a Singleton instance solely through Singleton’s Instance operation.

Singleton Consequences Controlled access to sole instance Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it. Reduced name space The Singleton pattern is an improvement over global variables. It avoids polluting the name space with global variables that store sole instances.

Singleton Consequences Permits refinement of operations and representations The Singleton class may be subclassed and it is easy to configure an application with an instance of this extended class at run-time. More flexible than class operations An alternative is to use static member functions in C++. However it is difficult to change the design to allow more than one instance of a class and static member functions in C++ are never virtual, so subclasses can not override polymorphically.

Singleton Implementation Ensuring a unique instance The Singleton pattern makes the sole instance a normal instance of a class, but that class is written so that only one instance can ever be created. A common way to do this is to hide the operation that creates the instance behind a static class operation that guarantees that only one instance is created.

Singleton Sample Code class Singleton { public: static Singleton* Instance(); // clients access the Singleton exclusively through // the Instance() member function protected: Singleton(); // the constructor is protected, such that a client // which tries to instantiate a Singleton object gets // a compiler error private: static Singleton* instance_; };

Singleton Sample Code Singleton* Singleton::instance_ = 0; // initialize static member data of class Singleton Singleton* Singleton::Instance() { if (instance_ == 0) // if not created yet instance_ = new Singleton; // create once return instance_; }

Fiaspel BoardPosition -xpos, ypos int -occupied bool voidDraw() Bool Occupied() Token* GetToken() BoardPosition* Next(colour, step) Token -colour int -state enum void Draw (BoardPosition) void SetState(int) int GetColour() token 1 0,1 next 4 1

Fiaspel HomeBoard Position BoardPosition* Next(colour, step) BoardPosition next_pos * 6 BoardPosition

Fiaspel Dice -state int void RollDice() int GetState() Base -xpos, yos int -colour int int GetColour() Bool Clicked(xpos,ypos) BoardPosition* Start() Bool Empty() void Draw() Token* RemoveToken() Void Add(Token*) Token token 0,10..4 BoardPosition start 0,11

Fiaspel Board Bool Occupied() Token* GetToken(xpos, ypos) BoardPosition* Clicked(xpos, ypos) positions 1 N BoardPosition bases 1 4 Base

Fiaspel Player -colour int -name string String GetName() Bool Won() Bool CanMove(int) Bool OwnToken (Token *) Token -colour int -state enum void Draw (BoardPosition) token 14

Fiaspel Game void InitGame() Player* CurrentPlayer() Bool LegalMove( BoardPosition*, BoardPosition*) void MoveToken (BoardPosition*, BoardPosition*) Dice Player Board dice 11 players 14 board 11

Sample Code class Game { private: Board* board; vector players; Dice* dice; Player* current_player; public: bool LegalMove(Base* from, BoardPosition* to); bool LegalMove(BoardPosition* from, BoardPosition* to); Move(Base* from, BoardPosition* to); Move(BoardPosition* from, BoardPosition* to); };

Sample Code class BoardPosition { private: Token* token; vector next; bool occupied; public: bool Occupied() { return occupied;}; void RemoveToken() { occupied=false; token=0;}; Token* GetToken(){ return token}; AddToken(Token *new_token) {occupied=true; token=new_token;}; virtual BoardPosition* Next(int colour, int steps) ; virtual BoardPosition* Next(int colour); };

Sample Code class HomeBoardPosition : public BoardPosition { private: vector next_pos; // holds next board // positions for every possible value of the dice public: virtual BoardPosition* Next(int colour, int steps); virtual BoardPosition* Next(int colour); };

Sample Code BoardPosition* BoardPosition::Next(int colour) { return tmp->next[colour]; }; BoardPosition* BoardPosition::Next(int colour, int steps) { BoardPosition* tmp=this; for (int i=0; i<steps; i++) tmp=tmp->Next(colour); return tmp; };

Sample Code BoardPosition* HomeBoardPosition::Next(int colour, int steps) { return next[steps-1]; }; BoardPosition* HomeBoardPosition::Next(int colour) { return next[0]; };

Sample Code Bool Game::LegalMove(BoardPosition* from, BoardPosition* to) { if (! from->Occupied()) return false; if (! from->GetToken()->GetColour()==CurrentPlayer()->GetColour()) return false; if (to->GetToken()->GetColour()==CurrentPlayer()->GetColour()) return false; if (from->Next(CurrentPlayer()->GetColour(),dice->GetState()) == to) return true; else return false; }

Sample Code Bool Game::LegalMove(Base* from, BoardPosition* to) { if (from->Empty()) return false; if (! from->GetColour()==CurrentPlayer()->GetColour()) return false; if (to->GetToken()->GetColour()==CurrentPlayer()->GetColour()) return false; if (from->Start()== to) return true; else return false; }

Sample Code enum token_states {inbase, onboard, home}; void Game::Move(BoardPosition* from, BoardPosition* to) { Token *tmp=from->GetToken(); from->RemoveToken(); if (to->Occupied()) { Token* kicked=to->GetToken(); board->GetBase(kicked->GetColour())->Add(kicked); kicked->SetState(inbase); } to->AddToken(tmp); }

Course Analysis Fill out the questionaire on the course webpage Use the comment boxes for suggestions, complaints, negative and positive aspects of the course.

Course Analysis Do you think the course was Easy Medium Difficult How was the relation in the lecture between object oriented programming and design and C++ language specific parts. Too much C++ Right balance Too much object oriented design

Course Analysis Do you think the course was interesting and useful for you? Yes Partially useful No Do you think your previous knowledge (e.g. programming experience in C) was sufficient for this course? Yes Somewhat No

Course Analysis What do you think about the course literature? Stroustrup : The C++ Programming Language Lippman : C++ Primer Lafore : Object Oriented Prog. In C++ Gamma : Design Patterns

Course Analysis What do you think about the additional course material (e.g. slides, copies from books, quick references, tutorials, software tools)? Useful Partially useful Useless or incomplete

Course Analysis What do you think about the lectures? Pedagogics Good Acceptable Unacceptable Presentation, slides, questions Good Acceptable Unacceptable

Course Analysis What do you think of the exercise hours Contents Useful Partially useful Not useful at all Pedagogics, assistent Competent Partially Competent Incompetent

Course Analysis What do you think about the lab hours Help by assistents Good Acceptable Unacceptable Availability, number of hours, waiting time Good Acceptable Unacceptable

Course Analysis Did you feel that you got enough and competent help in general, help beside the labs, pointers to reading, hints, tips Good Acceptable Unacceptable

Course Analysis What do you think about the lab assignments Difficulty Easy Suitable Difficult Programming tasks Interesting Partially interesting Uninteresting

Course Analysis Which lab assignment did you like best? Lab 2 menu system Lab 3 drawing program Lab 4 Fia game Which lab assignment did you like the least? Lab 2 menu system Lab 3 drawing program Lab 4 Fia game

Course Analysis How much time did you spend on the labs in total? Less than 60 hours hours More than 120 hours What do you think about the examinations? Fair Mostly fair Unfair

Course Analysis What is the percentage of your study time this semester that you spend on this course? Less than 25% 25-50% More than 50% Do you think that 4 points for the course are not enough enough too many

Course Analysis How would rate the lectures? Very good Good Acceptable Barely acceptable Bad

Course Analysis How would rate the course in general? Very good Good Acceptable Barely acceptable Bad

Course Analysis Was it a problem for you that the lectures were taught in English? Yes Somewhat of a problem No

Course Analysis What did you like best about the course? What did you like least about the course?