Java - Generic Static Abstract functionality - java

import java.util.ArrayList;
public class Example {
public static abstract class item{
public static abstract int getID(String s);//<<< the problem
public abstract String doThingsWithID(int i);
}
public static class SpecificItem extends item{
int positive;
int negative;
public SpecificItem(int p,int n){
this.positive=p;
this.negative=n;
}
protected enum IDHelper{
POSITIVE(0),NEGATIVE(1);
final int i;
IDHelper(int i){this.i=i;}
}
public static int getID(String s){
return IDHelper.valueOf(s).i;
}
public String doThingsWithID(int i){
switch(i){
case 0:
return "positive="+String.valueOf(positive);
case 1:
return "negative="+String.valueOf(negative);
default:
return "shouldn't happen";
}
}
}
public class Container<T extends item>{
public ArrayList<T> l;
public int ID;
public Container(ArrayList<T> l,String id){
this.l=l;
this.ID=T.getID(id);
}
public void doThings(){
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i).doThingsWithID(ID));
}
}
}
public static void main(String[] args){
ArrayList<SpecificItem> l = new ArrayList<>();
l.add(new SpecificItem(1, -1));
l.add(new SpecificItem(2, -2));
Example main=new Example();
Container<SpecificItem> cont=main.new Container<>(l, "POSITIVE");
cont.doThings();
}
}
I have a code similar to this one. I want to be able to pass String s into a Container and forget about it, meaning give the Container constructor String s and have it do the getID() on its own. However, it appears I am forced to try to mimic the getID() functionality of Container constructor from outside, basically having a Container(ArrayList<item> l, int id) and expecting to pass in SpecificItem.getID(String s) to id, which I would ideally like to avoid. Any help would be greatly appreciated.
Edit: I primarily want the functionality of being able to link specific names to cases - basically the functionality of an enum - thus minimizing the amount of work if I want to rename a case.
And for anyone wondering, I am willing to modify most of the classes and methods, as long as I can retain the aforementioned functionality and can minimize the amount of "stuff" I need to pass into the function.
Also, I think the best option I have is to pass in a dummy new SpecificItem() that the Container constructor can use to do the T.getID(), and just have getID not be static. It feels kinda hacky though, so many there is a better option.

Related

Optimizing memory and LOC used when all class instances return the same value

I have a case where all instances of subclasses should return the same value.
Since there will be a lot of instances I want to reduce the amount of memory the use.
I made the following experiment:
1) Using fields and implementing the methods once. (This implementation uses 24 bytes)
private abstract class A{
private String string;
private int i;
public A(String string, int i) {
this.string = string;
this.i = i;
}
public String getName() {
return string;
}
public int getAge() {
return i;
}
}
private class B extends A{
public B() {
super("B",10);
}
}
2) Reimplementing methods for each class. (This implementation uses 16 bytes)
private abstract class A{
public abstract String getName();
public abstract int getAge();
}
private class B extends A{
#Override
public String getName() {
return "B";
}
#Override
public int getAge() {
return 10;
}
}
I read the object size using VisualVm.
Does anyone see a way I could implement the methods only once while reducing the amount of memory used?
Thank you in advance.
EDIT:
Since my question got downvoted a few times please let me know how I can improve it.
It is more subtle. In fact you want to have every child class to have its specific class (static) constant. A non-static method is used for that, as Java seems to offer no other means.
Though less neat-looking, the second solution seems more adequate. What is hard on my sense of nice code style.
I propose collecting all constants in an immutable class, and use either method, the second being nicer, though needing a method.
However one could do it following both intent of using a class static constant,
and optimal memory resp. not copying same data in every field:
class MetaData {
public final String string;
public final int i;
MetaData(String string, int i) {
this.string = string;
this.i = i;
}
}
Just one instance per child class. In fact one could consider not using child classes but a factory and delegation in class A.
private abstract class A {
protected final MetaData metaData;
public A(MetaData metaData) {
this.metaData = metaData;
}
public String getName() {
return metaData.string;
}
public int getAge() {
return metaData.i;
}
public MetaData getMetaData() {
return metaData;
}
}
private class B extends A {
private static final MetaData b = new MetaData("B", 10);
public B() {
super(b);
}
}
Of course instead of constructor+super field, one could better make
protected abstract MetaData getMetaData();
#Override
protected MetaData getMetaData() {
return b;
}
The advantage would be when later an extra constant has to be added in the hierarchy.

What design pattern to avoid overloading functions

