Whats the recommended design approach/alternative to the situation below:
BaseCalculator:
BaseType prepareData()
useData(BaseType)
Derived calculators use derived type to override base functionality -
DerivedCalculator1:
BaseType prepareData(){ return DerivedType1}
useData(BaseType t1){ DerivedType1 t=(DerivedType1)t1 //typecast down and proceed....}
DerivedCalculator2
BaseType prepareData(){ return DerivedType2}
useData(BaseType t1){ DerivedType2 t=(DerivedType2)t1 //typecast down and proceed....}
Is there a design approach to avoid typecasting by the derived classes - as it always leaves the gate open for a run-time mishap?
One alternative is to move the polymorphic behavior into the implementations of the BaseType rather than in the implementations of BaseCalculator. For example:
public interface BaseType {
public void process(Calculator calc);
}
public class DerivedType1 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 1
}
}
public class DerivedType2 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 2
}
}
public class Calculator {
public void doSomething(BaseType bt) {
bt.process(this);
}
}
If that type of solution is insufficient, a more complex solution is the Visitor Pattern. The Visitor Pattern allows any arbitrary BaseType object to be handled by any arbitrary BaseCalculator using double-dispatch. The catch is that all BaseCalculator implementations must have an method to handle each of the BaseType implementations. For example:
public interface BaseType {
public void process(Calculator calc);
}
public class DerivedType1 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 1
}
}
public class DerivedType2 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 2
}
}
public interface BaseCalculator {
public void handle(DerivedType1 dt);
public void handle(DerivedType2 dt);
}
public class DerviedCalculator1 implements BaseCalculator {
#Override
public void handle(DerivedType1 dt) {
dt.process(this);
}
#Override
public void handle(DerivedType2 dt) {
dt.process(this);
}
}
public class DerviedCalculator2 implements BaseCalculator {
#Override
public void handle(DerivedType1 dt) {
dt.process(this);
}
#Override
public void handle(DerivedType2 dt) {
dt.process(this);
}
}
Related
public abstract class CommonClass {
abstract void send(<what should i put here???>) {}
}
public class ClassA extends CommonClass {
void send(List<Comments> commentsList) {
// do stuff
}
}
public class ClassB extends CommonClass {
void send(List<Post> postList) {
// do stuff
}
}
I am new to OODP, I am trying to have a method that is able to take in any kind of List data so that I can abstract things out. How can i do this?
You could make it generic on some type T. Like,
public abstract class CommonClass<T> {
abstract void send(List<T> al);
}
And then, to implement it - use the generic. Like,
public class ClassA extends CommonClass<Comments> {
#Override
void send(List<Comments> commentsList) {
// do stuff
}
}
public class ClassB extends CommonClass<Post> {
#Override
void send(List<Post> postList) {
// do stuff
}
}
Also, as discussed in the comments, your class names could be improved to be more intuitive; something like,
public abstract class AbstractSender<T> {
abstract void send(List<T> al);
}
and then
public class CommentSender extends AbstractSender<Comment> {
#Override
void send(List<Comment> commentsList) {
// do stuff
}
}
public class PostSender extends AbstractSender<Post> {
#Override
void send(List<Post> postList) {
// do stuff
}
}
That has the advantage(s) of being more readable and easier to reason about (I can tell what a PostSender does by reading the name, ClassB not so much).
Finally, this looks like a case where an interface would work since your abstract class is purely virtual (and should be preferred since you can implement multiple interface, but can only extend from a single parent class);
public interface ISender<T> {
void send(List<T> al);
}
public class CommentSender implements ISender<Comment> {
#Override
void send(List<Comment> commentsList) {
// do stuff
}
}
public class PostSender implements ISender<Post> {
#Override
void send(List<Post> postList) {
// do stuff
}
}
In order to achieve this, you can take multiple approaches, I would suggest looking into Generics: https://docs.oracle.com/javase/tutorial/java/generics/index.html
With that said, there is one approach that is the most elegant and simple: you can supply a List<T> where T is a generic type.
public abstract class CommonClass<T> {
abstract void send(List<T>) {}
}
public class ClassA extends CommonClass<Comment> {
void send(List<Comments> commentsList) {
// do stuff
}
}
public class ClassB extends CommonClass<Post> {
void send(List<Post> postList) {
// do stuff
}
}
You can do that with the help of generics. https://www.tutorialspoint.com/java/java_generics.htm
Example
The abstract class
public abstract class CommonClass {
public abstract <T> void send(List<T> data);
}
Its child
public class Child extends CommonClass {
public <T> void send(List<T> data) {
// code here
}
}
Retrieving the list's contents
Retrieving the generified list's contents is similar to retrieving any list's contents. In the scope of the method, "T" is a type of object contained in the list.
for (T t : data) {
// to check if t is a string
if (t instanceof String) {
// code
}
}
You can also use lambdas to retrieve every element in the list.
I have the following GameObject interface:
public interface GameObject {
void viewDetails();
}
Character Interface:
interface Character{
void pickUp(Weapon weapon);
void use(Weapon weapon);
}
and abstract Weapon class:
public abstract class Weapon implements GameObject {
//left out constructor to focus on methods
#Override
public abstract void viewDetails();
public abstract void attack(Enemy enemyObj);
//Could be bullets, could be a mystical item.
public abstract void replenish(ReplenishItem rpItem);
}
The problem with this is, a GameObject sometimes can be used in different ways.
For example, the primary use of a game weapon is to attack a target, but what if I wanted to reload? How do I let my character interface reload or beware that reload is an option?
I would use the following approach.
I would declare interfaces:
interface MeleeWeapon {
void hit();
void cut();
}
interface FirearmWeapon {
void fire();
void reload();
}
interface MagicWeapon {
void throw();
void apply();
void recharge();
}
Then implement classes, like these:
class Knife implements MeleeWeapon {
public void hit() {
}
public void cut() {
}
}
class Dagger implements MeleeWeapon {
public void hit() {
}
public void cut() {
}
}
class GarandRifle implements FirearmWeapon {
public void fire() {
}
public void reload() {
}
}
class Fireball implements MagicWeapon {
public void throw() {
}
public void apply() {
}
public void recharge() {
}
}
Then, I would declare these interfaces:
interface MeleeWeaponUser {
void use(MeleeWeapon weapon);
}
interface FirearmWeaponUser {
void use(FirearmWeapon weapon);
}
interface MagicWeaponUser {
void use(MagicWeapon weapon);
}
And, I would declare character classes:
class Peasant implements MeleeWeaponUser {
public void use(MeleeWeapon weapon) {
}
}
class Marine implements MeleeWeaponUser, FirearmWeaponUser {
public void use(FirearmWeapon weapon) {
}
public void use(MeleeWeapon weapon) {
}
}
class Sorcerer implements MeleeWeaponUser, MagicWeaponUser {
public void use(MeleeWeapon weapon) {
}
public void use(MagicWeapon weapon) {
}
}
This approach let us add new weapons and characters without sufficient effort later.
In your use() method you can call reload() if there is no more ammo in the weapon dispenser.
But if your game character receives signal from outside, for example, reload the gun, even there is enough ammo to fire, then have an Event->Listener approach implemented.
Create a WeaponEvent class, extend this class to have FirearmWeaponEvent, MeleeWeaponEvent etc.
Make your game character class(es) as a listener to WeaponEvent events, then in your game character class have a method processEvent(WeaponEvent event), and act accordingly to the event you have received.
I've been wondering about the getThis() trick, and the alternative of the unsafe cast from a self-bounded type to its type parameter.
public abstract class SelfBound<T extends SelfBound<T>> {
protected abstract T getThis();
public void doSomething(T instance) { ... }
public final void doSomethingWithThis() { doSomething(getThis()); }
public final void doSomethingWithThisUnsafe() { doSomething((T) this); }
}
Is it possible to subclass SelfBound such that doSomethingWithThisUnsafe() throws a ClassCastException? (Is it possible to do this without subclassing SelfBound?)
Surely it's possible to have ClassCastException with subclassing. Here's a simple example:
public abstract class SelfBound<T extends SelfBound<T>> {
protected abstract T getThis();
public void doSomething(T instance) { }
public final void doSomethingWithThis() { doSomething(getThis()); }
public final void doSomethingWithThisUnsafe() { doSomething((T) this); }
public static class A extends SelfBound<A> {
#Override
protected A getThis() {
return this;
}
}
public static class B extends SelfBound<A> {
#Override
public void doSomething(A instance) {
super.doSomething(instance);
}
#Override
protected A getThis() {
return null;
}
}
public static void main(String[] args) {
new B().doSomethingWithThisUnsafe();
}
}
Output:
Exception in thread "main" java.lang.ClassCastException: SelfBound$B cannot be cast to SelfBound$A
at SelfBound$B.doSomething(SelfBound.java:1)
at SelfBound.doSomethingWithThisUnsafe(SelfBound.java:6)
at SelfBound.main(SelfBound.java:28)
It's not so clear what do you mean by "without subclassing SelfBound". As SelfBound is an abstract class, you cannot call its methods without subclassing it, thus you cannot have any exception when calling its methods.
I have interface:
interface operations{
void sum();
}
and I want to have classes:
class matrix implements operations {
#override
void sum(matrix m) {
}
}
class vector3D implements operations {
#override
void sum(vecor3D v) {
}
}
How to do this?
I tried something like this:
interface operations < T > {
<T> void sum(T t);
}
class matrix implements operations<matrix>{
#Override
void sum(matrix m){};
}
}
class vector3D implements operations<vector3D>{
#Override
void sum(vector3D v){};
}
but it doesn't work.
Don't add a type parameters to the interface and the type. Also you should specify the generic parameters of the interface you implement:
interface operations<T> {
void sum(T t);
}
class matrix implements operations<matrix> {
#Override
public void sum(matrix m){
}
}
class vector3D implements operations<vecor3D> {
#Override
public void sum(vecor3D v){
}
}
I created something like below:
ConcreteObserver1 is the subclass of IObserver
Interfaces definitions:
public interface ISubject<T,O extends IObserver<T>> {
public void addObserver(O observer);
public void removeObserver(O observer);
public void updateAllSubjects(T value);
}
public interface IObserver<T> {
public void update(T value);
}
public class ConcreteObserver1<T> implements IObserver<T> {
Concrete Subject class containing IObserver
public class ConcreteSubject<T, O extends IObserver<T>> implements
ISubject<T,O> {
public ConcreteSubject() {
addObserver(new ConcreteObserver1<T>());
}
ConcurrentSkipListSet<O> observersList = new ConcurrentSkipListSet<O>();
public void addObserver(O observer) {
observersList.add(observer);
addObserver(new ConcreteObserver1<T>()); not working.
It complains as below
The method addObserver(O) in the type ConcreteSubject is not applicable for the arguments (ConcreteObserver1)
Why ?
I told O extends IObserver<T> in ConcreteSubject type parameter definition, which says O is a sub-type of IObserver which is ConcreteObserver1
Why it is complaining.
The type parameter O is defined to be a subtype of IObserver<T>, this is correct. But it may stand for any subtype. You don't say anywhere that it stands for ConcreteObserver1. It could also stand for ConcreteObserver42 or something else.
From what you posted so far, it does not seem like you really have to define O as a type parameter of ConcreteSubject. You could specifically say that ConcreteSubject always uses a ConcreteObserver1. If this is not the case, you should probably explain your intention more clearly (and possibly, in a more readable form).
(Edited based on the comments)
interface ISubject<T,O extends IObserver<T>>
{
void addObserver(O observer);
void removeObserver(O observer);
void updateAllSubjects(T value);
}
interface IObserver<T>
{
void update(T value);
}
class ConcreteObserver1<T> implements IObserver<T>
{
#Override
public void update(T value) {}
}
class ConcreteObserver2<T> implements IObserver<T>
{
#Override
public void update(T value) {}
}
class ConcreteObserver3<T> implements IObserver<T>
{
#Override
public void update(T value) {}
}
class ConcreteSubject<T> implements ISubject<T,IObserver<T>>
{
ConcurrentSkipListSet<IObserver<T>> observersList =
new ConcurrentSkipListSet<IObserver<T>>();
public ConcreteSubject()
{
addObserver(new ConcreteObserver1<T>());
addObserver(new ConcreteObserver2<T>());
addObserver(new ConcreteObserver3<T>());
}
#Override
public void addObserver(IObserver<T> observer)
{
observersList.add(observer);
}
#Override
public void removeObserver(IObserver<T> observer)
{
observersList.remove(observer);
}
#Override
public void updateAllSubjects(T value)
{
for (IObserver<T> observer : observersList)
{
observer.update(value);
}
}
}