Thursday, 30 November 2017

Abstract Factory Design Pattern


Motivation
Modularization is a big issue in today's programming. Programmers all over the world are trying to avoid the idea of adding code to existing classes in order to make them support encapsulating more general information. Take the case of a information manager which manages phone number. Phone numbers have a particular rule on which they get generated depending on areas and countries. If at some point the application should be changed in order to support adding numbers form a new country, the code of the application would have to be changed and it would become more and more complicated.
In order to prevent it, the Abstract Factory design pattern is used. Using this pattern a framework is defined, which produces objects that follow a general pattern and at runtime this factory is paired with any concrete factory to produce objects that follow the pattern of a certain country. In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories).

Intent
Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes.

Where to use
Abstract Factory should be used when:
A system should be configured with one of multiple families of products
A system should be independent of how its products are created, composed and represented
Products from the same family should be used all together, products from different families ahould not be used together and this constraint must be ensured.
Only the product interfaces are revealed, the implementations remains hidden to the clients.

Common Usage
Examples of abstract factories:
java.awt.Toolkit - the abstract superclass of all actual implementations of the Abstract Window Toolkit. Subclasses of Toolkit are used to bind the various components to particular native toolkit implementations(Java AWT).
javax.swing.LookAndFeel - an abstract swing factory to swithct between several look and feel for the components displayed(Java Swing).
java.sql.Connection - an abstract factory which create Statements, PreparedStatements, CallableStatements,... for each database flavor.
Example: Gui Look & Feel in Java

Applicability
Use the Abstract Factory pattern when
1. a system should be independent of how its products are created, composed, and represented.
2. a system should be configured with one of multiple families of products.
3. a family of related product objects is designed to be used together, and you need to enforce this constraint.
4. you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Benefit(s)
1. It isolates concrete classes.
2. It makes exchanging product families easy.
3. It promotes consistency among products.

Disadvantage(s)
1. Supporting new kinds of products is difficult.

Happy Learning.

Factory Design Pattern


Motivation
The Factory Design Pattern is probably the most used design pattern in modern programming languages like Java and C#. It comes in different variants and implementations. If you are searching for it, most likely, you'll find references about the GoF patterns: Factory Method and Abstract Factory.

Intent
creates objects without exposing the instantiation logic to the client.
refers to the newly created object through a common interface

Where to use
Factory pattern should be used when: - a framework delegate the creation of objects derived from a common superclass to the factory - we need flexibility in adding new types of objects that must be created by the class

Common Usage
Along with singleton pattern the factory is one of the most used patterns. Almost any application has some factories. Here are a some examples in java:
- factories providing an xml parser: javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory
- java.net.URLConnection - allows users to decide which protocol to use.

Applicability
Use the Factory Method pattern when 1. a class can't anticipate the class of objects it must create. 2. a class wants its subclasses to specify the objects it creates. 3. classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Benefit(s)
1. Provides hooks for subclasses.
2. Connects parallel class hierarchies.

Disadvantage(s)
A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object. Subclassing is fine when the client has to subclass the Creator class anyway, but otherwise the client now must deal with another point of evolution.


Happy Learning.

Singleton Design Pattern


Motivation
Sometimes it's important to have only one instance for a class. For example, in a system there should be only one window manager (or only a file system or only a print spooler). Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves.
The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

Intent
Ensure that only one instance of a class is created.
Provide a global point of access to the object.

Where to use
Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code. A special care should be taken in multi-threading environments when multiple threads must access the same resources through the same singleton object.

Common Usage
There are many common situations when singleton pattern is used:
- Logger Classes
- Configuration Classes
- Accessing resources in shared mode
- Other design patterns implemented as Singletons: Factories and Abstract Factories, Builder, Prototype

Example: Lazy Singleton in Java, Early Singleton in Java

Applicability
Use the Singleton pattern when 1. there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point. 2. when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Benefit(s)
1. Controlled access to sole instance.
2. Reduced name space.
3. Permits refinement of operations and representation.
4. Permits a variable number of instances.
5. More flexible than class operations.

Happy Learning.

Friday, 8 September 2017

How to Create alerts in Splunk


The Steps include:


  • Open Search page in Search and Reporting Page.
  • Enter your query for which alerts need to be triggered.for example:
         index = os_web sourcetype = custom-prod-up-ext serviceName=SHARP response=failure                  feature=PCI_DE_TOKENIZATION 
  • Select Save as ---->Alert



  • Next Specify Setting,Triggered Condition,Triggered Action.


  • Select Save.

Enjoy Learning.

Tuesday, 29 August 2017

Java program to check unique number


