Wednesday, 25 February 2015

Different types of Design patterns and the differences between them

Different types of Design patterns and the differences between them:

Design patterns divided into three groups.

Now let us see about each one, and where exactly these are useful:

Creational Design pattern:

These patterns are useful when we want to create the object of the particular class that is the solution for our problem.  I .e when we try to take the decision of the class that we want to access at the run time instead of the compile time.

Creational patterns support the creation of objects in a system. Creational patterns allow objects to be created in a system without having to identify a specific class type in the code, so you do not have to write large, complex code to instantiate an object.

 It does this by having the subclass of the class create the objects. However, this can limit the type or number of objects that can be created within a system.

Thursday, 19 February 2015

Adapter Design pattern with example

Adapter Design pattern with examples:

Adapter Pattern comes under the Structural pattern.
Adapter Design pattern work as the bridge between the two incompatible interfaces. I.e. it converts the interface of a class into another interface that clients expect.

What are the uses of the Adapter pattern?
This pattern is used when we need an existing class, and its interface does not match the one we need.

It is also use when we want to create a reusable class that cooperates with unrelated classes, that is, classes that do not necessarily have compatible interfaces.

And also where we need to use several existing subclasses, but it's impractical to adapt their interface by sub classing every one. An object adapter can adapt the interface of its parent class.

In real time example could be a case of card reader, which acts as an adapter between memory card and a laptop. When we plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.

Let us see the simple example that helps us to understand more:

Tuesday, 17 February 2015

Proxy Design Pattern with example

Proxy Design Pattern with examples:

The proxy pattern is the one of the design pattern. This is anything that signifies the network connection, a large object in the memory etc.

What is Proxy?
Proxy means that the object represents like another object or in other words, it provides the replacement for another object to control access to it.

Where the proxy pattern used?
The proxy patterns are used where multiple copies of the objects exist. For example there is one complex class needs to be used in many places, using the multiple proxy objects we can access the single complex class object reference. That is all the proxy class objects are forwarded to the one complex class object. The memory for the complex class object gets deleted after all the proxy objects are out of scope.
proxy design pattern

Monday, 2 February 2015

How to print the Floyd's triangle in C? Explain with examples.

Please explain how to print the Floyd's triangle in C with examples:

This is also one of the basic interview questions for the fresher’s.

This  is a right-angled triangular array of natural numbers, used in computer science education. This  Floyd's triangle is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner

2 3

4 5 6

7 8 9 10

11 12 13 14 15

Here is the example program to print the Floyd's triangle in C.

Pattern printing in C with examples

Pattern printing in C with examples:





The below code helps to print the above format

Get the maximum occurrence of the character in the string with example in C/C++

Get the maximum occurrence of the character in the string with example in C/Cpp:

Here Input string = “test string for testing”

Constructing the character count array from the input string.

  iCount['e'] = 2
  iCount['s'] = 3
  iCount['t'] = 5
  iCount['r'] = 2
  iCount['i'] = 2
  iCount['f'] = 1
  iCount['o'] = 1
  iCount['f'] = 1
  iCount['n'] = 2
  iCount['g'] = 1

In the above array we can get the maximum number is ‘5’ that is ‘t’