+ All Categories
Home > Documents > Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu -...

Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu -...

Date post: 31-Jan-2018
Category:
Upload: dinhliem
View: 226 times
Download: 0 times
Share this document with a friend
41
Curs 2 Storage types Tablouri Clase Java Membri, Constructori,... Membri statici Singleton Pattern
Transcript
Page 1: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2

Storage types Tablouri Clase Java –Membri, Constructori,...

Membri statici – Singleton Pattern

Page 2: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Where storage lives

Registers: (fastest storage) are place different inside the processor The number of registers is severely limited You don’t have direct control

The stack: lives in the general RAM area has direct support from the processor via its stack pointer The stack pointer is moved down to create new memory and

moved up to release that memory extremely fast and efficient way to allocate storage object references and primitive values

The heap: a general-purpose pool of memory (also in the RAM area) important flexibility in using storage on the heap it takes more time to allocate heap storage than it does for the stack Java objects are placed on the heap

Static storage: “in a fixed location” (it’s also in RAM). static data are available for the entire time a program is running

Constant storage: constants are often placed directly in the program code

Non-RAM storage: streamed objects, persistent objects

2 Curs 2 POO

-Virginia Niculescu -

Page 3: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Memory Segments

The Von Neumann architecture is defined by the fact that both the machine

instructions and data are stored in the same memory space.

This fact necessitates dividing memory into

text segment, and

data segment.

In modern operating systems, there are more than just these two segments.

Most systems use a single text segment, but actually use up to three data

segments, depending on the storage class of the data being stored there.

3 Curs 2 POO

-Virginia Niculescu -

Page 4: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Code & Data segments

The code segment, also known as text segment contains the machine

instructions of the program.

The data segment contains the static data of the program, i.e. the variables

that exist throughout program execution.

Global variables in a C or C++ program are static, as are variables

declared as static in C, C++, or Java.

4 Curs 2 POO

-Virginia Niculescu -

Page 5: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Stack DrawSquare()

{

[...]

DrawLine ();

[...]

}

5 Curs 2 POO

-Virginia Niculescu -

Page 6: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Size of the segments

The heap segment is a pool of memory used for dynamically allocated memory, such as with malloc() in C or new in C++ and Java.

The size of the text and data segments are known as soon as compilation or assembly is completed.

The stack and heap segments, on the other hand, grow and shrink during program execution.

For this reason, they tend to be configured such that they grow toward each other.

This way, they each have a fixed starting point (one adjacent to the text and data segments, and the other at one end of the entire memory space), and the boundary between them is flexible.

Both can grow until all available memory is used.

6 Curs 2 POO

-Virginia Niculescu -

Page 7: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Reprezentare

+---------------+

| |

| text | (fixed size)

| |

+----------------+

| |

| data | (fixed size)

| |

+----------------+

| stack | | growth

+----------------+ V

| |

| free |

| |

+----------------+ ^

| heap | | growth

+----------------+ http://www.cs.uwm.edu/classes/cs315/Baco

n/Lecture/HTML/ch10s04.html

7 Curs 2 POO

-Virginia Niculescu -

Page 8: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

JAVA

8 Curs 2 POO

-Virginia Niculescu -

Page 9: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Tipuri de date referinta

Tipurile Java neprimitive sunt obiectele si tablourile

( tablourile sunt de fapt obiecte).

Tipurile Java neprimitive sunt numite in general “tipuri referinta" pentru ca

se utilizeaza prin “ referinta”

(adresa obiectului sau tabloului este stocata intr-o variabila, transmisa

metodelor, etc.).

Tipurile primitive sunt “utilizate prin valoare“

9 Curs 2 POO

-Virginia Niculescu -

Page 10: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Nu sunt pointeri in Java!

Referentierea si dereferentierea obiectelor se face automat.

Java nu permite manipularea pointerilor sau a adreselor de memorie:

NU este permisa transformarea prin cast a unei referinte la obiect sau la

tablou la intregi si vice-versa.

NU este permisa aritmetica cu adrese.

NU este permisa calcularea marimii reprezentarii in octeti pentru

nici un tip primitiv sau obiect.

10 Curs 2 POO

-Virginia Niculescu -

Page 11: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

null

Valoarea implicita pentru variabilele de tip referinta este null.

