mercredi 16 août 2017

Which design pattern to use (if exists)?

I have a question about OOP implementation and design patterns.

I have a fixed class model which I cannot change (because it is generated automatically each time the application starts). There are many classes there with equals fields like in example below: as you can see the fields city and streets are contained in the both classes.

public class A{
   String city;
   String street;

   String name;

   ....//get methods
}

public class B{
   String city;
   String street;

   String age;

   ....//get methods
}

I need to extract an address form the both types of classes and I want to implement it with one method (because it seems to be silly to write the same code twice). If the class model were changeable, I could add a new interface Addressable which A and B could implement.

public interface Addressable{

     public String getStreet();
     public String getCity();
}

//somewhere in code
public Address getAddress(Addressable addressable){
      return new Address(addressable.getCity(), addressable.getStreet());
} 

What is the most elegant way to implement the same without interface and without coding the same for different classes?

Aucun commentaire:

Enregistrer un commentaire