I have a class called a Plane.
class Plane {
private int _planeId;
public int getModel() { }
public int getNumberOfPassengers() {}
}
And I another class called PlaneService which depends on plane and adds some logic to it,
class PlaneService {
private Plane _plane;
public PlaneService(Plane _plane) {
this._plane = _plane;
}
public void validdatePlane(int planeId) {
fetch plane from planeId from db.
if ( plane.getNumberOfPassengers() is in range of {100 to 300} )
plane is valid.
}
}
But now a new requirement comes in: plane must be extended to support fighter jets. So new design looks like this:
class Plane {
private int _planeId;
public int getModel() { }
}
class PassengerPlane extends Plane {
public int getNumberOfPassengers() {}
}
class FigherJet extends Plane {
public boolean isCommissioned() {}
}
My question is how can I best design 'PlaneSvc'in OOP way ? Is there a good design pattern ?
Currently, my code looks like this:
class PlaneService {
private Plane _plane;
public PlaneService(Plane _plane) {
this._plane = _plane;
}
public void validdatePlane(int planeId) {
fetch CommercialPlane from planeId from db.
if (commericialPlaneObject != null) {
if ( plane.getNumberOfPassengers() is in range of {100 to 300} )
plane is valid.
}
fetch FighterPlaneObject from planeId from db.
if (FighterPlaneObject != null) {
if (fighterplane.isCommissioned()) {
return validPlane;
}
}
}
}
I am sure there is some design pattern to deal with such a case. I need to understand a cleaner approach to if-else here.
What you have here is the strategy pattern and you can find it here.
I dont thing you should pass planeId to the method because you have attached an plane to the PlaneService in the constructor which means that no service without a plane, i also assume that plane has the planeId in it.
If you want the implementation not to be bound at compile time you should use bridge pattern. More or less is the same but you use it for structural purpuses and you pass the delegator not in the constructor but with a setter method.
You could define a validate method in the plane class, for example:
class Plane {
private int _planeId;
public boolean validate(){
return false;
}
public int getModel() { }
}
and then in the child classes, you could override the behavior of the validate method:
class FigherJet extends Plane {
public boolean isCommissioned() {}
#Override
public boolean validate() {
return isComissioned();
}
}
class PassengerPlane extends Plane {
public int getNumberOfPassengers() {}
#Override
public boolean validate(){
//if plane.getNumberOfPassengers() is 100 to 300, return true, else return false
}
}
And then your plane service can call the validate() method on any of the child objects:
public boolean validatePlane(int planeId) {
//fetch passenger plane from planeId from db.
if (passengerPlane != null) {
return passengerPlane.validate();
}
}
You can use Visitor patter for this case as well.
class Plane {
private int _planeId;
public int getModel() { }
abstract boolean validateWith(PlaneValidator validator);
}
class PassengerPlane extends Plane {
public int getNumberOfPassengers() {}
boolean validateWith(PlaneValidator validator) {
return validator.validate(this);
}
}
class FigherJet extends Plane {
public boolean isCommissioned() {}
boolean validateWith(PlaneValidator validator) {
return validator.validate(this);
}
}
class PlaneService implements PlaneValidator {
...
boolean validatePlane(int planeId) {
//fetch Plane object from db
return plane.validateWith(this);
}
//Methods implemented from PlaneValidator
#Override
boolean validate(FighterJet plane) {
return plane.isCommissioned();
}
#Override
boolean validate(PassengerPlane plane) {
return plane.getNumberOfPassengers() in range(100, 300);
}
}
In this way, you can easily extend your system with new types, all thing you need to do is override validateWith(PlaneValidator) method in derived type and add appropriate method to PlaneValidator and describe its behavior in implemented method. I don't know is it pattern applicable to your system, but for me looks it could be.
Related
I have two classes: Fish and Plant. They do not inherit from any classes.
But both of them have one method called isAlive() which have the same implementation details. Now I have a list of fish and another list of dog and I need to remove dead fish and dead dog. I want my method to have same name but it is not possible without adding additional field to method signature. Is it possible I do not need to write additional chunk of code which does the same as the last chunk of code?
Below is the code. For class Model, Fish and Plant are two data members and they are ArrayList of Fish and Plant objects.
Is there any way I can write only one method called count and I do not need to add additional field to my method signature or modify my return type?
public class Fish{
public boolean isAlive(){
if(this.size > 0){
return true;
}
return false;
}
}
public class Plant{
public boolean isAlive(){
if(this.size > 0){
return true;
}
return false;
}
}
public class Model{
private int countDeadFish() {
int totalCount = 0;
for(Fish aFish : this.fish) {
if(aFish.isAlive() == false) {
totalCount += 1;
}
}
return totalCount;
}
private int countDeadPlants() {
int totalCount = 0;
for(Plant plant : this.plants) {
if(plant.isAlive() == false) {
totalCount += 1;
}
}
return totalCount;
}
}
If you do not want to use inheritance, then you can use a common method:
public class AliveChecker {
public static boolean isAlive(int size) {
return size > 0;
}
}
public class Plant{
public boolean isAlive(){
return AliveChecker.isAlive(this.size);
}
}
public class Fish{
public boolean isAlive(){
return AliveChecker.isAlive(this.size);
}
}
Since Fishand Plant do not inherit from anything yet you can consider creating a superclass and extend from it:
public class LivingThing {
protected int size = 1;
public boolean isAlive() {
return size > 0;
}
}
public class Plant extends LivingThing {
}
public class Fish extends LivingThing {
}
This example uses inheritance to classify Plantand Fish into the superclass LivingThing. You can set the size for example in the constructor of the Plant or an instance method:
public class Plant extends LivingThing {
public Plant(int size){
this.size = size;
}
}
Your Model could then be:
public class Model{
private int countDeadFish() {
return countDead(this.fish);
}
private int countDeadPlants() {
return countDead(this.plants);
}
private int countDead(ArrayList<LivingThing> things) {
int totalCount = 0;
for(LivingThing thing: things) {
if(!thing.isAlive()) {
totalCount++;
}
}
return totalCount;
}
}
Use interface
public interface LiveObject {
boolean isAlive();
}
public class Fish implements LiveObject {
public boolean isAlive(){
if(this.size > 0){
return true;
}
return false;
}
}
public class Plant implements LiveObject {
public boolean isAlive(){
if(this.size > 0){
return true;
}
return false;
}
}
public class Model{
private int countDead(Collection<LiveObject> objects) {
int totalCount = 0;
for(LiveObject obj : objects) {
if(obj.isAlive() == false) {
totalCount += 1;
}
}
return totalCount;
}
private int countDeadFish() {
return countDead(this.fish);
}
}
Based on the comments it seems you can't modify Fish or Plant. Here's an approach to reduce duplication in countDead<Something> methods which does not require this.
Basically you want to count items in an array which satisfy certain criteria. With Java 8 you can capture this criteria in a predicate using lambdas or method references. You do not need inheritance or implementation of a certain interface for this.
private long countDeadFish() {
return countDeadItems(this.fish, Fish::isAlive);
}
private long countDeadPlants() {
return countDeadItems(this.plants, Plant::isAlive);
}
private <T> long countDeadItems(Collection<T> items, Predicate<? super T> isAlive) {
return items.stream().filter(isAlive.negate()).count();
}
You could create a utility method (in a utility class somewhere):
public final class Liveliness {
private Liveliness() {
}
public static boolean isAlive(final IntSupplier sizer) {
return sizer.getAsInt() > 0;
}
}
Your method then becomes:
public boolean isAlive(){
return Liveliness.isAlive(this::getSize);
}
Alternatively, use an interface Life:
public interface Life {
int getSize();
default boolean isAlive(){
return getSize() > 0;
}
}
This way, adding a getSize method and inheriting from Life will add the method.
Note, avoid the following antipattern:
if(test) {
return true;
} else {
return false;
}
Use return test.
I am attempting to implement my first Factory Design Pattern, and I'm not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I'm trying to do:
for (Blueprint bp : blueprints) {
Vehicle v = VehicleFactory.buildVehicle(bp);
allVehicles.add(v);
// Can I accomplish this without using 'instanceof'?
if (v instanceof Car) {
cars.add((Car) v);
} else if (v instanceof Boat) {
boats.add((Boat) v);
} else if (v instanceof Plane) {
planes.add((Plane) v);
}
}
From what I've read on Stack Overflow, using 'instanceof' is a code smell. Is there a better way to check the type of vehicle that was created by the factory without using 'instanceof'?
I welcome any feedback/suggestions on my implementation as I'm not even sure if I'm going about this the right way.
Full example below:
import java.util.ArrayList;
class VehicleManager {
public static void main(String[] args) {
ArrayList<Blueprint> blueprints = new ArrayList<Blueprint>();
ArrayList<Vehicle> allVehicles = new ArrayList<Vehicle>();
ArrayList<Car> cars = new ArrayList<Car>();
ArrayList<Boat> boats = new ArrayList<Boat>();
ArrayList<Plane> planes = new ArrayList<Plane>();
/*
* In my application I have to access the blueprints through an API
* b/c they have already been created and stored in a data file.
* I'm creating them here just for example.
*/
Blueprint bp0 = new Blueprint(0);
Blueprint bp1 = new Blueprint(1);
Blueprint bp2 = new Blueprint(2);
blueprints.add(bp0);
blueprints.add(bp1);
blueprints.add(bp2);
for (Blueprint bp : blueprints) {
Vehicle v = VehicleFactory.buildVehicle(bp);
allVehicles.add(v);
// Can I accomplish this without using 'instanceof'?
if (v instanceof Car) {
cars.add((Car) v);
} else if (v instanceof Boat) {
boats.add((Boat) v);
} else if (v instanceof Plane) {
planes.add((Plane) v);
}
}
System.out.println("All Vehicles:");
for (Vehicle v : allVehicles) {
System.out.println("Vehicle: " + v + ", maxSpeed: " + v.maxSpeed);
}
System.out.println("Cars:");
for (Car c : cars) {
System.out.println("Car: " + c + ", numCylinders: " + c.numCylinders);
}
System.out.println("Boats:");
for (Boat b : boats) {
System.out.println("Boat: " + b + ", numRudders: " + b.numRudders);
}
System.out.println("Planes:");
for (Plane p : planes) {
System.out.println("Plane: " + p + ", numPropellers: " + p.numPropellers);
}
}
}
class Vehicle {
double maxSpeed;
Vehicle(double maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
class Car extends Vehicle {
int numCylinders;
Car(double maxSpeed, int numCylinders) {
super(maxSpeed);
this.numCylinders = numCylinders;
}
}
class Boat extends Vehicle {
int numRudders;
Boat(double maxSpeed, int numRudders) {
super(maxSpeed);
this.numRudders = numRudders;
}
}
class Plane extends Vehicle {
int numPropellers;
Plane(double maxSpeed, int numPropellers) {
super(maxSpeed);
this.numPropellers = numPropellers;
}
}
class VehicleFactory {
public static Vehicle buildVehicle(Blueprint blueprint) {
switch (blueprint.type) {
case 0:
return new Car(100.0, 4);
case 1:
return new Boat(65.0, 1);
case 2:
return new Plane(600.0, 2);
default:
return new Vehicle(0.0);
}
}
}
class Blueprint {
int type; // 0 = car; // 1 = boat; // 2 = plane;
Blueprint(int type) {
this.type = type;
}
}
You could implement the Visitor pattern.
Detailed Answer
The idea is to use polymorphism to perform the type-checking. Each subclass overrides the accept(Visitor) method, which should be declared in the superclass. When we have a situation like:
void add(Vehicle vehicle) {
//what type is vehicle??
}
We can pass an object into a method declared in Vehicle. If vehicle is of type Car, and class Car overrode the method we passed the object into, that object would now be processed within the method declared in the Car class. We use this to our advantage: creating a Visitor object and pass it to an overriden method:
abstract class Vehicle {
public abstract void accept(AddToListVisitor visitor);
}
class Car extends Vehicle {
public void accept(AddToListVisitor visitor) {
//gets handled in this class
}
}
This Visitor should be prepared to visit type Car. Any type that you want to avoid using instanceof to find the actual type of must be specified in the Visitor.
class AddToListVisitor {
public void visit(Car car) {
//now we know the type! do something...
}
public void visit(Plane plane) {
//now we know the type! do something...
}
}
Here's where the type checking happens!
When the Car receives the visitor, it should pass itself in using the this keyword. Since we are in class Car, the method visit(Car) will be invoked. Inside of our visitor, we can perform the action we want, now that we know the type of the object.
So, from the top:
You create a Visitor, which performs the actions you want. A visitor should consist of a visit method for each type of object you want to perform an action on. In this case, we are creating a visitor for vehicles:
interface VehicleVisitor {
void visit(Car car);
void visit(Plane plane);
void visit(Boat boat);
}
The action we want to perform is adding the vehicle to something. We would create an AddTransportVisitor; a visitor that manages adding transportations:
class AddTransportVisitor implements VehicleVisitor {
public void visit(Car car) {
//add to car list
}
public void visit(Plane plane) {
//add to plane list
}
public void visit(Boat boat) {
//add to boat list
}
}
Every vehicle should be able to accept vehicle visitors:
abstract class Vehicle {
public abstract void accept(VehicleVisitor visitor);
}
When a visitor is passed to a vehicle, the vehicle should invoke it's visit method, passing itself into the arguments:
class Car extends Vehicle {
public void accept(VehicleVisitor visitor) {
visitor.visit(this);
}
}
class Boat extends Vehicle {
public void accept(VehicleVisitor visitor) {
visitor.visit(this);
}
}
class Plane extends Vehicle {
public void accept(VehicleVisitor visitor) {
visitor.visit(this);
}
}
That's where the type-checking happens. The correct visit method is called, which contains the correct code to execute based on the method's parameters.
The last problem is having the VehicleVisitor interact with the lists. This is where your VehicleManager comes in: it encapsulates the lists, allowing you to add vehicles through a VehicleManager#add(Vehicle) method.
When we create the visitor, we can pass the manager to it (possibly through it's constructor), so we can perform the action we want, now that we know the object's type. The VehicleManager should contain the visitor and intercept VehicleManager#add(Vehicle) calls:
class VehicleManager {
private List<Car> carList = new ArrayList<>();
private List<Boat> boatList = new ArrayList<>();
private List<Plane> planeList = new ArrayList<>();
private AddTransportVisitor addVisitor = new AddTransportVisitor(this);
public void add(Vehicle vehicle) {
vehicle.accept(addVisitor);
}
public List<Car> getCarList() {
return carList;
}
public List<Boat> getBoatList() {
return boatList;
}
public List<Plane> getPlaneList() {
return planeList;
}
}
We can now write implementations for the AddTransportVisitor#visit methods:
class AddTransportVisitor implements VehicleVisitor {
private VehicleManager manager;
public AddTransportVisitor(VehicleManager manager) {
this.manager = manager;
}
public void visit(Car car) {
manager.getCarList().add(car);
}
public void visit(Plane plane) {
manager.getPlaneList().add(plane);
}
public void visit(Boat boat) {
manager.getBoatList().add(boat);
}
}
I highly suggest removing the getter methods and declaring overloaded add methods for each type of vehicle. This will reduce overhead from "visiting" when it's not needed, for example, manager.add(new Car()):
class VehicleManager {
private List<Car> carList = new ArrayList<>();
private List<Boat> boatList = new ArrayList<>();
private List<Plane> planeList = new ArrayList<>();
private AddTransportVisitor addVisitor = new AddTransportVisitor(this);
public void add(Vehicle vehicle) {
vehicle.accept(addVisitor);
}
public void add(Car car) {
carList.add(car);
}
public void add(Boat boat) {
boatList.add(boat);
}
public void add(Plane plane) {
planeList.add(plane);
}
public void printAllVehicles() {
//loop through vehicles, print
}
}
class AddTransportVisitor implements VehicleVisitor {
private VehicleManager manager;
public AddTransportVisitor(VehicleManager manager) {
this.manager = manager;
}
public void visit(Car car) {
manager.add(car);
}
public void visit(Plane plane) {
manager.add(plane);
}
public void visit(Boat boat) {
manager.add(boat);
}
}
public class Main {
public static void main(String[] args) {
Vehicle[] vehicles = {
new Plane(),
new Car(),
new Car(),
new Car(),
new Boat(),
new Boat()
};
VehicleManager manager = new VehicleManager();
for(Vehicle vehicle : vehicles) {
manager.add(vehicle);
}
manager.printAllVehicles();
}
}
You can add method to vehicle class to print the text. Then override the method in each specialized Car class. Then just add all the cars to the vehicle list. And loop the list to print the text.
I'm not too happy with the lists of cars, boats and planes in the first place. You have multiple examples of reality but the list isn't inherently all-inclusive--what happens when your factory starts making submarines or rockets?
Instead, how about an enum with the types car, boat and plane. You have an array of lists of vehicles.
The generic vehicle has an abstract property CatalogAs, the various vehicles actually implement this and return the proper value.
Done some restructuring of your code. Hope that works for you. Check this:
import java.util.ArrayList;
class VehicleManager {
public static void main(String[] args) {
ArrayList<ABluePrint> bluePrints = new ArrayList<ABluePrint>();
ArrayList<AVehicle> allVehicles = new ArrayList<AVehicle>();
ArrayList<ACar> cars = null;
ArrayList<ABoat> boats = null;
ArrayList<APlane> planes = null;
/*
* In my application I have to access the blueprints through an API
* b/c they have already been created and stored in a data file.
* I'm creating them here just for example.
*/
ABluePrint bp0 = new ABluePrint(0);
ABluePrint bp1 = new ABluePrint(1);
ABluePrint bp2 = new ABluePrint(2);
bluePrints.add(bp0);
bluePrints.add(bp1);
bluePrints.add(bp2);
for (ABluePrint bp : bluePrints) {
AVehicle v = AVehicleFactory.buildVehicle(bp);
allVehicles.add(v);
// Can I accomplish this without using 'instanceof'?
// dont add objects to list here, do it from constructor or in factory
/*if (v instanceof ACar) {
cars.add((ACar) v);
} else if (v instanceof ABoat) {
boats.add((ABoat) v);
} else if (v instanceof APlane) {
planes.add((APlane) v);
}*/
}
cars = ACar.getCars();
boats = ABoat.getBoats();
planes = APlane.getPlanes();
System.out.println("All Vehicles:");
for (AVehicle v : allVehicles) {
System.out.println("Vehicle: " + v + ", maxSpeed: " + v.maxSpeed);
}
System.out.println("Cars:");
for (ACar c : cars) {
System.out.println("Car: " + c + ", numCylinders: " + c.numCylinders);
}
System.out.println("Boats:");
for (ABoat b : boats) {
System.out.println("Boat: " + b + ", numRudders: " + b.numRudders);
}
System.out.println("Planes:");
for (APlane p : planes) {
System.out.println("Plane: " + p + ", numPropellers: " + p.numPropellers);
}
}
}
class AVehicle {
double maxSpeed;
AVehicle(double maxSpeed) {
this.maxSpeed = maxSpeed;
}
void add(){}
}
class ACar extends AVehicle {
static ArrayList<ACar> cars = new ArrayList<ACar>();
int numCylinders;
ACar(double maxSpeed, int numCylinders) {
super(maxSpeed);
this.numCylinders = numCylinders;
}
void add(){
cars.add(this);
}
public static ArrayList<ACar> getCars(){
return cars;
}
}
class ABoat extends AVehicle {
static ArrayList<ABoat> boats = new ArrayList<ABoat>();
int numRudders;
ABoat(double maxSpeed, int numRudders) {
super(maxSpeed);
this.numRudders = numRudders;
}
void add(){
boats.add(this);
}
public static ArrayList<ABoat> getBoats(){
return boats;
}
}
class APlane extends AVehicle {
static ArrayList<APlane> planes = new ArrayList<APlane>();
int numPropellers;
APlane(double maxSpeed, int numPropellers) {
super(maxSpeed);
this.numPropellers = numPropellers;
}
void add(){
planes.add(this);
}
public static ArrayList<APlane> getPlanes(){
return planes;
}
}
class AVehicleFactory {
public static AVehicle buildVehicle(ABluePrint blueprint) {
AVehicle vehicle;
switch (blueprint.type) {
case 0:
vehicle = new ACar(100.0, 4);
break;
case 1:
vehicle = new ABoat(65.0, 1);
break;
case 2:
vehicle = new APlane(600.0, 2);
break;
default:
vehicle = new AVehicle(0.0);
}
vehicle.add();
return vehicle;
}
}
class ABluePrint {
int type; // 0 = car; // 1 = boat; // 2 = plane;
ABluePrint(int type) {
this.type = type;
}
}
With the above code, the class will have to know about the collection to which it has to be added. This can be considered as a downside to a good design and it can be overcome using the visitor design pattern as demonstrated in the accepted answer (How to avoid 'instanceof' when implementing factory design pattern?).
I know its been a long time since this question was asked. I found http://www.nurkiewicz.com/2013/09/instanceof-operator-and-visitor-pattern.html which looks to be useful. Sharing it here in case if somebody is interested.
Had a similar issue so I used this pattern, to understand it better I created a simple UML drawing showing the sequence of things in comments (follow the numbers). I used Vince Emighs solution above.. The pattern solution is more elegant but can requires some time to truly understand. It requires one interface and one class more then the original but they are very simple.
What if AVehicle classes are out of your control? E.g. you have it from some 3rd party lib? So you have no way to add the Visitor pattern accept() method. Also you could probably dislike boilerplate code in each of the AVehicle subclass and prefer to put everything in one special class keeping your classes clean.
For some cases it could be better just to use HashMap.
In your sample just use:
Map<Class<? extends AVehicle>, List<? extends AVehicle>> lists = new HashMap<>();
lists.put(ACar.class, new ArrayList<ACar>());
lists.put(ABoat.class, new ArrayList<ABoat>());
lists.put(APlane.class, new ArrayList<APlane>());
for (ABluePrint bp : bluePrints) {
AVehicle v = AVehicleFactory.buildVehicle(bp);
allVehicles.add(v);
lists.get(v.getClass()).add(v);
}
The problem with this HashMap approach is that you have to register all possible classes including all known subclasses. Although if you have huge hierarchy and it is not needed all classes for your task you can save lots of work registering in the Map just needed ones.
I want to, within my abstract class, define two constructors.
When create a new instance of the class, i want the toString to return something different depending on what was called:
The FireEngine Class
public class FireEngine extends EmergencyVehicle {
private String colour;
public FireEngine(String colour) {
super (colour);
}
public FireEngine() {
this("red");
}
public String toString () {
if (colour == "red") {
return "red";
} else
return "no";
}
}
The EmergencyVehicle class:
public abstract class EmergencyVehicle extends RoadVehicle {
public boolean codeBlue = false;
public EmergencyVehicle(String colour){
super(colour);
}
public boolean isEmergency () {
if (codeBlue == true) {
return true;
} else {
return false;
}
}
public void setEmergency(boolean newEmergency) {
codeBlue = newEmergency;
}
}
This is a homework exercise so I don't want the answer per se, but does the above code make sense?
For example, if I add a new EmergencyVehicle, I want an if statement depending on what colour the vehicle I add is.
1st Remark
Don't call
this("red");
in the default constructor, do
colour = "red";
unless the EmergencyVehicle(String colour) RoadVehicle(String colour) constructor is doing something else.
2nd Remark
Don't compare using
if (colour == "red")
use
if ("red".equals(colour))
3rd Remark
The method
public String toString()
is supposed to return a string representation of the instance. You implementation only returns red or no which is not very informative. Use something like
return("FireEngine(colour=" + colour + ")");
Let's say that I have property/field of a class Character that's called position which is basically two coordinates:
public class Character {
int[][] position;
public Character(int[][] position) {
this.position = position;
}
}
Let's say that I want characters to be able to interact if and only if their position is exactly the same, something like this:
public void interact() {
for character in all_characters {
if (this.position = character.position) {
// Do something
}
}
}
How would I get an array of all instantiated objects from that class, i.e. all_characters above.
Regarding getting properties.
It should be available if it's not private.
In java there is a practice to make properties private and implement public getter method for them.
Let me show you an example.
And regarding comparison.
I would suggest try to use deepEquals static method of Arrays class.
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepEquals(java.lang.Object[],%20java.lang.Object[])
file Character.java
public class Character {
private int[][] position;
public Character(int[][] position) {
this.position = position;
}
public int[][] getPosition() {
return position;
}
}
file Worker.java
public class Worker {
List<Character> all_characters = new ArrayList<Character>();
// all_characters.add(ch1);
// all_characters.add(ch2);
// all_characters.add(chN);
public void interact() {
for (chLeft in all_characters) {
for (chRight in all_characters) {
if(java.util.Arrays.deepEquals(chLeft.getPosition(), chRight.getPosition())) {
// Let's interact :)
}
}
}
}
This is not automagically done. You must keep track of entities, possibly decorating them with interfaces to be able to track such things, for example:
class Position
{
private int[][] data;
public boolean equals(Position other) { .. }
}
interface Positionable
{
public Position getPosition();
}
class Character implements Positionable
{
private Position position;
#Override public Position getPosition() { return position; }
}
class EntityGroup
{
List<Positionable> entities;
public Optional<Positionable> findEntityInSamePosition(Positionable p) {
return entities.stream().filter(p2 -> !p.equals(p2) && p.getPosition().equals(p2.getPosition())).findfirst();
}
}
I'm currently working on a game using the Component Pattern, and have always wondered how this is done.
I have an entity which is practically just a bag of components. Each components extends the Component class, which just has some basic functionality.
Extending the component class, new components are created, for handeling input, graphics etc. Now comes the problem; When I'm trying to get a specific component from the entity, it always returns the basic Component class, which prevents me from using the specific component functions.
public class GameEntity
{
private ArrayList<Component> components;
public GameEntity()
{
components = new ArrayList<Component>();
}
public void addComponent(Component component)
{
components.add(component);
}
public void update()
{
}
public Component getComponent(Class type)
{
for (Component component : components)
{
if(component.getClass() == type)
{
//return component as Class;
}
}
return null;
}
public void draw(Canvas canvas)
{
for (Component component : components)
{
component.update();
component.draw(canvas);
}
}
}
Some example components:
public class GraphicsComponent extends Component {
public Bitmap bitmap;
public Rect currentFrameRect;
private ArrayList spriteAnimations;
public SpriteAnimation currentAnimation; public int x = 0; public int y = 50; public
GraphicsComponent() {
spriteAnimations = new ArrayList(); }
/**
* Adds image [converts to spriteanimation]
* #param image
*/
public void addImage(Bitmap image, String label)
{
Rect[] tmpRects = {new Rect(0, 0, image.getWidth(), image.getHeight())} ;
addAnimation(new SpriteAnimation(
image, tmpRects, label
));
}
public void addAnimation(SpriteAnimation spriteAnimation)
{
spriteAnimations.add(spriteAnimation);
if(currentAnimation == null)
{
currentAnimation = spriteAnimation;
}
}
#Override
public void update()
{
currentFrameRect = currentAnimation.frames[currentAnimation.currentFrame];
}
#Override public void draw(Canvas canvas) {
if(currentAnimation != null)
{
currentAnimation.draw(x, y, canvas);
} }
public int getWidth()
{
return currentAnimation.frames[currentAnimation.currentFrame].width();
}
public int getHeight()
{
return currentAnimation.frames[currentAnimation.currentFrame].height();
}
}
public class InteractiveComponent extends Component
{
public GraphicsComponent graphics;
public InteractiveComponent(GraphicsComponent graphics)
{
this.graphics = graphics;
}
public boolean isOver(int tapX, int tapY)
{
//left top right bottom
if(tapX > graphics.x && tapX < graphics.x + graphics.getWidth() &&
tapY > graphics.y && tapY < graphics.y + graphics.getHeight()
)
{
return true;
}
return false;
}
}
There seem to be some problems with the code formatting, but it should be clear.
I can't access any specific functions like getHeight() in the graphicComponent or isOver() from the interactiveComponent because I just get a basic Component returned.
I would like to return a GraphicsComponent or InteractiveComponent based on the Class that I pass into getComponent().
What you're trying to do seems architecturally suspect to me, but nonetheless it can be done in a "clean" way using generics, and you're already passing in the class object as a type token:
class Animals {
List<Animal> animals = new ArrayList<Animal>();
public <T extends Animal> T getAnimal(Class<T> type) {
for (Animal animal : animals) {
if (type.isAssignableFrom(animal.getClass())) {
return type.cast(animal);
}
}
return null;
}
}
class Main {
public static void main(String[] args) {
Animals animals = new Animals();
animals.getAnimal(Cat.class).meow();
animals.getAnimal(Dog.class).bark();
}
}
interface Animal {}
class Cat implements Animal {
public void meow() {}
}
class Dog implements Animal {
public void bark() {}
}
You can == or equals() instead of the isAssignableFrom() check depending on the exact behaviour you want.
Others have suggested downcasting as a solution. I would say: redesign.
If you find yourself needing to downcast, then it indicates you probably have an architectural problem. And indeed, you have. You want to access each object, but not in a polymorphic way. So trying to access specific objects via a container of the supertype is unnatural/unhelpful/inefficient.
I would suggest:
class GameEntity {
private GraphicsComponent graphics;
private InteractiveComponent interactive;
private SoundComponent sound;
private List<Component> components;
public GameEntity() {
components.add(graphics);
// etc.
}
public GraphicsComponent getGraphics() { return graphics; }
// etc.
public void draw() {
for (Component c : components) {
...
}
}
}
Once you have received a component in this manner, you would have to cast it back to the class type. Then you could access the getHeight() etc. The object is simply not known to be a graphicComponent but it is.
Well, the function returns a Component. If you know you have requested a specific component, cast the result:
GraphicsComponent gc = (GraphicsComponent)(gameEntityInstance.getComponent(GraphicsComponent.class));
Your best bet would be to cast when you call getComponent() and remove the Class argument from that method.
So, getComponent will return a Component which can be cast into either InteractiveComponent or GraphicsComponent:
public Component getComponent() { /* its contents */ };
GraphicsComponent gc = (GraphicsComponent)...getComponent();
InteractiveComponent ic = (InteractiveComponent)...getComponent();