null este un cuvant rezervat si corespunde unei valori care indica faptul

ca variabila nu refera nici un obiect.

(in C NULL este doar o constanta egala cu 0)

null nu poate fi transformat prin cast la nici un tip primitiv. Nu trebuie sa

fie considerat a fi egal cu zero desi ar putea fi implementat in acest fel.

11 Curs 2 POO

-Virginia Niculescu -

Page 12: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Exemplu

Complex p, q;

p = new Complex();

// p refers to a Complex object.

q = p;

// q refers to the same object of type Complex.

p.setReal(10.2);

// A change to the object through p...

double r = q.getReal();

// ...is also visible through q. // r now contains 10.2

int i = 3;

// i contains the value 3.

int j = i;

// j contains a copy of the value in i.

i = 2;

// Changing i doesn't change j.

// Now, i == 2 and j == 3.

12 Curs 2 POO

-Virginia Niculescu -

Page 13: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Exemplu –swap - eronat!

public void swap( Object a, Object b)

{

Object temp = a;

a = b;

b = temp;

}

Parametrii se transmit prin valoare!

13 Curs 2 POO

-Virginia Niculescu -

Page 14: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Copierea Obiectelor

Complex a = new Complex(1,1);

Complex b = new Complex();

a = b;

variabila a contine o referinta la obiectul pe care b il refera. Obiectul pe care a il referea este pierdut.

Pentru a copia un obiect se foloseste metoda clone():

Vector b = new Vector();

Vector c = b.clone();

Pentru a se putea folosi metoda clone() clasa respectiva trebuie sa implementeze interfata Cloneable.

14 Curs 2 POO

-Virginia Niculescu -

Page 15: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Verificarea egalitatii

Operatorul == testeaza daca doua variabile refera acelasi obiect, si nu daca doua obiecte au

aceeasi stare.

Metoda equals() se poate folosi pentru a testa egalitatea.

Daca o clasa are atribute de tip referinta, metoda equals() ar trebui suprascrisa!

15 Curs 2 POO

-Virginia Niculescu -

Page 16: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Tablouri

Tablourile se utilizeaza folosind referinte.

Sunt create dinamic (new); ca si obiectele: -> clasa Array

Garbage collecter-ul elibereaza memoria corespunzatoare lor atunci cand nu mai sunt referite.

byte octet_buffer[] = new byte[1024];

Complex tc[] = new Complex[10];

Elementele unui tablou se initializeaza cu valoarea implicita a tipului lui.

(deci pentru obiecte = null)

=>Creare unui tablou cu elemente de tip obiect nu implica si crearea obiectelor constitutive.

for (int i=0; i<10; i++) tc[i] = new Complex(i, i);

16 Curs 2 POO

-Virginia Niculescu -

Page 17: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Tablouri(2)

creare si initializare tablou

int lookup_table[] = {1, 2, 4, 8, 16, 32, 64, 128};

creare anonima -> parametrii

Menu m = createMenu("File",

new String[] {"Open...", "Save", "Quit" });

17 Curs 2 POO

-Virginia Niculescu -

Page 18: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Tablouri multidimensionale

se implementeaza ca tablouri de tablouri ca si in C

byte TwoDimArray[][] = new byte[256][16];

int threeD[][][] = new int[10][][];

double temperature_data[][][] = new double[100][][10]; // illegal

String param_info[][] = { {"foreground", "Color", "foreground color"}, {"background", "Color", "background color"}, {"message", "String", "the banner to display"}};

18 Curs 2 POO

-Virginia Niculescu -

Page 19: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Tablouri multidimensionale nerectangulare

short triangle[][] = new short[10][];

// Tablou uni- dimensional cu elemente de tip tablou

