Q) We will create a factory pattern. The factory pattern is useful when you have to create objects based on user input or when you need to defer the instantiation of objects to subclasses. An object is created by calling a factory method. The factory method uses an abstract class for the superclass. Any subclasses extend the super class. Below you will have an example of a Factory Design Pattern. This is one of the most popular design patterns used in industry today. Use this example to create your own! It can be a factory of colors, shapes, animals, vehicles, toys, food, products, employees, etc. Anything you want! Include ALL of your classes (you should have 5 at the minimum), and a screenshot of the output. Below is an example using an abstract phone class as the superclass. The two subclasses are iphone and android. The phonefactory class provides the instantiation of the class. The factory is the main class. Notice that the Samsung and iphonex variables are of type Phone). Phone.java – this is the super class and it is abstract public abstract class Phone {     //The abstract methods in Phone must be implemented in the subclasses     public abstract String getRAM();     public abstract String getStorage();     public abstract String getSize();     public abstract void slogan();     @Override      public String toString(){           return "RAM= "+this.getRAM()+", Storage="+this.getStorage()+", Size="+this.getSize();      } } Android.java – subclass 1 public class Android extends Phone{         private String ram;      private String storage;      private String size;           public Android(String r, String s, String sz){           ram=r;           storage=s;           size=sz;      }      @Override      public String getRAM() {           return this.ram;      }        @Override      public String getStorage() {           return this.storage;      }        @Override      public String getSize() {           return this.size;      }         public void slogan()         {             System.out.println("Be together, not the same");         } }   iPhone.java – subclass 2 public class iPhone extends Phone {             private String ram;      private String storage;      private String size;           public iPhone(String r, String s, String sz){           ram=r;           storage=s;           size=sz;      }      @Override      public String getRAM() {           return this.ram;      }        @Override      public String getStorage() {           return this.storage;      }        @Override      public String getSize() {           return this.size;      }         public void slogan()         {             System.out.println("Think different");         } } PhoneFactory – factory class that instantiates the object requested public class PhoneFactory {     public Phone getPhone(String PhoneType, String r, String s, String sz)     {         //Create object here         if(PhoneType.equalsIgnoreCase("ANDROID"))             return new Android(r, s, sz);         else if(PhoneType.equalsIgnoreCase("IPHONE"))             return new iPhone(r, s, sz);        else             return null;     } } Main – Factory.java public class Factory {       public static void main(String[] args) {         PhoneFactory pf = new PhoneFactory();         Phone samsung= pf.getPhone("android","6 GB","64 GB","6.0 inches");      Phone iphonex= pf.getPhone("iphone","3 GB","64 GB","5.8 inches");      System.out.println("Android Configuration:"+samsung);         samsung.slogan();      System.out.println("iPhone Configuration:"+iphonex);         iphonex.slogan();     } }     Results of the above code: Android Configuration:RAM= 6 GB, Storage=64 GB, Size=6.0 inches Be together, not the same iPhone Configuration:RAM= 3 GB, Storage=64 GB, Size=5.8 inches Think different BUILD SUCCESSFUL (total time: 0 seconds)   Your code: Screenshot showing it working:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Q) We will create a factory pattern. The factory pattern is useful when you have to create objects based on user input or when you need to defer the instantiation of objects to subclasses. An object is created by calling a factory method. The factory method uses an abstract class for the superclass. Any subclasses extend the super class. Below you will have an example of a Factory Design Pattern. This is one of the most popular design patterns used in industry today. Use this example to create your own! It can be a factory of colors, shapes, animals, vehicles, toys, food, products, employees, etc. Anything you want!

Include ALL of your classes (you should have 5 at the minimum), and a screenshot of the output.

Below is an example using an abstract phone class as the superclass. The two subclasses are iphone and android. The phonefactory class provides the instantiation of the class. The factory is the main class. Notice that the Samsung and iphonex variables are of type Phone).

Phone.java – this is the super class and it is abstract

public abstract class Phone {

    //The abstract methods in Phone must be implemented in the subclasses

    public abstract String getRAM();

    public abstract String getStorage();

    public abstract String getSize();

    public abstract void slogan();

    @Override

     public String toString(){

          return "RAM= "+this.getRAM()+", Storage="+this.getStorage()+", Size="+this.getSize();

     }

}

Android.java – subclass 1

public class Android extends Phone{

        private String ram;

     private String storage;

     private String size;

    

     public Android(String r, String s, String sz){

          ram=r;

          storage=s;

          size=sz;

     }

     @Override

     public String getRAM() {

          return this.ram;

     }

 

     @Override

     public String getStorage() {

          return this.storage;

     }

 

     @Override

     public String getSize() {

          return this.size;

     }

        public void slogan()

        {

            System.out.println("Be together, not the same");

        }

}

 

iPhone.java – subclass 2

public class iPhone extends Phone {

            private String ram;

     private String storage;

     private String size;

    

     public iPhone(String r, String s, String sz){

          ram=r;

          storage=s;

          size=sz;

     }

     @Override

     public String getRAM() {

          return this.ram;

     }

 

     @Override

     public String getStorage() {

          return this.storage;

     }

 

     @Override

     public String getSize() {

          return this.size;

     }

        public void slogan()

        {

            System.out.println("Think different");

        }

}

PhoneFactory – factory class that instantiates the object requested

public class PhoneFactory {

    public Phone getPhone(String PhoneType, String r, String s, String sz)

    {

        //Create object here

        if(PhoneType.equalsIgnoreCase("ANDROID"))

            return new Android(r, s, sz);

        else if(PhoneType.equalsIgnoreCase("IPHONE"))

            return new iPhone(r, s, sz);

       else

            return null;

    }

}

Main – Factory.java

public class Factory {

 

    public static void main(String[] args) {

        PhoneFactory pf = new PhoneFactory();

        Phone samsung= pf.getPhone("android","6 GB","64 GB","6.0 inches");

     Phone iphonex= pf.getPhone("iphone","3 GB","64 GB","5.8 inches");

     System.out.println("Android Configuration:"+samsung);

        samsung.slogan();

     System.out.println("iPhone Configuration:"+iphonex);

        iphonex.slogan();

    }

}

 

 

Results of the above code:

Android Configuration:RAM= 6 GB, Storage=64 GB, Size=6.0 inches

Be together, not the same

iPhone Configuration:RAM= 3 GB, Storage=64 GB, Size=5.8 inches

Think different

BUILD SUCCESSFUL (total time: 0 seconds)

 

Your code:

Screenshot showing it working:

Expert Solution
steps

Step by step

Solved in 9 steps with 1 images

Blurred answer
Knowledge Booster
Software Development
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education