32

JAVA design patterns and Basic OOp concepts

Embed Size (px)

DESCRIPTION

This Presentation is about java design patterns. I have covered some OOPs concepts as well: Polymorphism, Interface, Inheritance, abstraction etc. And the Main Topic Covers: Factory Design Patterns,Observer Design Patterns,Proxy Design Patterns,Adapter Design Patterns,MVC Design Patterns etc

Citation preview

Page 1: JAVA design patterns and Basic OOp concepts
Page 2: JAVA design patterns and Basic OOp concepts

What is Java?

Programming

Language FastSecureReliable

Page 3: JAVA design patterns and Basic OOp concepts

What is Java?

Programming Language

Fast

Secure

Reliable

Page 4: JAVA design patterns and Basic OOp concepts

Some Facts about JAVA:

Developed by JAMES GOSLING at SUN MICROSYSTEMS

Related to two languages C and C++

HOW ?JAVA derives its Syntax from C and,

Many concepts based on OOPs were influenced by C++

Initially JAVA was named as “Oak” But was renamed by “JAVA” in 19953977 classes

Page 5: JAVA design patterns and Basic OOp concepts

The Way Java Works :Objective : Write an Application and make it run on whether devices your friends have.

Page 6: JAVA design patterns and Basic OOp concepts

The JAVA BUZZWORDS :

SimpleSecurePortableDynamicDistributedObject-orientedRobust(Consistent)MultithreadedArchitecture neutralHigh Performance

Page 7: JAVA design patterns and Basic OOp concepts

Major OOP Principles :

Abstraction

Encapsulation

Inheritance

Polymorphism

Page 8: JAVA design patterns and Basic OOp concepts

Abstraction:

Manages Complexity(Generalization)

Hierarchal Classification

Makes Java more Understandable

Page 9: JAVA design patterns and Basic OOp concepts

Encapsulation:

Binds Code and data Together

Keeps it safe from outside interference and Misuse

It works through Classes. A

AB

C

CD

C is accessible with in this Block

Page 10: JAVA design patterns and Basic OOp concepts

Inheritance :

Process of acquiring the properties of another object

In absence of inheritance, each object needs to define all characteristics

Page 11: JAVA design patterns and Basic OOp concepts

Polymorphism :

An Ability of an object to acquire more than one form

For example:

public interface Vegetarian{ } public class Animal{ } public class Deer extends Animal implements Vegetarian{ }

A Deer IS-A AnimalA Deer IS-A VegetarianA Deer IS-A DeerA Deer IS-A Object

Page 12: JAVA design patterns and Basic OOp concepts

Interface :

It is an Collection of Abstract Methods

It can contain any no. of methods

How do it do this???

HOW ?

Page 13: JAVA design patterns and Basic OOp concepts

Lets Design Some Patterns

General solution to programmers problems faced while designing software

Increase the Code Maintainability

GOF: Erich Gamma, Richard helm, Ralph Johnsons and John vlissides

Helps in Reusing the codes

Time saving and Cost reduction

Page 14: JAVA design patterns and Basic OOp concepts

Types Of Patterns

Creational Pattern:

Way to create Objects while hiding the Creational Logic

Structural Pattern:

Concerns class and object composition

Behavioral pattern:

Referred to communication between objects

J2EE:

Presentation tier.

Page 15: JAVA design patterns and Basic OOp concepts

Classification Of Patterns

Creational pattern:1. Factory Pattern2. Singleton Pattern3. Abstract Factory Pattern4. Builder Pattern

5. Prototype Pattern

Structural Patterns:1. Adapter Pattern 2. Composite Pattern3. Proxy Pattern4. Flyweight Pattern5. Façade Pattern6. Bridge Pattern7. Decorator PatternBehavioral Pattern:

1. Template Method Pattern2. Mediator Pattern3. Chain of Responsibility Pattern4. Observer Pattern5. Strategy Pattern6. Command Pattern

7. State Pattern8. Visitor Pattern9. Iterator Pattern10. Memento Pattern

Page 16: JAVA design patterns and Basic OOp concepts

Factory Pattern

Page 17: JAVA design patterns and Basic OOp concepts

Factory Pattern

Comes Under the creational Pattern

Create object without Exposing the Creational Logic

used to instantiate an object from one among a set of classes based on a logic

Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one or more arguments. This method is our factory method.

Page 18: JAVA design patterns and Basic OOp concepts

Factory Pattern

