Singletons. The Singleton Design Pattern using Rhapsody in,c



Similar documents
Cyclic Architectures in UML

Excerpts from Chapter 4, Architectural Modeling -- UML for Mere Mortals by Eric J. Naiburg and Robert A. Maksimchuk

UML - Getting Started EA v4.0

ReqXChanger Closing the Gap between Requirements and Modelling

ABSTRACT FACTORY AND SINGLETON DESIGN PATTERNS TO CREATE DECORATOR PATTERN OBJECTS IN WEB APPLICATION

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Designing a Home Alarm using the UML. And implementing it using C++ and VxWorks

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Organization. Introduction to Software Engineering

UML for the C programming language.

Design Patterns. Design patterns are known solutions for common problems. Design patterns give us a system of names and ideas for common problems.

Getting Started with the Internet Communications Engine

Embedded/Real-Time Software Development with PathMATE and IBM Rational Systems Developer

How To Develop A Telelogic Harmony/Esw Project

Evaluating OO-CASE tools: OO research meets practice

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

I219 Software Design Methodology

From Systems to Services

UML for C# Modeling Basics

Object Oriented Software Design II

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Freescale Semiconductor, I

Python, C++ and SWIG

Roulette: Inheritance Case Study

Chain of Responsibility

1 Posix API vs Windows API

An Incomplete C++ Primer. University of Wyoming MA 5310

Java Interview Questions and Answers

UML Codegeneration. A Demonstration of Current Research Activities. Target: MSP430G Family TI Launchpad Evaluation Board

Applying Design Patterns in Distributing a Genetic Algorithm Application

Automated Virtual Cloud Management: The need of future

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Manage Software Development in LabVIEW with Professional Tools

Using UML Part One Structural Modeling Diagrams

A HW/SW Codesign Methodology based on UML

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Design Pattern for the Adaptive Scheduling of Real-Time Tasks with Multiple Versions in RTSJ

Programming Language Constructs as Basis for Software Architectures

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Moving from CS 61A Scheme to CS 61B Java

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Code Qualities and Coding Practices

Copyright 2008 The Pragmatic Programmers, LLC.

Automatic generation of fully-executable code from the Domain tier of UML diagrams

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Product Review ControlUp

Extreme Programming and Embedded Software Development

1 Abstract Data Types Information Hiding

Embedded Component Based Programming with DAVE 3

Rapid Application Development of a Decision Support System using Object Oriented Programming

Java in Education. Choosing appropriate tool for creating multimedia is the first step in multimedia design

Categories and Subject Descriptors. General Terms Algorithms, Design, Languages. Keywords Polymorphism, Object-Oriented Programming, Learning

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Special FEATURE. By Heinrich Munz

Curriculum Map. Discipline: Computer Science Course: C++

Source Code Translation

Applied Informatics C++ Coding Style Guide

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Install Java Development Kit (JDK) 1.8

UML SUPPORTED SOFTWARE DESIGN

Classes and Pointers: Some Peculiarities (cont d.)

Coding conventions and C++-style

Object oriented design process

Chapter 3 Chapter 3 Service-Oriented Computing and SOA Lecture Note

Generate Android App

Structural Design Patterns Used in Data Structures Implementation

Chapter 3 ADDRESS BOOK, CONTACTS, AND DISTRIBUTION LISTS

Enterprise Mobile Application Development: Native or Hybrid?

UML FOR OBJECTIVE-C. Excel Software

The Definitive Guide. Active Directory Troubleshooting, Auditing, and Best Practices Edition Don Jones

Club Accounts Question 6.

'& ##! %1# ##!!* #!!! 23!!!

Glossary of Object Oriented Terms

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Component visualization methods for large legacy software in C/C++

1 of 2 4/24/2005 9:28 PM

Why (and Why Not) to Use Fortran

Software documentation systems

What you should know about: Windows 7. What s changed? Why does it matter to me? Do I have to upgrade? Tim Wakeling

