Code Explosion

Code that explodes conventions

Inversion of Control and Dependency Injection: Part 2

এর আগের পোস্ট-এ ইনভার্সন অব কন্ট্রোল এবং ডিপেনডেন্সি ইনজেকশান এর ভেতরের কথা গুলো বলার চেষ্টা করেছি, এবার একটি উদাহরণ দেওয়া যাক।

এই উদাহরণটির জন্যে আমি স্প্রিং ফ্রেমওয়ার্ক ব্যবহার করবো। আমি একটি ছোট্ট এপ্লিকেশান লিখেছি যার নাম টকার এপ, যে বিভিন্ন ভাষায় হ্যালোওয়ার্ল্ড বলতে পারে।  শুরুতে একটা ITalkable ইন্টারফেইস আছে যা একটি talk() মেথড এক্সপোজ করে।

package org.codexplo;

public interface ITalkable {
public void talk();
}

তারপর EnglishTalker এবং JapaniTalker  ক্লাস  Italkable  কে ইম্প্লিমেন্ট করে এবং তারা দুই ভাষায় কথা বলে।

package org.codexplo;

public class EnglishTalker implements ITalkable {

@Override
public void talk() {
System.out.println("Hello world");
}
}

 

package org.codexplo;

public class JapaniTalker implements ITalkable {

@Override
public void talk() {
System.out.println("こんにちは、世界");
}
}

এখন মনে করি আমাদের একটি TalkerService আছে যে নির্ধারণ করে দেয় এই মুহূর্তে আমাদের এপ কোন টকারটি ব্যবহার করবে। আমাদের TalkerService এ একটি মেথড আছে, sayHelloworld() এবং এর উপরে একটি ITalkable এর রেফারেন্স আছে, যেখানে আমরা সরাসরি এর  এর ইনসটেন্স ক্রিয়েট করেছি।

package org.codexplo;

public class TalkerService {
private ITalkable talkable = new EnglishTalker();

public void sayHelloworld() {
talkable.talk();
}
}

এখন আমরা আমাদের মেইন এপটা লিখি ।

package org.codexplo;

public class TalkerApp {
public static void main(String[] args) {
TalkerService talkerService = new TalkerService();
talkerService.sayHelloworld();
}
}

এখন আমরা একটু ক্লাস ডায়াগ্রাম দেখি।

আমরা খুব সহজেই দেখতে পারছি, আমাদের সিস্টেম খুব টাইটলি কাপল্ড। কারণ আমরা কোন ভাষায় হ্যালো ওয়াল্ড বলবো আমাদেরকে হার্ড কোড করে বলে দিতে হচ্ছে প্রতিবার।

যেহেতু আমাদের উদ্দেশ্য একটি লুজলি কাপলড সিস্টেম তৈরি করা, এখন দেখি কিভাবে এই জিনিসটি ইনভার্সন অব কন্ট্রোল ব্যবহার করে করা যায়। স্প্রিং ফ্রেমওয়ার্ক একটি একটি কনটেইনার প্রোভাইড করে কম্পোনেন্ট ম্যনেজ করার জন্যে। আমরা এগুলো আগের পোস্টে বলেছি। আমরা আমাদের TalkerService টাকে একটু পরিবর্তন করি।

package org.codexplo;

public class TalkerService {
private ITalkable talkable;

public void setTalkable(ITalkable talkable) {
this.talkable = talkable;
}

public void sayHelloworld() {
talkable.talk();
}
}

আমরা এখনে সরাসরি অবজেক্ট ক্রিয়েট না করে একটি সেটার ইনজেকশান ব্যবহার করেছি। অবজেক্ট ক্রিয়েট এর কাজটি ছেড়ে দিলাম কনটেইনার এর কাছে। এই TalkerService কখনোই  ITalkable এর ইম্প্লিমেন্টেশানের এর অবজেক্ট ক্রিয়েট করবে না, বরং তাকে তা প্রভাইড করা হবে। অর্থাৎ সে রানটাইমে জেনে যাবে তাকে কোন ভাষায় কথা বলতে হবে।