Super Class

SubClass 3Sub Class 2Sub Class 1

Factory Super ClassLogic

Page 19: JAVA design patterns and Basic OOp concepts

Factory Pattern

Page 20: JAVA design patterns and Basic OOp concepts

Adapter Pattern

Allows you to use two Incompatible Interfaces to work together

Used when the client expects a(target) interface

Adapter Class allows the use of available Interface and the target interface

Any class can work together as long as the Adapter solves the issue that All classes must implements every method defined by the shared interface

Page 21: JAVA design patterns and Basic OOp concepts

Adapter Pattern

ClientEnemy Attacker

<Interface>

Fire weapon():voidDrive Forward():voidAssign driver():void

Enemy Robot Adapter

Fire weapon():voidDrive Forward():voidAssign driver():void

Enemy Robot Smash with hands():void

walk Forward():voidReact to human():void

Enemy Robot

Enemy Attacker(Target)

Enemy Robot Adapter(Adaptor)

Enemy Robot(Adaptee)

Page 22: JAVA design patterns and Basic OOp concepts

Adapter Pattern

Page 23: JAVA design patterns and Basic OOp concepts

Observer Pattern

In observer design pattern multiple observer objects registers with a subject for change notification. When the state of subject changes, it notifies the observers. Objects that listen or watch for change are called observers and the object that is being watched for is called subject.

Pattern involved is also called as publish-subscribe pattern. Model view controller (MVC) architecture’s core uses the observer design pattern.

Page 24: JAVA design patterns and Basic OOp concepts

Observer Pattern

Subject provides interface for observers to register and unregister themselves with the subject.

Subject knows who its subscribers are.

Multiple observers can subscribe for notifications.

Subject publishes the notifications.

Subject just sends the notification saying the state has changed. It does not pass any state information.

Once the notification is received from subject, observers call the subject and get data that is changed.

Page 25: JAVA design patterns and Basic OOp concepts

Observer Pattern

Consider an excel sheet, data is shown to the user in different views. Generally data is shown in grid cells and as required different graphs, charts can be created for same data. Underlying data is same and when that data (subject) state changes all the different view are updated.

When multiple objects depend on state of one object and all these multiple objects should be in sync with the state of this one object then observer design pattern is the right choice to apply.

When?

Page 26: JAVA design patterns and Basic OOp concepts

Observer Pattern

Let us take a blog and subscriber example for observer design pattern sample implementation. Assume that there is a blog and users register to that blog for update. When a new article is posted in the blog, it will send update to the registered users saying a new article is posted. Then the user will access the blog and read the new article posted. In this example, blog is the subject and user is the observer.

Page 27: JAVA design patterns and Basic OOp concepts

Proxy Design Pattern

Proxy means ‘in place of’. In attendance roll call, we give proxy for our friends in college right? ‘Representing’ or ‘in place of’ or ‘on behalf of’ are literal meanings of proxy and that directly explains proxy design pattern.

Page 28: JAVA design patterns and Basic OOp concepts

Proxy Design Pattern

Remote Proxy – Represents an object locally which belongs to a different address space. Think of an ATM implementation, it will hold proxy objects for bank information that exists in the remote server.

Virtual Proxy – In place of a complex or heavy object use a skeleton representation.

Protection Proxy –working on a MNC? If so, you might be well aware of the proxy server that provides you internet. Saying more than provides, the right word is censors internet.

Smart Reference – Just we keep a link/reference to the real object a kind of pointer.

Page 29: JAVA design patterns and Basic OOp concepts

Proxy Design Pattern

Provide a class which will limit access to another class

You may do this for Security reasons, because an Object is intensive to Create, or is accessed from a remote location.

Page 30: JAVA design patterns and Basic OOp concepts

Proxy Design Pattern

Client

GetATMState<interface>

+getATMState(): ATMState+getCashINMachine():int

ATM Proxy

+getATMState(): ATMState+getCashINMachine():int

ATM Machine

+getATMState(): ATMState

+getCashINMachine():int

Page 31: JAVA design patterns and Basic OOp concepts

MVC Design Pattern

Completely Separates the Calculation and Interface from each other

Model: Data and Model Used to Work with it

View: The Interface

Controller: Coordinates interactions between the view and Model

My Favorite One!!

Page 32: JAVA design patterns and Basic OOp concepts

Rahul Malhotra

[email protected]+91-74041-37709www.facebook.com/btiworld14