Wednesday, 6 March 2019

AdapterPattern

AdapterPattern

                                 Intent

Convert the interface of a class into another interface clients expect. Adapter
lets classes work together that couldn't otherwise because of incompatible
interfaces.

                              Also Known As

Wrapper

                              Applicability

Use the Adapter pattern when

  • you want to use an existing class, and its interface does not match the one you need.
  • you want to create a reusable class that cooperates with unrelated or
    unforeseen classes, that is, classes that don't necessarily have compatible
    interfaces.
  • (object adapter only) you need to use several existing subclasses, but it's
    impractical to adapt their interface by subclassing every one. An object
    adapter can adapt the interface of its parent class.
     

                                  Structure



Class Diagram

Program


package AbstractFactory;

/**
 *
 * @author Sonu
 */
public interface SumsungMobile {

    String getMobile();
   

 ================Concrete Class for Creating the Mobile========

package AbstractFactory;

/**
 *
 * @author Sonu
 */
public class SumsungNote2 implements SumsungMobile{

    @Override
    public String getMobile() {
//        System.out.println("This is Sumsung Note2 Mobile.");
        return "This is Sumsung Note2 Mobile.";
    }
   

================Another concrete class for creating anther Mobile==
package AbstractFactory;

/**
 *
 * @author Sonu
 */
public class SumsungNote4 implements SumsungMobile{
    @Override
    public String getMobile() {
//        System.out.println("This is Sumsung Note4 Mobile.");
        return "This is Sumsung Note4 Mobile.";
    }
}

==========Another interface for creating anther product===========

package AbstractFactory;

/**
 *
 * @author Sonu
 */
public interface SumsungEarphone {
    String getEarphone();
}

============== Concrete class for creating  Earphone ===========

package AbstractFactory;

/**
 *
 * @author Sonu
 */
public class SumsungNote2 implements SumsungMobile{

    @Override
    public String getMobile() {
//        System.out.println("This is Sumsung Note2 Mobile.");
        return "This is Sumsung Note2 Mobile.";
    }
   
}

============Concrete class for creating  Earphone ==============

package AbstractFactory;
/**
 *
 * @author Sonu
 */
public class SumsungNote4EP implements SumsungEarphone{

    @Override
    public String getEarphone() {
//        System.out.println("This is Sumsung Note4  Earphone.");
        return "This is Sumsung Note4  Earphone.";
    }
   
}

 ============Creating factory class for product ================

package AbstractFactory;

import java.util.Scanner;

/**
 *
 * @author Sonu
 */
public class FactoryMobile extends AbstractFactory{
    @Override
    public SumsungEarphone getEarphone()
    {

       SumsungEarphone earphone=new SumsungNote2EP();
       return earphone;
    }

    @Override
    public SumsungMobile getMobile() {

        SumsungMobile sm=new SumsungNote2();
        return sm;
    }
}

================factory for creating another product===========

package AbstractFactory;
/**
 *
 * @author Sonu
 */
public class FactoryEarphone extends AbstractFactory{
    public SumsungMobile getMobile()
    {
        SumsungMobile sm=new SumsungNote4();
        return sm;
    }
    public SumsungEarphone getEarphone()
    {
        SumsungEarphone  earphone=new SumsungNote4EP();
        return earphone;
    }
}

=============Most IMP abstract factory ====================

package AbstractFactory;
/**
 *
 * @author Sonu
 */
public class FactoryEarphone extends AbstractFactory{
    public SumsungMobile getMobile()
    {
        SumsungMobile sm=new SumsungNote4();
        return sm;
    }
    public SumsungEarphone getEarphone()
    {
        SumsungEarphone  earphone=new SumsungNote4EP();
        return earphone;
    }
}

=================The main class =======================

package AbstractFactory;

import java.util.Scanner;

/**
 *
 * @author Sonu
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Enter Number 1 for Sumsung Note 2 Mobile and Earphone.");
        System.out.println("Enter Number 2 for Sumsung Note 4 Mobile and Earphone.");
        System.out.println("3 for note 5");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        AbstractFactory abstractFactory =AbstractFactory.getAbstractFactory(i);
        SumsungMobile sm = abstractFactory.getMobile();
        System.out.println(""+sm.getMobile());
        SumsungEarphone se = abstractFactory.getEarphone();
        System.out.println(""+se.getEarphone());
        //System.out.println(""+sm.getMobile());
   
    }
}

=================== output =========================

Output


 





0 comments:

Post a Comment