.NET Hawa

Design Patterns – I (Introduction)

Authoring software requires a set of rules and methodologies to take your idea to center point without any rework for future enhancements.  In Software Engineering, we have a set of principles stated by Robert Martin called SOLID principle.   SOLID word has principles as Single Responsibility, Open Close, Liskov’s Substitution, Interface Segregation and Dependency Inversion. Let me explain you about SOLID as one word

SOLID Read the rest of this entry »

2 Comments »

Interview With Garbage Collector

Today we have a guest who will carry the job of shredding all unwatend objects while our applications or our Operating Systems are running i.e., Grabage Collector. Let’s Check with him what he can do….

Tech: Hi Sir, how are you ?

GC: I am doing good I hope….

Tech: Sir we have set of questions about to know about you? Read the rest of this entry »

Leave a comment »

Mutable and Immutable objects.

While Learning a concept, my strong belif is bottom-up approch is better than top-down approch which gives clear idea. First I will start with Immutable type objects. An Immutable type object means the object which cannot change it’s state once it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. Immutable objects are often useful because they are inherently thread safe.

Immutable objects are sole property of OOP Concepts.Before the advent of OOP, values held in program variables whose content never changed were known as ‘constants’ to differentiate them from variables that could be altered during execution. Examples might include conversion factors from kilogram weights to pounds or the value of Pi to several decimal places. In most object-oriented languages, objects can be referred to using references. Some examples of such languages are Java, C++, C#, VB.NET, and many scripting languages, such as Python and Ruby. In this case, it matters whether the state of an object can vary when objects are shared via references. If an object is known to be immutable, it can be copied simply by making a copy of a reference to it instead of copying the entire object. Because a reference (typically only the size of a pointer) is usually much smaller than the object itself, this results in memory savings and a boost in execution speed.

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

Some of the Examples in different langaugaes:

Java

A classic example of an immutable object is an instance of the Java String class.
String s = “ABC”;
s.toLowerCase();

The method toLowerCase() will not change the data “ABC” that s contains. Instead, a new String object is instantiated and given the data “abc” during its construction. A reference to this String object is returned by the toLowerCase() method. To make the String s contain the data “abc”, a different approach is needed.

s = s.toLowerCase();

Now the String s references a new String object that contains “abc”. There is nothing in the syntax of the declaration of the class String that enforces it as immutable; rather, none of the String class’s methods ever affect the data that a String object contains, thus making it immutable.
By default, fields and local variables are mutable. They can be made immutable using the final keyword.

final int j = 42;
j = 43; // does not compile

C#:

Best example for Immutable in C# is String Class.

C# Supports Immutability with two key words const and readonly. They are used by the C# compiler to ensure that the state of a field won’t be changed once an object is created.

Mutable Objects:

Mutable Objects are complety opposite to immutable objects where we can modified any where.

Leave a comment »