Factory and abstract factory design patterns with examples in Java, Lecture notes of Computer Science

This document includes explanation of Factory and abstract factory design patterns with examples.

Typology: Lecture notes

2019/2020
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 02/19/2020

Sikander_Iqbal
Sikander_Iqbal 🇵🇰

5

(1)

7 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Factory and Abstract Factory Design
Pattern with examples in Java
Factory Design Pattern:
A Factory Pattern or Factory Method Pattern says that it only defines an interface or abstract
class that creates an object. Subclasses are therefore responsible for creating the class case.
Factory Design Method is also known as Virtual Constructor.
Advantages of Factory Design Method:
Factory Design Method allows sub-classes to choose the object type that they create.
This promotes loose connection by eliminating the need for binding specific
application classes to the code. It means that the code only communicates with the
resulting interface or abstract class, so it operates with all classes that enforce or
extend it abstract class.
Usage of Factory Design method:
If a class doesn't understand what subclasses are needed to create.
If a class needs to define the objects to be generated in its subclasses.
If the parent classes choose to create objects in their subclasses.
Deployment:
We will create a plan that extends the Plan abstract class and concrete classes. The next move
is to identify a GetPlanFactory factory class.
GetPlanFactory will be used to acquire a Project object in the GenerateBill package. It will
transmit data to the GetPalnFactory to get the kind of object it needs. (DOMESTERS /
COMMIRCIALPLAN / INSTITUTIONALPLEN).
pf3
pf4
pf5
pf8
pf9
pfa
Discount

On special offer

Partial preview of the text

Download Factory and abstract factory design patterns with examples in Java and more Lecture notes Computer Science in PDF only on Docsity!

Factory and Abstract Factory Design

Pattern with examples in Java

Factory Design Pattern:

A Factory Pattern or Factory Method Pattern says that it only defines an interface or abstract

class that creates an object. Subclasses are therefore responsible for creating the class case.

Factory Design Method is also known as Virtual Constructor.

Advantages of Factory Design Method:

 Factory Design Method allows sub-classes to choose the object type that they create.

 This promotes loose connection by eliminating the need for binding specific

application classes to the code. It means that the code only communicates with the

resulting interface or abstract class, so it operates with all classes that enforce or

extend it abstract class.

Usage of Factory Design method:

 If a class doesn't understand what subclasses are needed to create.

 If a class needs to define the objects to be generated in its subclasses.

 If the parent classes choose to create objects in their subclasses.

Deployment:

We will create a plan that extends the Plan abstract class and concrete classes. The next move

is to identify a GetPlanFactory factory class.

GetPlanFactory will be used to acquire a Project object in the GenerateBill package. It will

transmit data to the GetPalnFactory to get the kind of object it needs. (DOMESTERS /

COMMIRCIALPLAN / INSTITUTIONALPLEN).

Electricity Bill Calculate: A Global Factory Method Sample:

Phase-1: Construct an explicit class plan.

1. import java.io.*.;