এখন একটু ক্লাস ডায়াগ্রাম দেখি।

এখন সিস্টেম অনেক লোজলি কাপল্ড। এখানে সেটার ইনজেকশান ব্যবহার করা হয়েছে।

এই কাজটি করতে হলে আমাদের কে একটি কনফিগারেশান ফাইল মেইনটেইন করতে হয়

<!--?<span class="hiddenSpellError" pre=""-->xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >

<bean
id="englishTalker"
>
<!--<span class="hiddenSpellError" pre=""-->bean>
<bean
id="japaniTalker"
>
</bean>

<bean
id="talkerService"
>
<property name="talkable" >
<ref local="englishTalker" />
</property>
</bean>
</beans>

আর আমাদের মেইনএপ ক্লাসটি দেখতে এখন হবে …

</pre>
package org.codexplo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTalkerApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
TalkerService talkerService = (TalkerService) context
.getBean("talkerService");
talkerService.sayHelloworld();
}
}
<pre>

এই প্রোগ্রামটি রান করতে তোমার যা যা লাগবে

antlr-runtime-3.0.jar
commons-logging-1.0.4.jar
org.springframework.asm-3.0.0.M3.jar
org.springframework.beans-3.0.0.M3.jar
org.springframework.context-3.0.0.M3.jar
org.springframework.context.support-3.0.0.M3.jar
org.springframework.core-3.0.0.M3.jar
org.springframework.expression-3.0.0.M3.jar

এই যারগুলো ক্লাসপাথ এ এড করতে হবে।

ডাউনলোড: Full Source & Libs

About these ads

3 Comments on “Inversion of Control and Dependency Injection: Part 2

  1. lordamit
    June 23, 2012

    খুব সুন্দর করে বুঝিয়েছিস। কিন্তু এখানে Container Provide করছে Spring.
    আমার ঐ লুজলি কাপলড ডায়াগ্রাম পর্যন্ত সহজ লেগেছে। একটা কি Container নিজে বানিয়ে দেখানো সম্ভব?

    • Bazlur Rahman
      June 26, 2012

      defiantly বানানো সম্ভব, কিন্তু জিনিসটি খুব বেশি সহজ হবে না, তাছাড়া অনেক ভাল ভাল Container বানানোই আছে, গুগুল গুইস আমরা যেকোন প্রজেক্টের জন্যে ব্যবহার করতে পারি। ডট নেটের জন্যে ninject সবচেয়ে ভাল। আমি ninject ব্যবহার করি।

  2. Sahib Bin Mahboob
    August 20, 2012

    Nice explanation.
    But I failed to understand the symbols you used in the class diagram. I tried these:
    http://www.holub.com/goodies/uml/
    http://loufranco.com/blog/assets/cheatsheet.pdf

    Can you please suggest me some resource to understand your class diagram?
    Thanks in advance :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Information

This entry was posted on June 18, 2012 by in Design pattern, Java, Tips.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 291 other followers

RSS Jenkov.com

  • Java Exception Handling - New book published!
    Download this book for free from Amazon until and including march 1st! Exception handling is an aspect of Java development that has not received as much attention as it deserves. If you do not have exception handling under control, you risk that your application or data ends up in an unhealthy state, which can have serious consequences. That is why I wrote t […]
  • HTML4 Compressed - Kindle book published
    In this day and age it should be easy and cheap to learn HTML4, which everyone needs to learn in order to learn HTML5. Therefore I published this Kindle book which explains HTML4. The parts of HTML4 that is not removed in HTML5, that is.
  • jQuery Compressed - Updated
    I have updated the jQuery Compressed book to be compatible with version 1.9.1 of jQuery.
  • Displaying SVG in Browsers
    The SVG tutorial has been updated, showing how to include SVG in HTML pages using the img HTML element, or the SVG element.
  • Maven Tutorial.
    This Maven tutorial explains the core concepts of Maven which are important to understand for new Maven users.

my tweets

Follow

Get every new post delivered to your Inbox.

Join 291 other followers