Sample Program:-


 import java.util.*;  
 import java.io.*;  
 /**  
  * Created by Dixit on 29-08-2017.  
  */  
 public class IsUniqueNumber {  
   public static boolean  
   isUniqueUsingHash(String word)  
   {char[] chars = word.toCharArray();  
     Set<Character> set = new HashSet<Character>();  
     for (char c : chars)  
       if (set.contains(c))  
         return false;  
       else {  
         set.add(c);  
       }  
     return true;  
   }  
   public static boolean  
   isUniqueUsingSort(String word)  
   {char[] chars = word.toCharArray();  
     if (chars.length <= 1)  
       return true;  
     Arrays.sort(chars);  
     char temp = chars[0];  
     for (int i = 1; i < chars.length; i++)  
     {if (chars[i] == temp)  
       return false;  
       temp = chars[i];  
     }  
     return true;  
   }  
   public static void main(String[] args)  
       throws IOException  
   {  
     System.out.println(isUniqueUsingHash("1234")? "Unique" : "Not Unique");  
         System.out.println(isUniqueUsingSort("123")? "Unique" : "NotUnique");  
   }  
 }  


Enjoy Coding

Sunday, 20 August 2017

Interesting facts of Java


Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]).
The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suite various types of platforms. Ex: J2EE for Enterprise Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere.
Java is:
  • Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
  • Platform independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.
  • Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java would be easy to master.
  • Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
  • Architectural-neutral: Java compiler generates an architecture-neutral object file format which makes the compiled code to be executable on many processors, with the presence of Java runtime system.
  • Portable: Being architectural-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary which is a POSIX subset.
  • Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.
  • Multithreaded: With Java's multithreaded feature it is possible to write programs that can do many tasks simultaneously. This design feature allows developers to construct smoothly running interactive applications.
  • Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light weight process.
  • High Performance: With the use of Just-In-Time compilers, Java enables high performance.
  • Distributed: Java is designed for the distributed environment of the internet.
  • Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Enjoy Learning.

Monday, 26 June 2017

WHAT TO CHOOSE for Android Game Development:- canvas or OpenGL or LibGDX or Game Engines!!

Android Game Development - WHAT TO CHOOSE

Canvas or OpenGL or LibGDX or Game Engines!!


Developing games on android is not straight forward. You need to be very specific about what you are going to develop, what are the content of it and what are the requirement for it. Game can be developed from all these but everyone has its own limitation.  

As I am a java and android developer and developed some games in java and android with simple logic(Graphics lib. for java and Canvas for android) I had concluded some points.
  • If graphics are heavy than your game FPS may jitter even though you have proper architecture, but can be solved from accurate and efficient algorithm.
  • Rendering too much graphics can be tedious task.
  • There should be proper architecture of everything.
  • Efficient and best algorithm should be there.
  • Application manageability depends on how you develop it.


Canvas


It is a predefined class in android sdk. Android application can draw things on screen using this class.

java.lang.Object 
   ↳android.graphics.Canvas

means Object class is the parent class of canvas class. This class hold the "draw" calls. It can draw anything like rectangle, circle, bitmap image, etc. To draw anything some basic components is needed which are mentioned below:-
  • Paint object(to give color and style to drawing)
  • Bitmap to hold the pixels
  • Canvas to receive draw calls(drawing on Bitmap)
  • Drawing primitive(path, text, etc.)
How to use it please check android developer documentation.

OpenGL


It is the environment to develop 2D and 3D graphics. It gives the high visual quality and performance. It is cross platform environment.


OpenGl can process image data as well as geometric data.


LibGDX


It is the free game development framework, also it is open source. It is written in Java programming language with some components written in C and C++ to increase the performance. It is the cross platform framework. It can be used desktop application and also for android also.

However this framework is written in java, so the compiled bytecode is language dependent and can be used in any JVM. It main focus was to ease the simplicity and increase the performance.

Game Engine 


It is the tool for fast development of the 2D and 3D games. It is the software framework to develop the mobile and desktop games. The core functionality of the game engine are:-
  • rendering(for 2D and 3D graphics)
  • Physics(collision detection)
  • Sounds
  • Artificial Intelligence
  • Networking
  • Memory management and many more.....
It makes the portability and reusability of the code. There are so many game engines in the market right now, one can choose as per its convenience. Some popular Game Engines are:-
  • UNITY
  • UNREAL
  • RENDERWARE
  • X-RAY

WHAT TO CHOOSE!!


The choice is based on some factors like how much time is available, availability of resources, quality of product and many more. after considering all these points one should choose accordingly.

But my personal advice would be Game Engine as you can do so much in less time and no heavy programming skill is needed. Not because of less programming but the quality of content is far more good and manageability is easy. If one wants to develop the game from basic logic, one can to understand how things happen but if they want to build heavy game with simple libraries it will be surely very difficult. So, it is better to choose game engine.

I personally prefer the UNITY game engine as it is easy to learn and implement. Others are also good but I hadn't used them. Tutorials for all these are available online.


GAME ENGINE would be good option.