How to expand the field of the base element?

I have a class (Base) and an InfoBase type field containing some information. specialization (Ext) of the base should contain additional information (InfoExt). Therefore, Ext assigns InfoExt to Base.info. However, I ran into problems when the Base replaces information, since it assigns info = new InfoBase() , therefore, additional InfoExt information is lost.

Therefore, I created abstract void assign() in the database (option A). In this event information should be sent to InfoExt every time it is used in Ext.

In option B, I additionally created abstract InfoBase info() .

  variant A variant B +----------------+ +---------------------------+ +----------------------------+ | InfoBase | | Base | | Base' | |----------------| |---------------------------| |----------------------------| | + name: String | | + info: InfoBase | | + abstract InfoBase info() | | | | + abstract void assign() | | + abstract void assign() | | | | | | | +----------------+ +---------------------------+ +----------------------------+ ^ ^ ^ | | | + + + +----------------+ +---------------------------+ +----------------------------+ | InfoExt | | Ext | | Ext' | |----------------| |---------------------------| |----------------------------| | + id: int | | + void assign() { | | + InfoExt info | | | | info = new InfoExt(); | | + InfoBase info() { | | | | } | | return info; | +----------------+ +---------------------------+ | } | | + void assign() { | | info = new InfoExt(); | | } | +----------------------------+ 

.

  class InfoBase { public String name; } abstract class Base { abstract public void assign(); abstract InfoBase info(); } class InfoExt extends InfoBase { public int id; } class Ext extends Base { public InfoExt info; @Override InfoBase info() { return info; } @Override public void assign() { info = new InfoExt(); } } 

This is a common situation with a general way, how to deal with it? Are there any flaws in the A / B option?

How can I provide an information field in Base that subclasses can use to store extended information?

Thank you for your attention

+7
source share
4 answers

Perhaps something like this is what you are looking for?

 abstract class Base { private InfoBase info; abstract public void assign(); protected void setInfo(InfoBase info) { this.info = info; } public InfoBase getInfo() { return info; } } class Ext extends Base { private InfoExt info; @Override public InfoExt getInfo() { return info; } @Override public void assign() { info = new InfoExt(); setInfo(info); } } 

This will allow the Base to serve InfoBase objects, and Ext can serve as an InfoExt .

+1
source

if you have one subclass of InfoBase, which will be accessed mainly by data, you should change the type of information "info" in the Base Class to InfoExt.

in the near future, perhaps you will have any other InfoBase subclasses with different data and behavior, how about you to do this? in this case, I advise you to use Bridge-Pattern GOF, this will help you transfer it.

0
source

If you are looking for a way to avoid casting, this might work (using generics):

 public abstract class Base<T extends InfoBase> { protected T info; public T info(){ return info; } public abstract void assign(); } public class ExtBase extends Base<InfoExt> { @Override public void assign() { info = new InfoExt(); } } 

The information will be of type InfoExt for the ExtBase subclass. Example)

 ExtBase b = new ExtBase(); b.info() // Returns Type InfoExt 
0
source

I do not quite understand your problem, but here is a hint:

As a rule, when creating basic subclasses, each subclass executes its specific instructions in the second method ( initialize or something like this), redefined by the subclass. So, for example, each subclass will create an instance of info using a special subclass of InfoBase .

After that, Ext should only redefine methods that require specific InfoBase behavior by adding info to the desired class.

This is the simplest design. Since I do not know the complexity of your code, I cannot give you a more specific solution; but you can also consider using an adapter template, strategy, or state (depending on the problem to solve, of course).

0
source

All Articles