Search This Blog

Wednesday 11 January 2017

Design Patterns by Type

Let us try to understand Design Patterns by Type
Creational Patterns
Creational patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
  • Abstract Factory Pattern : The abstract factory pattern provides a way for creating families of related or dependent objects without specifying their concrete classes.
  • Builder pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations
  • Factory Method Pattern Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.
  • Prototype Pattern creates objects by cloning an existing object. Create new objects by copying this prototype.
  • Singleton Pattern restricts object creation for a class to only one instance. Ensure a class has only one instance, and provide a global point of access to it.
Structural Patterns
Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
  • Adapter Pattern allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  • Bridge Pattern decouples an abstraction from its implementation so that the two can vary independently.
  • Composite Pattern composes zero-or-more similar objects so that they can be manipulated as one object.
  • Decorator Pattern dynamically adds/overrides behaviour in an existing method of an object.
  • Facade Pattern provides a simplified interface to a large body of code.
  • Flyweight Pattern reduces the cost of creating and manipulating a large number of similar objects.
  • Proxy Pattern provides a placeholder for another object to control access, reduce cost, and reduce complexity.
 Behavioral Pattern
These design patterns are specifically concerned with communication between objects
  • Chain of responsibility Pattern delegates commands to a chain of processing objects.
  • Command Pattern  creates objects which encapsulate actions and parameters.
  • Interpreter Pattern  implements a specialized language.
  • Iterator Pattern  accesses the elements of an object sequentially without exposing its underlying representation.
  • Mediator Pattern  allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
  • Memento Pattern  provides the ability to restore an object to its previous state (undo).
  • Observer Pattern  is a publish/subscribe pattern which allows a number of observer objects to see an event.
  • State Pattern  allows an object to alter its behavior when its internal state changes.
  • Strategy Pattern  allows one of a family of algorithms to be selected on-the-fly at runtime.
  • Template Pattern  method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
  • Visitor Pattern  separates an algorithm from an object structure by moving the hierarchy of methods into one object.
Design Pattern By Types
Design Patterns By Types

Tuesday 10 January 2017

What is a Design Pattern?

Christopher Alexander  says, "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

A pattern has four essential elements:
  •  Pattern Name : is used to describe a design problem, its solutions, and consequences in a word or two. Naming a pattern increases our design vocabulary. It makes it easier to think about designs and convey them to others.
  • Problem : describes when to apply the pattern. It explains the problem and its context. It might describe specific design problems.
  • Solution : describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, since a pattern is like a template which can be applied in many different situations.
  •  Consequences : are the results and trade-offs of applying the pattern.



A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design.
The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities.
Each design pattern focuses on a particular object-oriented design problem or issue.

Organizing Design Patterns
Design patterns vary in their granularity and level of abstraction. Because there are many design patterns, we need a way to organize them.
The classification helps us learn the patterns faster.


We classify design patterns by two criteria.

1.    Purpose : reflects what a pattern does. 

Patterns can have either creational, structural, or behavioral purpose. Creational patterns concern the process of object creation. 

Structural patterns deal with the composition of classes or objects. 

Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility.


2.     Scope :  specifies whether the pattern applies primarily to classes or to objects. 

Class patterns deal with relationships between classes and their subclasses. These relationships are established through Inheritance, so they are static—fixed at compile-time. 

Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.




Design Pattern
Design Pattern






Friday 6 January 2017

Differences between Java EE and Java SE

Java technology is both a programming language and a platform. The Java programming language is a high-level object-oriented language that has a particular syntax and style. A Java platform is an environment in which Java programming language applications run.

The Java Programming Language Platforms
There are four platforms of the Java programming language:
·         Java Platform, Standard Edition (Java SE)
·         Java Platform, Enterprise Edition (Java EE)
·         Java Platform, Micro Edition (Java ME)
·         JavaFX