Enumerated Types in Java

A workshop on C to C++ migration and creation of classes that represent a type of On- Chip HW-Peripherals.

THE BCS PROFESSIONAL EXAMINATIONS Diploma. April 2006 EXAMINERS REPORT. Systems Design

Searching Algorithms

Understanding RSS in Safari

IRA 423/08. Designing the SRT control software: Notes to the UML schemes. Andrea Orlati 1 Simona Righini 2

The C Programming Language course syllabus associate level

Test-Driven Development

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

Efficient Time Management with Technology

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Reuse in Oracle SOA Suite 12c: Templates, Libraries or Services?

Marketing Tips. From M R K Development

Software Engineering Techniques

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Managing Special Authorities. for PCI Compliance. on the. System i

To Java SE 8, and Beyond (Plan B)

A Real Time, Object Oriented Fieldbus Management System

Copyright 2010, Jeffrey S. Krause, Esq. and Lawtopia, LLC

How To Design Your Code In Php (Php)

Transcription:

Singletons Techletter Nr 3 Content What are Design Patterns? What is a Singleton? Singletons in Rhapsody in,c Singleton Classes Singleton Objects Class Functions Class Variables Extra Functions More Information The Singleton Design Pattern using Rhapsody in,c There is a huge amount of different design patterns. The singleton design pattern is a simple, but also a very useful pattern. The pattern is used, when there should be only a single instance (object) of a certain class. An example for the usage of the pattern is the realization of hardware classes for dealing with interrupts or certain devices. These can only exist once in the whole system. For instance, there may be multiple timers in the embedded systems, but in most cases, they only have one interrupt. Therefore, the class for handling the interrupt can not be instantiated more than once and the singleton design pattern has to be used. This Techletter shows you different possibilities to use it, automatically or with some handwritten modification that offer you advantages!.

