Professional Documents
Culture Documents
o We are going to create a Plan abstract class and concrete classes that extends the Plan
abstract class. A factory class GetPlanFactory is defined as a next step.
o GenerateBill class will use GetPlanFactory to get a Plan object. It will pass information
(DOMESTICPLAN / COMMERCIALPLAN / INSTITUTIONALPLAN) to GetPalnFactory to get
the type of object it needs.
1. import java.io.*;
2. abstract class Plan{
3. protected double rate;
4. abstract void getRate();
5.
6. public void calculateBill(int units){
7. System.out.println(units*rate);
8. }
9. }//end of Plan class.
Step 2: Create the concrete classes that extends Plan abstract class.
1. class DomesticPlan extends Plan{
2. //@override
3. public void getRate(){
4. rate=3.50;
5. }
6. }//end of DomesticPlan class.
1. class CommercialPlan extends Plan{
2. //@override
3. public void getRate(){
4. rate=7.50;
5. }
6. /end of CommercialPlan class.
1. class InstitutionalPlan extends Plan{
2. //@override
3. public void getRate(){
4. rate=5.50;
5. }
6. /end of InstitutionalPlan class.
1. class GetPlanFactory{
2.
3. //use getPlan method to get object of type Plan
4. public Plan getPlan(String planType){
5. if(planType == null){
6. return null;
7. }
8. if(planType.equalsIgnoreCase("DOMESTICPLAN")) {
9. return new DomesticPlan();
10. }
11. else if(planType.equalsIgnoreCase("COMMERCIALPLAN")){
12. return new CommercialPlan();
13. }
14. else if(planType.equalsIgnoreCase("INSTITUTIONALPLAN")) {
15. return new InstitutionalPlan();
16. }
17. return null;
18. }
19. }//end of GetPlanFactory class.
Step 4: Generate Bill by using the GetPlanFactory to get the object of concrete classes
by passing an information such as type of plan DOMESTICPLAN or COMMERCIALPLAN
or INSTITUTIONALPLAN.
1. import java.io.*;
2. class GenerateBill{
3. public static void main(String args[])throws IOException{
4. GetPlanFactory planFactory = new GetPlanFactory();
5.
6. System.out.print("Enter the name of plan for which the bill will be generated: ");
7. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
8.
9. String planName=br.readLine();
10. System.out.print("Enter the number of units for bill will be calculated: ");
11. int units=Integer.parseInt(br.readLine());
12.
13. Plan p = planFactory.getPlan(planName);
14. //call getRate() method and calculateBill()method of DomesticPaln.
15.
16. System.out.print("Bill amount for "+planName+" of "+units+" units is: ");
17. p.getRate();
18. p.calculateBill(units);
19. }
20. }//end of GenerateBill class.
Example let’s implement a generic shape interface. We can derive various concrete classes
from this interface like the circle, rectangle, etc. Then we will have a shape Factory class that
will access the concrete class objects.
package factory_pattern;
/**
*
* @author Urgesa
*/
interface Geometric_shape {
void draw_shape();
}
//circle
Geometric_shape shape_Circle = shapeFactory.shapeObject("CIRCLE");
//Rectangle
Geometric_shape shape_Rectangle = shapeFactory.shapeObject("RECTANGLE");
//Square
Geometric_shape shape_Square = shapeFactory.shapeObject("SQUARE");
B)UML diagram and the java implementation for Singleton design
pattern
Implementation of the Singleton Pattern
As already mentioned, a singleton design pattern restricts the class with only one instance and this
instance is given a global point of access. This was all classes that refer to the same object again and
again.
This implementation of this singleton class as explained above is shown in the Java
program below.
package singleobject;
class SingletonObject {
private SingletonObject(){}
//returns the only available object
return instance;
object.printMessage();
Implementation as follow
package adapterdemo;
interface LightningPhone {
void recharge();
void useLightning();
interface MicroUsbPhone {
void recharge();
void useMicroUsb();
@Override
connector = true;
System.out.println("Lightning connected");
}
@Override
if (connector) {
System.out.println("Recharge started");
System.out.println("Recharge finished");
} else {
@Override
connector = true;
System.out.println("MicroUsb connected");
@Override
if (connector) {
System.out.println("Recharge started");
System.out.println("Recharge finished");
} else {
}
}
this.lightningPhone = lightningPhone;
@Override
System.out.println("MicroUsb connected");
lightningPhone.useLightning();
@Override
lightningPhone.recharge();
phone.useMicroUsb();
phone.recharge();
phone.recharge();
rechargeMicroUsbPhone(android);
rechargeLightningPhone(iPhone);