All Java platforms consist of a Java Virtual Machine (JVM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular platform, that is used to run Java applications. An API is like a library that is used to create other software components or applications. Each Java platform provides a virtual machine and an API, and this allows applications written for that platform to run on any compatible system with all the advantages of the Java programming language.

Java SE
Java SE's API provides the core functionality of the Java programming language. Java SE defines the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.
In addition to the core API, the Java SE platform consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits commonly used in Java technology applications.

Java EE
The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.

Java ME
The Java ME platform provides an API and a small-footprint virtual machine for running Java programming language applications on small devices, like mobile phones. The API is a subset of the Java SE API, along with special class libraries useful for small device application development. Java ME applications are often clients of Java EE platform services.

JavaFX
JavaFX is a platform for creating rich internet applications using a lightweight user-interface API. JavaFX applications use hardware-accelerated graphics and media engines to take advantage of higher-performance clients and a modern look-and-feel as well as high-level APIs for connecting to networked data sources. JavaFX applications may be clients of Java EE platform services.

Java Programming Language Platforms
Java Programming Language Platforms

Wednesday 4 January 2017

Variable argument or varargs in Java

Variable argument or varargs in Java 

Variable argument or varargs in Java allows you to write more flexible methods which can accept as many argument as you need. variable arguments or varargs were added in Java 1.5 Variable arguments is a  useful for a developer. Some time we have a scenario that one method can take variable number of argument  and now with varargs from language makes it much easier.

Variable arguments before Java 1.5 
Prior to Java 1.5 if a Java programmer needs to send multiple arguments to a method, they mainly have two choices to :

1. Either overload the method.
2. Take an array or Collection and pass the no of argument wrapped in array or Collection like List, Set or Map.

The real challenge for a developer comes when the developer is not aware about the number of arguments to handle and how many overloaded methods will be created. This will result in long and clumsy code, which again can be error prone if the developer has missed including the method with specific number of argument in the code.
This is where in varargs comes into picture.

varargs or variable arguments makes it possible for the developer  to call one method with variable number of argument; means define only one method and call that method with zero or more argument.



Syntax:
             type … variable Name.

Ellipses stands for variable argument java treats variable argument as an array of same data type. 3 dots is used to denote variable argument in a method and if there are more than one parameter, varargs arguments must be last, as better listed below

Some points which should be taken care when use varargs:
  1.       Ellipse can be used once in method parameter list.
  2.       Ellipse with type must be used in parameter list at the end of the method



Example to illustrate Varargs
public class Example01 {
      public static int add(int ...var){
            int sum=0;
            for(int i:var)
                  sum+=i;
     
      return sum;
}
      public static void main(String[] args) {
            System.out.println("Sum of 1 and 2 is = " +add(1,2));
            System.out.println("Sum of 1, 2 and 3 is = " + add(1,2,3));
            System.out.println("Sum of 1, 2, 3 and 4 is = "+add(1,2,3,4));
      }
}
Output of the Code

Variable argument or varargs in Java
Variable argument or varargs in Java




Monday 2 January 2017

Understanding Object and Class

The below post explains Object and Class
What is an Object?

An object represents an entity, either physical, conceptual or software.
Ø  Physical – Car or a Person
Ø  Conceptual – Chemical Process
Ø  Software – Bill Number 101, Purchase Order SO01

 An object is an entity with a well-defined boundary and identity that encapsulates its state & behaviour:
Ø  Identity - a name
Ø  State - determined by the values of its attributes
Ø  Behaviour - determined by how the object acts or reacts to requests (messages) from other objects

Object Has State

 The state of an object is one of the possible conditions in which an object may exist
 The state of an object normally changes over time
 E.g.  Rahul is an object of class Employee. The Rahul object has state:
-        Name= Rahul
-        Employee Id=123
-        Hire date=02/02/2013

Object has Behaviour

 Behaviour determines how an object acts and reacts to requests from other objects.

 The visible behaviour of an object is modelled by the set of messages it can respond to (operations the object can perform)
 Employee Rahul’s Behaviour:
-        Submit TimeSheet()
-        Complete TaskAssigned()

 The behaviour may change the state of an object.

Object has Identity

 Each object has a unique identity, even if the state is identical to that of another object.
 E.g. Employee Rahul is from XX City. Even if there is another employee with the same name – Rahul working in same city or another city they both are distinct objects

What is a Class?

 A class is a description of a set of objects that share the same attributes, operations, relationships and semantics.
Ø  An object is an instance of class

 A class is an abstraction in that it
Ø  Emphasizes relevant characteristics
Ø  Suppresses other characteristics


Relationship between Classes & Objects

Ø  A class is an abstract definition of an object.
Ø  It defines the structure & behaviour of each object in the class
Ø  It serves as a template / blue print for creating objects

Attributes of a Class

Ø  An attribute is a named property of a class that describes a range of values that instances of the property may hold
Ø  A class may have any number of attributes or no attributes at all.
Ø  An attribute has a type, which tells us what kind of attribute it is.
Ø  Typically attributes are integer, boolean, varchar etc. These are called primitive types.
Ø  Primitive types can be specific for a certain programming language.

Operations of a Class

Ø  An operation is the implementation of a service that can be requested from any object of the class to affect behaviour
Ø  A class may have any number of operations or none at all
Ø  The operations in a class describe what class can do
Ø  The operation is described with a return-type, name, zero and more parameters. This is known as signature of an operation
Often, but not always, invoking an operation on an object changes the object’s data or state 

Original Post At : http://learnerpoint.in/2017/01/02/understanding-object-and-class/