Monday, 9 April 2018

Abstract Factory


                                 Intent

Provide a interface for creating families of related or depended object without specifying their concrete classes.

                              Also Known As

Kit

                              Applicability

Use the Abstract Factory when
                      
  •   A system should be independent of how its products are created,composed and represented.
  •   A system should be configured with one of multiple families of products.
  •  A family of related product objects is designed to be used together, and
    you need to enforce this constraint.
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations. 

                                  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