2. abstract class Plan {

3. Protected double rate;

4. abstract void getRate();

5. public void calculateBill( int units){

6. System.out.println(units*rate);

8. }//end of Plan class.

Phase-2: Creating concrete classes to cover the abstract class of Plan.

9. class DomesticPlan extends Plan{

  1. //@override
  2. public void getRate(){
  3. rate=3.50;
  4. }
  5. }//end of DomesticPlan class. 15. class CommercialPlan extends Plan{
  6. //@override
  7. public void getRate(){
  8. rate=7.50;
  9. }
  10. /end of CommercialPlan class. 21. class InstitutionalPlan extends Plan{
  11. //@override
  12. public void getRate(){
  13. rate=5.50;
  14. }
  15. /end of InstitutionalPlan class.

Phase-3: Build a GetPlanFactory to create artefacts of specific classes based on data.

27. class GetPlanFactory{

  1. //use getPlan method to get object of type Plan
  2. public Plan getPlan(String planType){
  3. if (planType == null ){
  4. return null ;
  5. }
  6. if (planType.equalsIgnoreCase("DOMESTICPLAN")) {
  7. return new DomesticPlan();

Output: Abstract Factory Design Pattern:

Abstract Factory Design says that only an interface or abstract class can be specified to

construct families of connected (or dependent) objects, without defining their particular

subclasses. That is why Abstract Model Factory is one level higher than the Model Factory.

The Kit is also regarded as an Abstract Factory design.

Advantages of Abstract Factory Design:

 Abstract Factory Pattern isolates the client code from concrete (implementation)

classes.

 It eases the exchanging of object families.

 It promotes consistency among objects.

Uses of Abstract Factory pattern:

 When it comes to making, writing and representing the device independently of its

source.

 Such limitation must be applied when the class of associated objects is to be used

together.

 If you wish to supply an object library that shows no implementations and only

interfaces.

 If the system needs one of several families of objects to be installed.

Deployment:

 We will be building a bank Interface, an abstract loan class and its subclasses.

 Then as a next step we construct AbstractFactory class.

 Afterwards, the BankFactory and LoanFactory classes are developed that expand the

abstractFactory class.

 Following this, the FaktoryCreator class uses AbstractFactoryPatternExample to get

an AbstractFactory object class.

 See the following diagram attentively:

Example of Abstract Factory Method:

We are calculating the loan for various banks like, HBL, ABL, SBL, etc,

Phase-1: create the bank interface.

1. import java.io.*;

2. interface Bank{

3. String getBankName();

Phase-2: Create specific classes to use the interface of the bank.

66. class HBL implements Bank{

  1. private final String BNAME;
  2. public HBL(){
  3. BNAME="HBL BANK";
  1. n=years* 12 ;
  2. rate=rate/ 1200 ;
  3. EMI=((rateMath.pow(( 1 +rate),n))/((Math.pow(( 1 +rate),n))- 1 ))loanamount;
  4. System.out.println("your monthly EMI is "+ EMI +" for the amount"+loanamount+" you have borrowed");
  5. }
  6. }// end of the Loan abstract class.

Phase-4: Create concrete classes that extend the Loan abstract class..

118. class HomeLoan extends Loan{

  1. public void getInterestRate( double r){
  2. rate=r;
  3. }
  4. }//End of the HomeLoan class. 123. class BussinessLoan extends Loan{
  5. public void getInterestRate( double r){
  6. rate=r;
  7. }
  8. }//End of the BusssinessLoan class. 129. class EducationLoan extends Loan{
  9. public void getInterestRate( double r){
  10. rate=r;
  11. }
  12. }//End of the EducationLoan class.

Phase-5: Create an abstract class (i.e AbstractFactory) to get the factories for Bank and Loan

Objects.

134. abstract class AbstractFactory{

  1. public abstract Bank getBank(String bank);
  2. public abstract Loan getLoan(String loan);
  3. }

Phase-6: Create the factory classes that inherit AbstractFactory class to generate the object of

concrete class based on given information.

138. class BankFactory extends AbstractFactory{

  1. public Bank getBank(String bank){
  2. if (bank == null ){
  3. return null ;
  4. }
  5. if (bank.equalsIgnoreCase("HBL")){
  6. return new HBL();
  1. } else if (bank.equalsIgnoreCase("ABL")){
  2. return new ABL();
  3. } else if (bank.equalsIgnoreCase("SBL")){
  4. return new SBL();
  5. }
  6. return null ;
  7. }
  8. public Loan getLoan(String loan) {
  9. return null ;
  10. }
  11. }//End of the BankFactory class. 156. class LoanFactory extends AbstractFactory{
  12. public Bank getBank(String bank){
  13. return null ;
  14. }
  15. public Loan getLoan(String loan){
  16. if (loan == null ){
  17. return null ;
  18. }
  19. if (loan.equalsIgnoreCase("Home")){
  20. return new HomeLoan();
  21. } else if (loan.equalsIgnoreCase("Business")){
  22. return new BussinessLoan();
  23. } else if (loan.equalsIgnoreCase("Education")){
  24. return new EducationLoan();
  25. }
  26. return null ;
  27. }
  28. }

Phase-7: Create a FactoryCreator class to get the factories by passing an information such as

Bank or Loan.

176. class FactoryCreator {

  1. public static AbstractFactory getFactory(String choice){
  2. if (choice.equalsIgnoreCase("Bank")){
  3. return new BankFactory();
  4. } else if (choice.equalsIgnoreCase("Loan")){
  5. return new LoanFactory();
  6. }
  7. return null ;
  8. }
  9. }//End of the FactoryCreator.

Draw backs of Abstract Factory Design Pattern:

In the circumstances mentioned below, Abstract Factory design patterns should be used

carefully.

 Don't use it if logically not connected to the underlying object factories

 Don't use it if the object underlying factories have different purposes for objects

 Don't use it when there are very different attributes within the object factory.

In addition to the scenarios mentioned above, the abstract factory model can also increase

object complexity as multiple abstract methods are implemented, whereas sub-classes only

use selected methods. It's hard to use the Abstract Factory style template in such scenarios.

Conclusion of Abstract Factory Design Pattern:

In scenarios where there are a number of similar object classes, the Abstract Factory Design

pattern is useful. To order to simplify object development, these classes require an abstract

layer. The emphasis is primarily on establishing this abstract class level in Abstract Factory

Model pattern. This article explained how an abstract concept of a factory is implemented in

a practical worst.