0% found this document useful (0 votes)
34 views1 page

Uml Diagram Code of Factory Pattern: Enemyship - Java: Enemyshiptesting

The Factory Pattern defines an interface for creating objects but lets subclasses decide which class to instantiate. It provides a way to encapsulate object creation. Some key advantages are that it handles situations where the class of the object that will be created is not known until runtime, encapsulates object creation, and allows subclasses to choose the type of object to create. The code example shows an EnemyShipFactory that creates different types of EnemyShip objects (UFOEnemyShip, RocketEnemyShip, BigUFOEnemyShip) based on a string passed to its makeEnemyShip method. This allows creation of the appropriate enemy ship subclass without coupling the client to the concrete classes.

Uploaded by

toarnabch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views1 page

Uml Diagram Code of Factory Pattern: Enemyship - Java: Enemyshiptesting

The Factory Pattern defines an interface for creating objects but lets subclasses decide which class to instantiate. It provides a way to encapsulate object creation. Some key advantages are that it handles situations where the class of the object that will be created is not known until runtime, encapsulates object creation, and allows subclasses to choose the type of object to create. The code example shows an EnemyShipFactory that creates different types of EnemyShip objects (UFOEnemyShip, RocketEnemyShip, BigUFOEnemyShip) based on a string passed to its makeEnemyShip method. This allows creation of the appropriate enemy ship subclass without coupling the client to the concrete classes.

Uploaded by

toarnabch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Factory Pattern

Also known as Virtual Constructor.


Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to
subclasses.
Advantage: i)When we don't know ahead of time what class object you need. ii)To Encapsulate object creation. iii)It allows the sub-classes to choose the
type of object to create.

UML Diagram

Code of Factory Pattern


EnemyShip.java:

EnemyShipTesting:

package com.e.demo.factory; package com.e.demo.factory;


public abstract class EnemyShip import
{
java.util.Scanner;
private String name;
public class EnemyShipTesting {
private double amtDamage;
public String getName() { return public static void main(String[] args){
name; }
EnemyShipFactory shipFactory = new EnemyShipFactory();
public void setName(String
EnemyShip theEnemy = null;
newName) {
Scanner userInput = new Scanner(System.in);
name = newName; }
System.out.print("What type of ship? (U / R / B)");
public double getDamage() { if (userInput.hasNextLine()){
return amtDamage; }
String typeOfShip = userInput.nextLine();
public void setDamage(double theEnemy = shipFactory.makeEnemyShip(typeOfShip);
newDamage){
if(theEnemy != null){
amtDamage = newDamage; }
doStuffEnemy(theEnemy);
public void followHeroShip(){
} else
SOP(getName() + " the hero");} System.out.print("Please enter U, R, or B next time");}
public void displayEnemyShip(){ }
SOP(getName() + " is on the
public static void doStuffEnemy(EnemyShip anEnemyShip)
screen");}
{
public void enemyShipShoots() { anEnemyShip.displayEnemyShip();
SOP(getName() + " attacks " +
anEnemyShip.followHeroShip();
UFOEnemyShip
getDamage() +:" hero");}
anEnemyShip.enemyShipShoots();}
package
} com.e.demo.factory;
}
public class UFOEnemyShip extends EnemyShip {
public UFOEnemyShip(){
UML Diagam of Factory Pattern
setName("UFO Enemy Ship");
setDamage(20.0);}
EnemyShipFactory:
}
package com.e.demo.factory;
public class EnemyShipFactory{
public EnemyShip makeEnemyShip(String newShipType){
if (newShipType.equals("U")){
BigUFOEnemyShip:
RocketEnemyShip:
return new UFOEnemyShip();
package com.e.demo.factory;
package com.e.demo.factory;
} else if
public class
BigUFOEnemyShip extends UFOEnemyShip
{ (newShipType.equals("R")){
public class RocketEnemyShip extends EnemyShip
{
return new RocketEnemyShip();
public BigUFOEnemyShip(){
public RocketEnemyShip(){
} else if (newShipType.equals("B")){
setName("Big UFO Enemy Ship");
setName("Rocket Enemy Ship");
return new BigUFOEnemyShip();
setDamage(40.0);
setDamage(10.0);
} else return null; }
}
}
}
}
}

You might also like