advanced programming
DESCRIPTION
Advanced Programming. Rabie A. Ramadan [email protected] 7. JAVA Naming and Directory Interface (JNDI) http:// java.sun.com /products/ jndi /tutorial/ Java Design Patterns . Topics to be Covered . - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/2.jpg)
Topics to be Covered
2
JAVA Naming and Directory Interface (JNDI)http://java.sun.com/products/jndi/tutorial/
Java Design Patterns
![Page 3: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/3.jpg)
JAVA Naming and Directory Interface (JNDI)
3
A naming service associates names with objects. Thus, you can look up an object by its name.
A directory service associates names and attributes with objects. Thus, you not only can look up an object by its name but also get the object's attributes or search for the object based on its attributes.
Common Naming/directory services:• DNS• RMI Registry• CORBA naming service• Lightweight Directory Access Protocol (LDAP)• OS File system.• Windows registry• Windows Active Directory
JNDI provides a unified approach (API) to access any naming/directory service.
![Page 4: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/4.jpg)
JNDI Overview
4
JNDI is an Application Programming Interface (API) that provides naming and directory functionality to JAVA applications.
It is defined to be independent of any specific directory service implementation. Thus a variety of directories--new, emerging, and already deployed--can be accessed in a common way.
Java 2 SDK, v1.3 already includes the JNDI classes. More service providers can be implemented or downloaded from Sun’s website.
![Page 5: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/5.jpg)
Javax.naming package:
5
Context The javax.naming package defines a Context
interface, which is the core interface for looking up, binding/unbinding, renaming objects and creating and destroying subcontexts.
The most commonly used operation is lookup(). You supply lookup() the name of the object you want to look up, and it returns the object bound to that name.
Printer printer = (Printer)ctx.lookup("treekiller");
printer.print(report);
![Page 6: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/6.jpg)
Java Design Patterns
6
![Page 7: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/7.jpg)
Java Design Patterns
7
Design Patterns have two main usages in software development:
Common platform for developersDesign patterns provide a standard terminology and are specific to
particular scenarioFor example, a singleton design pattern signifies use of single object so
all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.
Best Practices• Design patterns have been evolved over a long period of time and they
provide best solutions to certain problems faced during software development.
![Page 8: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/8.jpg)
Types of Design Pattern
8
there are 23 design patterns in three different categories :
Creational Patterns• A way to create objects while hiding the creation logic, rather
than instantiating objects directly using new operator.
Structural Patterns• concern class and object composition. Concept of inheritance is
used to compose interfaces and define ways to compose objects to obtain new functionalities.
![Page 9: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/9.jpg)
Types of Design Pattern
9
Behavioral Patterns• These design patterns are specifically concerned with
communication between objects.
![Page 10: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/10.jpg)
Factory Pattern
10
Comes under creational pattern as this pattern provides one of the best ways to create an object
Implementation- Create a Shape interface and concrete classes implementing the
Shape interface. - A factory class ShapeFactory is defined as a next step.- FactoryPatternDemo, our demo class will use ShapeFactory to
get a Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of object it needs.
![Page 11: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/11.jpg)
Factory Pattern- Class Diagram
11
![Page 12: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/12.jpg)
Factory Pattern- Implementation
12
Step 1: Create an interface.
![Page 13: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/13.jpg)
Factory Pattern- Implementation
13
Step 2: Create concrete classes implementing the same interface.
![Page 14: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/14.jpg)
Factory Pattern- Implementation
14
![Page 15: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/15.jpg)
Factory Pattern- Implementation
15
Step 3 : Create a Factory to generate object of concrete class based on given information.
![Page 16: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/16.jpg)
Factory Pattern- Implementation
16
Step 4: Use the Factory to get object of concrete class by passing an information such as type.
![Page 17: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/17.jpg)
Factory Pattern- Implementation
17
Step 5: Verify the output.
![Page 18: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/18.jpg)
Singleton Design Pattern
18
Creational pattern as this pattern provides one of the best way to create an object.
Involves a single class which is responsible to create its own object while making sure that only single object get created.
![Page 19: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/19.jpg)
Singleton Design Pattern
19
Implementation• SingleObject class have
its constructor as private and have a static instance of itself.
• SingleObject class provides a static method to get its static instance to outside world
![Page 20: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/20.jpg)
Singleton Design Pattern
20
Step 1: Create a Singleton Class
![Page 21: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/21.jpg)
Singleton Design Pattern
21
Step 2: Get the only object from the singleton class.
![Page 22: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/22.jpg)
Singleton Design Pattern
22
Step 3: Verify the output
![Page 23: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/23.jpg)
Prototype Design Pattern
23
Comes under creational pattern
Creating duplicate object while keeping performance in mind.
Involves implementing a prototype interface which tells to create a clone of the current object.
This pattern is used when creation of object directly is costly.
![Page 24: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/24.jpg)
Prototype Design Pattern
24
Implementation
Create an abstract class Shape and concrete classes extending the Shape class.
A class ShapeCache is defined as a next step which stores shape objects in a Hashtable and returns their clone when requested.
PrototypPatternDemo, our demo class will use ShapeCache class to get a Shape object.
![Page 25: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/25.jpg)
Prototype Design Pattern
25
Class Diagram
![Page 26: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/26.jpg)
Prototype Design Pattern
26
Step 1: Create an abstract class implementing Clonable interface.
![Page 27: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/27.jpg)
27
![Page 28: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/28.jpg)
Prototype Design Pattern
28
Step 2: Create concrete classes extending the above class.
}}
![Page 29: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/29.jpg)
Prototype Design Pattern
29
![Page 30: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/30.jpg)
Prototype Design Pattern
30
![Page 31: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/31.jpg)
Prototype Design Pattern
31
Step 3: Create a class to get concrete classes from database and store them in a Hashtable.
![Page 32: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/32.jpg)
32
![Page 33: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/33.jpg)
Prototype Design Pattern
33
![Page 34: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/34.jpg)
Prototype Design Pattern
34
Step 4: PrototypePatternDemo uses ShapeCache class to get clones of shapes stored in a Hashtable.
![Page 35: Advanced Programming](https://reader035.vdocuments.net/reader035/viewer/2022062813/56816468550346895dd652e9/html5/thumbnails/35.jpg)
Prototype Design Pattern
35
Step 5: Verify the output.