for(int i = 0; i < triangle.length; i++) {

// pentru fiecare element al tabloului

triangle[i] = new short[i+1];

// se aloca un nou tablou

for(int j=0; j < i+1; j++)

// fiecare element al noului tablou

triangle[i][j] = (short) i + j;

// se initializeaza cu o valoare

static int[][] twodim =

{{1, 2}, {3, 4, 5}, {5, 6, 7, 8}};

19 Curs 2 POO

-Virginia Niculescu -

Page 20: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Exemple public class ex_tablou {

public static void main(String[] args) {

int[][] multi = new int[][]{

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

};

int note[][] = new int[3][5];

for (int i=0; i<3;i++)

for (int j=0; j<5;j++)

note[i][j]=10;

for (int[] e: note ){

for (int ee: e )

System.out.print(ee+" ");

System.out.println();

}

}

20 Curs 2 POO

-Virginia Niculescu -

Page 21: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Variabile si parametrii de tip tablou

C vs. Java

void reverse(char strbuf[], int buffer_size) { char buffer[500];

...}

void reverse(char[] strbuf, int buffer_size) { char[] buffer = new char[500];

...}

lungimea unui tablou nu face parte din tip ca in C

String[] strings;

// this variable can refer to any String array

strings = new String[10];

// one that contains 10 Strings

strings = new String[20];

// or one that contains 20.

21 Curs 2 POO

-Virginia Niculescu -

Page 22: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Clase

- atribute/campuri

- metode

Modificatori de acces:

- private

- public

- protected

- “friendly” (fara specificator)

22 Curs 2 POO

-Virginia Niculescu -

Page 23: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Constructori

Metode speciale

rol esential in crearea obiectelor!

cel putin un constructor la fiecare clasa!

acelasi nume cu clasa!

nu returneaza nimic!

23 Curs 2 POO

-Virginia Niculescu -

Page 24: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

UML

Curs 2 POO

-Virginia Niculescu -

24

UML limbaj (standard) folosit pentru

specificare

vizualizare

constructie

documentare

sistemelor software

Page 25: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2 POO

-Virginia Niculescu -

25

UML

se pot specifica entitatile/conceptele care intervin intr-o aplicatie orientata-

obiect, precum si relatiile dintre ele.

reprezentare grafica

private “-”

public “+”

Page 26: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2 POO

-Virginia Niculescu -

26

S

-n: int

-e: E[]

+S( )

+card( ): int

+add( x: E)

+belong( x: E): int

+getElem(i:int):E

Exemplu: multime de elemente de tip E

Varianta 1 (nu e riguros corecta – dar acceptata)

Java dependenta

Page 27: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2 POO

-Virginia Niculescu -

27

S

-n: Integer

-e : E[*]

+S ( ) <<constructor>>

+card ( ): Integer

+add ( x: E)

+belong (x: E): Integer

+ getElem(i: Integer):E

Variant 2 (corecta)

(tipuri UML )

Page 28: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2 POO

-Virginia Niculescu -

28

Tipuri UML

Integer

Real

Character

String

Boolean

Page 29: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Curs 2 POO

-Virginia Niculescu -

29

Diagrama de clasa

Page 30: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Strings

Sunt obiecte, instante ale clasei java.lang.String or java.lang.StringBuffer

Obiectele String sunt imutabile (nemodificabile)

Obiectele StringBuffer sunt mutabile (modificabile)

Cateva metode length charAt equals compareTo indexOf lastIndexOf substring

30 Curs 2 POO

-Virginia Niculescu -

Page 31: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Exemplu

Curs 2 POO

-Virginia Niculescu -

31

clasa Circle

Page 32: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Clasa cu constructor implicit

public class Circle {

public double x, y;

// The coordinates of the center

public double r;

// The radius

// Methods that return the // circumference and area of the circle

public double circumference() {

return 2 * 3.14159 * r;

}

public double area() {

return 3.14159 * r*r;

}

public static void main(String args[]){

Circle c = new Circle();

}

}

32 32 Curs 2 POO

-Virginia Niculescu -

Page 33: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Constructor

class Circle{

// . . .

public Circle(double x, double y, double r) {

this.x = x;

this.y = y;

this.r = r;

}

public static void main(String args[]){

Circle c = new Circle(10.0,10.0,20);

// Circle c=new Circle(); incorect

}

}

33 Curs 2 POO

-Virginia Niculescu -

Page 34: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Mai multi constructori

public class Circle {

public double x, y, r;

public Circle(double x, double y, double r) {

this.x = x; this.y = y; this.r = r; }

public Circle(double r) {

x = 0.0; y = 0.0; this.r = r; }

public Circle(Circle c) {

x = c.x; y = c.y; r = c.r; }

public Circle() {

// x = 0.0; y = 0.0; r = 1.0; }

public double circumference() {

return 2 * 3.14159 * r; }

public double area() {

return 3.14159 * r*r; }

}

34 34 Curs 2 POO

-Virginia Niculescu -

Page 35: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Apelul unui constructor in alt constructor

public class Circle {

public double x, y, r;

public Circle(double x, double y, double r) {

this.x = x; this.y = y; this.r = r;

}

public Circle(double r) {

this(0.0,0.0,r);

}

public Circle(Circle c) {

this(c.x, c.y, c.r);

}

public Circle() {

// x = 0.0; y = 0.0; r = 1.0;

}

public double circumference() {

return 2 * 3.14159 * r; }

public double area() { return 3.14159 * r*r; }

}

Regula: apelul altui constructor = prima instructiune

35 35 Curs 2 POO

-Virginia Niculescu -

Page 36: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Variabile de tip clasa (statice)

public class Circle {

public double x, y, r;

public static int num_obj;

public Circle(double x, double y, double r) {

this.x = x; this.y = y; this.r = r;

num_obj++;

}

// ...

// cate obiecte au fost create!!!

public static void main(String args[]){

System.out.println(Circle.num_obj);

Circle c = new Circle(10.0,10.0,20);

System.out.println(Circle.num_obj);

}

36 36 Curs 2 POO

-Virginia Niculescu -

Page 37: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Observatii:

1. Datele membru statice nu se memoreaza ca stare a obiectului.

2. Unei date membru statice i se aloca memorie o singura data

indiferent de numarul obiectelor de tipul clasei respective existente

in memorie.

3. Referirea datelor membre statice se face astfel:

NumeClasa.numeDataMembru sau

numeObiect.numeDataMembru.

4. Daca o data membru statica nu este initializata explicit, atunci ea va fi

initializata cu valoarea implicita corespunzatoare tipului sau.

37 37 Curs 2 POO

-Virginia Niculescu -

Page 38: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Initializator static

public class Circle {

static private double sines[] = new double[1000];

static private double cosines[] = new double[1000];

// A static initializer "method" that fills them in.

// Notice the lack of any method declaration!

static {

double x, delta_x; int i;

delta_x = (Math.PI/2)/(1000-1);

for(i = 0, x = 0.0; i < 1000; i++, x += delta_x) {

sines[i] = Math.sin(x);

cosines[i] = Math.cos(x);

}

} // . . .

}

38 38 Curs 2 POO

-Virginia Niculescu -

Page 39: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Metode de tip clasa (statice)

public class Circle {

public double x, y, r;

// An instance method.

public Circle bigger(Circle c) {

if (c.r > r) return c;

else return this;

}

// A class method.

public static Circle bigger(Circle a, Circle b) {

if (a.r > b.r) return a;

else return b;

}

// . . .

public static void main(String args[]){

Circle c1 = new Circle(10.0,10.0,20);

Circle c2=new Circle();

if ( c1.bigger(c2)==c1 )

System.out.println(“c1 is bigger than c2”);

if ( Circle.bigger(c1, c2)==c1 )

System.out.println(“c1 is bigger than c2”);

}

}

39 39 Curs 2 POO

-Virginia Niculescu -

Page 40: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Observatii:

1. Metodele de tip clasa (statice) se declara folosind atributul static in fata.

2. O metoda statica nu poate folosi direct membrii nestatici ai clasei (atribute sau

metode)!

3. Metodele statice pot apela doar alte metode statice si pot referi doar atributele

statice.

4. In implementarea unei metode statice nu se poate folosi cuvantul this.

(... Ce inseamna this?)

40 40 Curs 2 POO

-Virginia Niculescu -

Page 41: Curs 2 - cs.ubbcluj.rovniculescu/didactic/POO/Curs2.pdf · 2 Curs 2 POO -Virginia Niculescu - Memory Segments The Von Neumann architecture is defined by the ...

Singleton pattern

se permite instantierea unui singur obiect pentru o clasa

implementarea se bazeaza pe folosirea

membrilor statici si

constructor privat

class Sunic{

private static Sunic instance;

private Sunic(){}

public static Sunic getInstance(){

if (instance==null) instance = new Sunic();

return instance;

}

}

41 Curs 2 POO

-Virginia Niculescu -


Recommended