I am implementing my first Decorator pattern. The base class which I want to decorate has a member variable initialized in the constructor. The decorated class also has this member variable (since it is a descendant of the base class). My question is, should I initialize this member variable in the decorated class too, or use the member variable of the base class (which lives inside the decorated class)?
Here is some code. I'm just curious whether Decorated1 or Decorated2 is better?
public class Base{
private String memberVariable;
public Base(){
memberVariable = "";
}
public Base(String s){
memberVariable = s;
}
public String Description(){
//code here
}
}
public abstract class BaseDecorator(){
public abstract String Description();
}
public class Decorated1 extends BaseDecorator{
Base b;
public Decorated1(Base _b){
b = _b;
}
public String Description(){
//code here
}
public String getMemberVariable(){
return b.getMemberVariable();
}
}
public class Decorated2 extends BaseDecorator{
Base b;
public Decorated1(Base _b){
super(_b.getMemberVariable());
b = _b;
}
public String Description(){
//code here
}
public String getMembervariable(){
return memberVariable;
}
}
You have to figure out what this variable means for your class, or if it is really needed, but i would suggest that no.
interface IObject{
//declare methods
void doSomething();
}
class ObjectA implements IObject{
private int variable;
public void doSomething(){
}
}
class DecorateObject implements IObject {
private IObject decoratedObject;
public void doSomething(){
decoratedObject.doSomething();
//do more things
}
}
if IObject is a drawable element, it would have x,y coordinates that would be inherited so it is correct to put on a superclass, in this case it would be an abstract class.
interface IObject{
//declare methods
}
abstract class AbstractObject implements IObject{
private int xCoordinate;
}
class ObjectA extends AbstractObject {
}
class DecorateObject extends AbstractObject {
private IObject decoratedObject;
}
Related
We have base class as follow:
public class Base {
protected static string rule = "test_1";
public static getRule(){
/* get rule value from origin class*/
}
}
We have some classes that extend from base class. For example:
public class Derived extends Base {
static {
rule = "test_2";
}
}
Now we wants to get rule variable, but in some conditions:
If user call Derived.getRule(), it return test_2,
If in derived class rule variable not init, it returned test_1,
I don't want to override getRule in all subclasses for answer the question.
What do I do?
The problem is, that once the Derived class is used (initialized), Base.rule is changed, and everywhere now test_2 is returned, irrespective of the actual class.
So the technique has to be done without static (in that form). There is a categorical, class level value.
public class Base {
private static final String BASE_RULE = "test_1";
public String getRule() {
return BASE_RULE;
}
}
public class Derived extends Base {
private static final String DERIVED_RULE = "test_2";
#Override
public String getRule() {
return DERIVED_RULE;
}
}
Alternatively you can use marker interfaces - which are not mutual-exclusive however, hence not for some getCategory().
public class Base implements Test1Category {
}
public class Derived extends Base implements Test2Category { ... }
if (base instanceof Test2Category) { ... }
My main Class has 2 inner class, 1 of them is thread, I don't know how my inner class 2 can access (Or how to know var1 is true or false) value of inner class 1, this is my example, thanks!
public class InnerClass {
public class InnerClass1 implements NativeKeyListener {
public boolean var1;
}
public class InnerClass2 implements Runnable{
#Override
public void run() {
while (true) {
var1...
}
}
}
}
You cannot access nonstatic variables/methods/inner classes unless you have instantiated the object (i.e. created an instance of the object). You need an InnerClass1 object before you can store or get anything out of it. Until you do something like InnerClass1 foo = new InnerClass1(), there is no var1 anywhere.
Anyway, I think you are misusing inner classes. I'd suggest if you haven't already walking through the Java Tutorials Trail to get a basic idea of how classes, fields, and instantiation work in Java.
You can do it by an interface or class that is implemented by innerclass1. Try this:
public interface NativeKeyListener {
boolean a();
}
public class InnerClass {
static NativeKeyListener m() {
class InnerClass1 implements NativeKeyListener {
public boolean var1;
public boolean a() {
return var1;
}
}
return new InnerClass1();
}
public class InnerClass2 implements Runnable {
public void run() {
NativeKeyListener i = InnerClass.m();
i.a();
}
}
}
I just have a question, is there any way to access public methods from a class which is private from a different class? For Example the print method can be accessed from a different class since the class is private?
private class TestClass {
public void print() {
}
}
Yes there is.
You don't actually return an direct reference to your private class, since other classes can't use it. Instead, you extend some public class, and return your private class as an instance of that public class. Then any methods it inherited can be called.
public interface Printable {
void print();
}
public class Test {
public Printable getPrintable() {
return new PrintTest();
}
private class PrintTest implements Printable {
public void print() {
}
}
}
Test test = new Test();
test.getPrintable().print();
You can do that by extending that class with a public class. Or you can always use reflection!
I have tree classes.
class MyObject{
public void DoSomething()
{
here I need to call method add from class base.
}
}
class base
{
protected final void add(){}
}
class extended extends base {
private MyObject pObject = new MyObject();
...
{
pObject.DoSomething();
}
}
I could have created class for each variation that extends class extended, but the type what I need to use becomes available only after class extended is already initiated.
How do I call base.add() from MyObject inner method?
You can do it in a couple of ways:
Have a reference of your extended class in MyObject class. When you instantiate MyObject variable in extended class, pass it the reference of extended.
Something like this:
class MyObject{
private base baseObj;
public MyObject(base baseObj){
this.baseObj = baseObj;
}
public void DoSomething()
{
//here I need to call method add from class base.
//use baseObj to call the methods
}
}
class base
{
protected final void add(){}
}
class extended extends base {
private MyObject pObject;
...
public extended(){
pObject = new MyObject(this);
}
{
pObject.DoSomething();
}
}
Declare the methods in base class static. This way you can call the methods without requiring an instance of the base class.
Something like this:
class MyObject{
public void DoSomething()
{
//here I need to call method add from class base.
//call like this
base.add();
}
}
class base
{
protected static final void add(){}
}
class extended extends base {
private MyObject pObject;
...
public extended(){
pObject = new MyObject(this);
}
{
pObject.DoSomething();
}
}
One more thing: This is off-topic, but you might want to read about Java Naming Conventions. Having class names start with lowercase is something that you wouldn't find in the naming conventions.
dummy code like this:
class MyObject{
public void DoSomething(Base base)
{
base.add();
}
}
class extended extends base {
private MyObject pObject = new MyObject();
...
{
pObject.DoSomething(this);
}
}
I have a base class in Java. In that class I want to create a private class and I want to access the object of that private class in the base class. How can I do that?
Thanks in advance!
Do you mean this:
class Test {
private Inner inner = new Inner();
private class Inner {
public void foo() {}
}
// later somewhere
public void bar() {
inner.foo();
}
}
You can access an object of an inner class by creating it and remembering its reference. Just like an instance of any other class.
public enum Outer {;
private static class Nested {
private Nested() { }
}
public static Object getNested() {
return new Nested();
}
}
public class Main {
public static void main(String... args) {
System.out.println("I have an "+ Outer.newNested());
}
}
prints
I have an Outer$Nested#3f0ef90c
A good example is from Arrays. This creates an instance of a private nested class which implements a public interface which makes it useful.
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
/**
* #serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
PrivateClass c = new PrivateClass();
c.getSomeObject(); //??
You can use the above code in your base class; provided the private class is an inner class of your base class.
class Test {
private class Inner {
public void foo() {
System.out.println("vsahdashdashd");
}
}
// later somewhere
public void bar() {
new Inner().foo();
}
}
class javaapplication9 extends Test
{
public static void main(String[] args) {
Test inner = new Test();
inner.bar();
}
you can access private member from this type.