I am currently writing a program that turns a String into an int
I got my own rules example : ("37dsqff" = 37) ("50 km/h" = 50)
The problem is the String can be any kind (StringBuffer, Vector, InputStream...) and i don't have any control on it.
So far I have made 1 parseInt() function for each one.
It looks this way :
public class Tools {
public static int parseInt(StringBuffer s)
{
...
return n;
}
public static int parseInt(Vector<Character> v)
{
...
return n;
}
....
}
I have noticed that every functions share too much similarities and I would like to use a design pattern to make it better and only have 1 parseInt function
I think about Visitor, template method but i don't know what s the best here.
The easiest (and generally simplest) technique is to find common interfaces and see if you can implement your function at that more general level.
Something like:
public static class Tools {
// CharSequence covers both String and StringBuilder.
public static int parseInt(CharSequence s) {
return 4;
}
// Use Iterable instead of Vector (Vector implements List).
public static int parseInt(Iterable<Character> v) {
return 6;
}
}
Once complete you can take it a little further by writing adaptors that transform one structure into another. This will make an Iterable<Character> out of a CharSequence.
// Make an Iterable<Character> from a CharSequence.
public static class CharWalker implements Iterable<Character> {
final CharSequence s;
public CharWalker(CharSequence s) {
this.s = s;
}
#Override
public Iterator<Character> iterator() {
return new Iterator<Character>() {
int i = 0;
#Override
public boolean hasNext() {
return i < s.length();
}
#Override
public Character next() {
return s.charAt(i++);
}
};
}
}
So now we can fold the two together into one:
public static class Tools {
// CharSequence covers both String and StringBuilder.
public static int parseInt(CharSequence s) {
// Forward to the Iterable version below.
return parseInt(new CharWalker(s));
}
// Use Iterable instead of Vector (Vector implements List).
public static int parseInt(Iterable<Character> v) {
return 6;
}
}

Getters and Setters

So, I have written a code that is supposed to take two ints -f and s (first and second) - and return the sum. Eclipse didn't really like what I was doing, so I changed my code and added some getters and setters - which I am very unfamiliar with. I got Eclipse to create my getters and setters. My code still doesn't work, but I don't really know why.
Here it is:
public class Main extends Comp{
public static void main(String[] args){
Comp.give(getF(), getS());
}
}
import java.util.Scanner;
//Feel the rhythm
//Feel the rhyme
//Come on Eclipse
//It's coding time!!!
public class Comp {
private int f;
private int s;
public void look(){
Scanner iscan = new Scanner(System.in);
setF(iscan.nextInt());
setS(iscan.nextInt());
iscan.close();
}
public static void give(int f, int s) {
System.out.println(f+s);
}
public int getS() {
return s;
}
public void setS(int s) {
this.s = s;
}
public int getF() {
return f;
}
public void setF(int f) {
this.f = f;
}
}
The Problem - Eclipse has underlined getF(), getS() (Main method only) in red. When I hover over it, it says change getF() to static [same for getS()], but I don't want it to be static.
It also used this.f and this.f. I kinda know what that means, but not too well. An explanation of that would be great.
this.f refers to the instance variable f in your Comp class. Since the parameter for setF(int f) is also called f, the this helps distinguish between the two. It's basically saying "assign the method parameter f to my instance variable f".
As for the error, you'd either need to make getF() and getS() static, or create an instance of your Comp class in main and call the two methods using that:
public static void main(String[] args){
Comp comp = new Comp();
Comp.give(comp.getF(), comp.getS());
}

What's the correct way to "share" variables between methods in different classes?