What s a Design Pattern? Design Patterns are solutions to often occurring problems in programming. A similar Problem that you are coping with right now has probably already been solved by someone else. Design Patterns are not library functions, they are just recipes for a good solution. Often they need to be implemented in a slightly different way than the standard solution. There are a lot of books about Design Patterns, we ll cover some later on. The most known book was that of the so-called GoF (Gang of Four) Modern OO Languages as Java offer some Design Patterns ready-implemented in a library. In,C this is a bit more difficult but not impossible. In this Techletter we describe a simple but very effective pattern called The Singleton Pattern. What s a Singleton? A Singleton is very simply a class that has only one instance. What is the use of that? Well, sometimes you have objects in your system where you are SURE that there is only one. Even more, you want to be SURE that you only have one at all times. On the right you see the JAVA implementation of a singleton. It uses a few specialities that come with OO languages, a Static Class Variable and a Static Class Function. The Static Class Variable is a variable that can be accessed by all objects of a Class. To some extend, the same applies for the Static Class Variable. The Function getinstance() can be called by any Object in this way: Object = ClassicSingleton::getinstance(); The variable instance will be null the first time. Then new is called and a Singleton Object is created. The constructor is also called and protected. In that way no other Class/Object can call the new function. This ensures that there can be no other instance. Now let s see if we can use this in Rhapsody in,c! Singletons in Rhapsody in,c The singleton implementation in Rhapsody in,c is quite confusing. There is a <<singleton>> stereotype that can be applied to an Object_Type which can be an Object or a Class. There is, however, no real code generation for a singleton Class as such. There is just a mechanism that checks if you have created too many static instances of the singleton class. Dynamically there is no check, so Rhapsody allows you Singleton in JAVA public class ClassicSingleton { private static ClassicSingleton instance = null; protected ClassicSingleton() { // Exists only to defeat instantiation. Beware! This JAVA Code looks pretty simple. It is, but it is not re-entrant... Two threads would be able to create 2 Objects if called at the right time! to call the create function as much as you like (The Create function is the Rhapsody in,c equivalent to the new statement in C++). A <<singleton>> object has an adapted code generation but has other disadvantages. Although it can be quite useful sometimes to use a <singleton> Object for instance when you want to implement interrupt routines that cannot be used with a me pointer. Even though there is no graphical way to instantiate a singleton object more then once, but in code you can and there is no way to check that! public static ClassicSingleton getinstance() { if(instance == null) { Beware! Many Design Patterns rely on OO principles like Inheritence and Templates. This is not always applicable to Rhapsody in,c! instance = new ClassicSingleton(); return instance;! Techl - TL3 Singleton.pages! 2

Singleton Class A singleton class does not affect the code generation as such, it just checks. If you only define a class as singleton, you will receive 2 warnings during code generation. One for the stereotype and the other for the fact that Rhapsody cannot find an instance. When you create an Instance from the Class, (Only one) then Rhapsody is (halfway) happy and serves you only one warning. Creating a second instance gives you another warning, multiple instances found. This is only checked for static instances, not for instances that are created dynamically. So, the only thing Rhapsody really does is checking if you have the correct number of instances. There is no difference in generated code, so a singleton object still has a me pointer and does not check itself if it is instantiated multiple times. This can be useful but the disadvantage is that there is no run-time check of instances. Singleton Object Another type of Singleton offered by Rhapsody is the Singleton Object. The stereotype <<singleton>> can be applied to an Implicit Object and will change the generated code. Compared to a normal implicit Object, the Singleton Object has no me-pointer, and no create/destroy function. There are certainly advantages to that, for instance, the absence of the,me-pointer allows programmers to use this for interrupt functions without having to create an extra global variable to supply the me-pointer to the interrupt. Also, because there are no dynamic functions, the creation of a second object is prevented. But the downside is that the users of the singleton need to know the name of the Singleton Object and the Singleton Object is always there, even if you don t need it.! Techl - TL3 Singleton.pages! 3

Here you see the code for the Singleton Object. The definition of the Singleton Object is done in the <singletonobject.c/.h> file and the declaration is done in the containing package file. Wouldn t it be nice to have a Singleton Class in,c that would have the advantages that the JAVA Example has? Is that possible? Well... partly. It contains some parts that must be done by hand. They could be done automatically too if we would use the User Adapted Code Generator. But we can use some mechanisms, like stereotypes and properties, in Rhapsody to obtain what we want with a little handwork. Perhaps IBM reads this too and changes the way Rhapsody uses Singletons... Stereotype A stereotype is something where you can create special #ifndef ASingletonObject_H #define ASingletonObject_H #include <oxf/ric.h> #include "SingletonObjectPkg.h" /*## package SingletonObjectPkg */ #include "SingletonObjectPkg.h" /*## classinstance ASingletonObject */ #include "ASingletonObject.h" /*## classinstance JustAnObject */ /*## classinstance ASingletonObject */ struct ASingletonObject_t ASingletonObject; /*## classinstance JustAnObject */ struct JustAnObject_t JustAnObject; void SingletonObjectPkg_initRelations( void ) { ASingletonObject_Init(); JustAnObject_Init(&(JustAnObject)); versions of model elements. We know the expression stereotype in our normal language. If we give a Human the stereotype Dutch we actually say: Dutch are humans, but they have special properties, they like cheese, live in windmills and wear wooden shoes. (UML stereotypes are always true, I m not sure about the normal language stereotype...) /*## class TopLevel::ASingletonObject */ struct ASingletonObject_t { RIC_EMPTY_STRUCT ; /*** User implicit entries ***/ /* Constructors and destructors:*/ void ASingletonObject_Init( void ); void ASingletonObject_Cleanup( void ); #endif Class Functions One of the problems with the implementation of the Singleton Pattern in Rhapsody in,c is the omission of the possibility to create Class Functions. Luckily there is an easy way to implement this using standard Rhapsody. The important one being the <<stereotype>> In a stereotype you can combine many properties. When you apply the stereotype to a model element, it will inherit all properties that the stereotype has. You can then use the New Type option to create a new Type, in this case we convert a plain function to a Class Function. Rhapsody will create a new folder where these are kept. We declare a stereotype <<ClassFunction>> as a New Term. We set the properties so that the me-pointer is not! Techl - TL3 Singleton.pages! 4

generated. We do not need the static modifier, in,c that would make the function inaccessible from outside the class. In fact, we could have used a normal public Class Function and just ignore the me-pointer. The method presented here is: - Much more elegant. It is clearly visible what happens. - Modeled instead of programmed. Everybody understands what is done here and can reproduce it. That is why UML is used instead of plain,c. Class Variables Class variables are a bit more difficult. Normally, Attributes are generated inside the Class structure and can never be declared static, that is a disadvantage of,c. But we also have types that we can use. A Type will be generated outside the Class structure. In combination with a <<stereotype>> New Term we can, with a minimal amount of hand labour, create a Class Variable that helps us in creating a real Singleton. The stereotype does the following: - It sets the name for the variable (prefixed with the class name) and the visibility. - The variable is declared static so it is not visible outside the C file. - Visibility is set to private. The Type must be Language Type and the declaration must be the desired Type. %s is the name of the Type. All we need to add now is the getinstance Function. We could just add it by hand but Rhapsody has a nice mechanism built-in, that helps us with the automatic generation of this function.! Techl - TL3 Singleton.pages! 5

Extra Function In Rhapsody it is possible to generate extra functions by creating properties. Unfortunately, one needs to modify the sitec.prp file for it. Two new properties should be created there like in the picture on the right. Subject C_CG! Metaclass Class!! Property <function>declaration MultiLine ""!! Property <function>definition MultiLine ""! end end The names can be chosen by the user, important is the suffix in the property. When these properties are created in sitec.prp with getinstance as <function>, Rhapsody will offer you these 2 properties (After closing and re-opening the model): C_CG::Class::getInstanceDeclaration and C_CG::Class::getInstanceDefinition These can then be filled with the prototype of the function and the body. In our case the one in the picture on the left. This will generate the getinstance Function exactly as we need it. This enables the use of a real singleton. Mind, however, graphical instances are not possible, you have to instantiate by hand using the getinstance Function. New Singleton Implementation With the help of these 2 stereotypes, and some property tricks, a Gang of Four (GoF) Singleton is possible, without much trouble. The code is very similar to that of the JAVA implementation as you can see on the right. In the Willert Forum, http://www.umlforum.de you can find an implementation model. Feel free to download, use and please... feel free to comment on it!! Techl - TL3 Singleton.pages! 6

My Personal Favorite! More Information There are many books about Design Patterns. Here s a small overview of them. Design Patterns - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Real-Time Design Patterns - Bruce Powel Douglass Design Patterns for Embedded Systems in C - Bruce Powel Douglas Head First Design Patterns - Eric & Elisabeth Freeman The Book pictures were copied from the Amazon website where you can order all books. Read!!! Reading about Design Patterns is helping you to set your mind to using them. If Design Patterns are well-known to you, you will use them regularly with ease!! Techl - TL3 Singleton.pages! 7

More releases of the NEWSLETTER / TECHLETTER you can find on: willert.de/newsletter willert.de/techletter Trainings and Workshops at Willert: EMBEDDED UML START-UP TRAININGS 1. Hands-on exercises based on Rational Rhapsody in C 2. Hands-on exercises based on SPARX Systems Enterprise Architect REQUIREMENT ENGINEERING START-UP TRAININGS 1. Hands-on exercises based on Rational DOORS 2. Hands-on exercises based on Polarion SOFTWARE ARCHITECTURE & DESIGN for Embedded Systems / Workshop Author: WALTER VAN DER HEIDEN Publisher: WILLERT SOFTWARE TOOLS GMBH Hannoversche Straße 21 31675 Bückeburg www.willert.de info@willert.de Tel.: +49 5722 9678-60