java design patterns and basic oop concepts
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 etcTRANSCRIPT
What is Java?
Programming
Language FastSecureReliable
What is Java?
Programming Language
Fast
Secure
Reliable
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
The Way Java Works :Objective : Write an Application and make it run on whether devices your friends have.
The JAVA BUZZWORDS :
SimpleSecurePortableDynamicDistributedObject-orientedRobust(Consistent)MultithreadedArchitecture neutralHigh Performance
Major OOP Principles :
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction:
Manages Complexity(Generalization)
Hierarchal Classification
Makes Java more Understandable
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
Inheritance :
Process of acquiring the properties of another object
In absence of inheritance, each object needs to define all characteristics
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
Interface :
It is an Collection of Abstract Methods
It can contain any no. of methods
How do it do this???
HOW ?
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
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.
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
Factory Pattern
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.
Factory Pattern
Super Class
SubClass 3Sub Class 2Sub Class 1
Factory Super ClassLogic
Factory Pattern
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
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)
Adapter Pattern
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.
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.
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?
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.
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.
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.
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.
Proxy Design Pattern
Client
GetATMState<interface>
+getATMState(): ATMState+getCashINMachine():int
ATM Proxy
+getATMState(): ATMState+getCashINMachine():int
ATM Machine
+getATMState(): ATMState
+getCashINMachine():int
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!!