I'm trying to share variables between methods in different classes, but I don't know if I'm doing this in the correct way. Basically when I wanna use the variables on method2 I have to "transport" them throught method1 to method2 from the Class A, just take a look at the example because I don't know how to explain this properly.
Is this the correct way to do it? Because sometimes I do this over an over through methods and it looks ugly.
Example:
public class A {
int var1, var2, var3;
B b = new B();
b.method1(var1, var2, var3);
}
public class B {
public void method1(int var1, int var2, int var3){
//doSomething
method2(var2, var3);
}
public void method2(int var2, int var3){
//doSomething
}
}
Btw, is there any community where code reviews are done? I'm pretty new to code and I'm afraid that I'm creating code that isn't effective.
Thanks for the help! :)
Use getters and setters to get variable of Class A from B as following..
public class A {
private int var1, var2, var3;
public int getVar1(){
return var1;
}
public void setVar1(int var1){
this.var1 = var1;
}
public int getVar2(){
return var2;
}
public void setVar2(int var2){
this.var2 = var2;
}
public int getVar3(){
return var3;
}
public void setVar3(int var3){
this.var3 = var3;
}
}
public class B{
// Use var 1 from class A as following
A a = new A();
int x = a.getVar1(); //x has the value of Var1 now
a.setVar1(2); // var1 in class A has value 2 now.
}
Use interfaces rather than directly call a method of another class.
public class A {
private InterfaceB interfaceB;
interfaceB.method1(var1, var2, var3);
}
public interface InterfaceB{
public void method1(int var1, int var2, int var3);
public void method2(int var2, int var3);
}
public class B implements InterfaceB{
#Override
public void method1(int var1, int var2, int var3){
//doSomething
method2(var2, var3);
}
#Override
public void method2(int var2, int var3){
//doSomething
}
}
You should read about encapsulation.
Passing 3 variables encapsulated in 1 object with appriopriate accessors sounds like a better option to me (at least the code looks a bit cleaner).
Also, think of creating a utility class with static methods if it makes sense of course - sometimes you do not need class member fields at all because there is no state to this class (Math class is an example) and static methods that return the result of some calculation/transformation is a better option.
On a side note I can recommend you considering "Program to an interfaces" principle. You can read the relevant section right on the top of this page.
In B class, you declare a instance of A class, variables in A class is public. when you can use variable in A class.
Here is my 2 cents...
public class Sample {
//Property of the class
private int valueA;
//method to do some operation
//that relies explicitly on a
//property of the class
public void doSomething(){
//valueA is over 9000!?
int valueA = valueA + 9000;
}
//Method that does something that does not explicitly
//rely on a property of the class
//could be called from this or another class
public int doSomeOperationWithSomething(int something){
return something++;
}
}
Another alternative would be to create a static "utility" class for your methods
public class Utils{
public static int doMagic(int var){
return var * var;
}
}
used like this,
int num = Utils.doMagic(9);
These come about when you have some code that does that one useful thing, but you just can't figure out where to put it.
More importantly, you will want to maintain proper "encapsulation" (Read about that) in your code. This means limiting access to variables by other classes and allowing access to only what is needed.
public class Website {
//No one should ever be able to
//access this variable directly
//So we set it a private
private String article;
//A reader should be able to get the aricle
public String getArticle(){
return article;
}
//The reader should never be able to set
//an aticle on the website only read it
//You can leave this part out or
//set the method to private as i did.
private void setArticle(String article){
this.article = article;
}
}
public class Reader {
//Reference to website
private Website website;
public Reader(){
...
//the user can read an article
website.getArticle();
// but this is not available to them
website.setArticle("Some text"); // results in ERROR
}
}

polymorphic call depends on variable

I have the following problem. Am trying to make a polymorphic call and the result would depend on the variable that changes value depending on the underlying class. Tried different things however it doesn't work. Please let me know what should be changed. Problem is that although c.w reads both the local variable w, which is defaulted to 0 and reads the one from appropriate class it always defaults to 0. Here is the code:
class Cycle{
private int w = 0;
public void move(){
System.out.println("Cycle moving");
}
public int wheels(Cycle c){
switch (c.w){
case 1: return 1;
case 2: return 2;
case 3: return 3;
default: return 0;
}
}
}
class Unicycle extends Cycle{
public int w = 1;
public void go(){
System.out.println("Unicycle go");
}
}
class Bicycle extends Cycle{
public int w = 2;
public void go(){
System.out.println("Bicycle go");
}
}
class Tricycle extends Cycle{
public int w = 3;
public void go(){
System.out.println("Tricycle go");
}
}
public class TestCycle {
public static void ride(Cycle c){
c.move();
int now = c.wheels(c);
System.out.println(now);
}
public static void main(String[] args){
Bicycle b = new Bicycle();
ride(b);
Unicycle u = new Unicycle();
ride(u);
Tricycle t = new Tricycle();
ride(t);
}
}
Your problem (well one of them) is that you are redefining the class variable 'w' in each of your subclasses. Define it one as a member of 'Cycle' and have each subclass set it correctly in their constructors.
class Cycle{
protected int w;
public void move(){
System.out.println("Cycle moving");
}
public int wheels(){
return w;
}
}
class Unicycle extends Cycle{
public Unicycle() {
w = 1;
}
public void go(){
System.out.println("Unicycle go");
}
}
Or you can define an abstract method called 'wheels()' in the superclass and override it in the subclasses. It's a matter of taste.
the wheels method should be more like
public int getWheelCount(){
return this.w;
}
You invoke it on the instance itself, you don't need to pass an argument. If the current instance is a Tricycle, the method will return 3, etc...
Since Cycle.w is private, it's not visible from its inheritors. This means that for example Tricycle.w it's not the "same" variable, and it's not visible in Cycle (that's why you always get 0). You have to make Cycle.w at least protected, then remove w from all subclasses, and set its value in each subclass's constructor to what you want.
It's probably not the answer you are looking for, but the following works. Please give more details on what you are trying to do.
public abstract class Cycle {
protected int nWheels;
protected String goText;
// no constructor.
public void go() {
System.out.println(goText);
}
public int wheels() {
return nWheels;
}
}
...
public class Unicycle extends Cycle {
public Unicycle() {
nWheels = 1;
goText = "Unicycle go";
}
}
Note that I made Cycle abstract because I don't want it to ever be instantiated.
EDIT:
public static int getNumberOfWheels(Cycle cycle) {
return cycle.wheels();
}
which is obviously not very useful since a simple call to cycle.wheels() would do the same as calling this function.
I'm not sure why you want to avoid constructors. Maybe you should write the exact question you are trying to answer.

Categories

Resources