How to access method in base class from child class in java - java

How to access parent class's methods from child class? Following is my class implementation:
public interface BaseUrl {
public String getNameSpace();
public String getUrl();
}
BaseSoapUrl Class:
public class BaseSoapUrl implements BaseUrl {
#Override
public String getNameSpace() {
return "https://host.com/AndroidWFC/";
}
#Override
public String getUrl() {
return "https://host.com/AndroidWFC/MobileWS.asmx";
}
}
SoapURL Interface:
public interface SoapURL {
public String getSoapAction();
public String getMethodName();
}
LoginSoap Class:
public class LoginSoap extends BaseSoapUrl implements SoapURL {
#Override
public String getSoapAction() {
return "https://host.com/AndroidWFC/UserControl";
}
#Override
public String getMethodName() {
return "UserControl";
}
}
For the sake of simplifying the code i want to implement them like this. earlier it was something like below :
public interface SoapURL {
public String getNameSpace();
public String getUrl();
public String getSoapAction();
public String getMethodName();
}
LoginSoap Class:
public class LoginSoap implements SoapURL {
#Override
public String getNameSpace() {
return "https://host.com/AndroidWFC/";
}
#Override
public String getUrl() {
return "https://host.com/AndroidWFC/MobileWS.asmx";
}
#Override
public String getSoapAction() {
return "https://host.com/AndroidWFC/UserControl";
}
#Override
public String getMethodName() {
return "UserControl";
}
}
and i could access these methods like below :
private SoapURL soapURL = new LoginSoap();
String static final url = soapURL.getUrl();
Now the return values form public String getNameSpace(); and public String getUrl(); are going to be same in all child classes ; so why to write code again and again. There are going to be many classes which is going to implements SoapURL interface because of web service is being used.
so my question is how to access methods which is in BaseSoapUrl through soapURL?

You can re-factor the SoapURL interface to be an abstract class, where you can provide some common implementation of the getNameSpace() and getUrl() methods and leave the others as abstract ones, so that the sub-classes will be forced to provide implementation for them.
public abstract class SoapURL {
public String getNameSpace() {
return "https://host.com/AndroidWFC/";
}
public String getUrl() {
return "https://host.com/AndroidWFC/MobileWS.asmx";
}
public abstract String getSoapAction();
public abstract String getMethodName();
}

I think your refactoring was not correct. If you consider that a SoapUrl must have a getNameSpace(), then define the getNameSpace() method in the interface.
If the purpose of your refactoring was to always return the same values for some of the SoapUrl's methods, I would suggest to follow this organization:
public interface SoapUrl {
String getNameSpace();
String getUrl();
String getSoapAction();
String getMethodName();
}
public abstract class BaseSoapUrl implements SoapUrl {
#Override
public String getNameSpace() {
return "https://host.com/AndroidWFC/";
}
#Override
public String getUrl() {
return "https://host.com/AndroidWFC/MobileWS.asmx";
}
}
public class LoginSoap extends BaseSoapUrl {
#Override
public String getSoapAction() {
return "https://host.com/AndroidWFC/UserControl";
}
#Override
public String getMethodName() {
return "UserControl";
}
}

Related

How to return a concrete implementation based on a variable(Enum/Object)

I was wondering how can I return a concrete implementation given a variable as argument in a function.
This is my test code
public interface Items {
String getName();
}
public class Car implements Items{
#Override
public String getName() {
return "Car";
}
public void drive(){
//To something
}
}
public class Shelf implements Items{
#Override
public String getName() {
return "Shelf";
}
public String getBooks(String bookName){
return bookName;
}
}
public enum Item {
CAR(Service::getCar),
TABLE(Service::getShelf),
;
Function<Service, ? extends Items> serviceFunction;
Item(Function<Service, ? extends Items> serviceFunction) {
this.serviceFunction = serviceFunction;
}
}
public class Service {
public Car getCar(){
return new Car();
}
public Shelf getShelf(){
return new Shelf();
}
public Items getItem(Item item){
return item.serviceFunction.apply(this);
}
public static void main(String[] args) {
Service service = new Service();
service.getItem(Item.CAR).getName();
// service.getItem(Item.CAR).drive(); // This is not valid.
}
}
So what I want is based on that enum I should be able to execute a set of functions related to that enum without passing the implementation identifier itself.
I know I can do this. And I will work but I was thinking of getting the concrete implementation without passing Class<T> klass.
public <T extends Items> T getItem(Item item, Class<T> klass){
return (T) item.serviceFunction.apply(this);
}
public static void main(String[] args) {
Service service = new Service();
service.getItem(Item.CAR, Car.class).drive();
}

Java abstract factory pattern

I'm trying to build a customer email generator in java using the abstract factory pattern. I understand how to use the factory method pattern; however, I'm a bit confused about the abstract factory pattern. I'm trying to generate an email based on customer type. Could you look at my code below and tell me if I'm using the abstract method correctly? Thank you
public abstract class EmailTemplate {
public abstract String getHeader();
public abstract String getBody();
public abstract String getFooter();
public String generateEmail(){
return getHeader()+"\n"+getBody()+"\n"+getFooter();
}
}
public interface EmailFactory {
EmailTemplate createEmail();
}
public class BusinessEmail extends EmailTemplate {
#Override
public String getHeader() {
return "Dear [business customer],";
}
#Override
public String getBody() {
return "Thank you for being our valued customer. We are so grateful for the pleasure of serving you and hope we met your expectations.";
}
#Override
public String getFooter() {
return "Best Regards," +
"[name]";
}
}
public interface EmailGeneratorFactory {
EmailTemplate createEmail();
}
public class BusinessFactory implements EmailGeneratorFactory {
#Override
public EmailTemplate createEmail() {
return new BusinessEmail();
}
}
public class EMailGenerationSystem {
private static EMailGenerationSystem EMailGenerationSystem = new EMailGenerationSystem();
private EMailGenerationSystem(){};
public static EMailGenerationSystem getInstance(){
return EMailGenerationSystem;
}
public EmailTemplate getEmail(EmailGeneratorFactory factory){
return factory.createEmail();
}
}

Creating a generic function in Java for builders of different implementations of interface

public interface A extends C {
String getCh();
String getId();
String getReview();
}
public interface B extends C {
String getCh();
String getId();
String getReview();
}
#Data
#Builder
public class AImpl implements A{
private String ch;
private String id;
private String review;
}
#Data
#Builder
public class BImpl implements B{
private String ch;
private String id;
private String review;
}
so now to use the builders of these I do:
return AImpl.builder()
.ch("ch")
.id("id")
.review("somerview");
For B I do:
return BImpl.builder()
.ch("ch1")
.id("id1")
.review("some new review");
Is there a way where I can make this builder part into a function? I dont like the idea of repeating the same code again. Like where I can pass id channel and review in a function and I can the object?
Disclaimer: I have never really dealt with builders so there might be a really much better option :D
This approach writes builders for each interface individually.
This does require that the interfaces provide a setter method.
Using generics, the methods of the RootBuilder and BaseABuilder return an instance of the ImplABuilder so that the chain can continue properly.
This is a very simple implementation of the Thistype generic which in other languages exists by default. This implementation also relies on casting to the actual Thistype but if you set the generics properly, that shouldnt be an issue.
public class Test
{
public static void main(String[] args)
{
ImplA implA = ImplA
.builder()
.id("id")
.description("description")
.valueA("a")
.build();
}
}
public interface Root
{
String getId();
void setId(String id);
String getDescription();
void setDescription(String description);
}
public class RootBuilder<Thistype extends RootBuilder<Thistype, Instance>, Instance extends Root>
{
protected final Instance object;
RootBuilder(Instance object)
{
this.object = object;
}
public Thistype id(String value)
{
object.setId(value);
return (Thistype)this;
}
public Thistype description(String value)
{
object.setDescription(value);
return (Thistype)this;
}
public Instance build()
{
return object;
}
}
public interface BaseA extends Root
{
String getValueA();
void setValueA(String valueA);
}
public class BaseABuilder<Thistype extends BaseABuilder<Thistype, Instance>, Instance extends BaseA> extends RootBuilder<Thistype, Instance>
{
protected Instance object;
BaseABuilder(Instance object)
{
super(object);
}
public Thistype valueA(String value)
{
object.setValueA(value);
return (Thistype)this;
}
}
public interface BaseB extends Root
{
String getValueB();
void setValueB(String valueB);
}
public interface BaseC extends Root
{
String getValueC();
void setValueC(String valueC);
}
public final class ImplA implements BaseA
{
private String id;
private String description;
private String valueA;
private ImplA() { }
public static ImplABuilder builder()
{
return new ImplABuilder(new ImplA());
}
private static class ImplABuilder extends BaseABuilder<ImplABuilder, ImplA> // assuming ImplA is final
{
ImplABuilder(ImplA object)
{
super(object);
}
// additional methods for ImplA class
}
}

Generics and inheritance in java

I have a tiny problem using (what I assume are) generics. I have this code:
public class A{
private String name;
public String getName(){
return this.name;
}
}
public class B extends A{
private String street;
public String getStreet(){
return this.street;
}
}
public class C extends A{
private int number;
public int getNumber(){
return this.number;
}
}
And I'd like to create new classes that will look like this :
public class AChange{
private A instance;
public String doSomething(){
return A.getName();
}
}
public class BChange extends AChange{
public String street(){
return A.getStreet();
}
}
public class CChange extends AChange{
public int number(){
return A.getNumber();
}
}
And of course, A class doesn't have those methods, but the subclasses do. How can I write this code, so it will work the way I want it to?
Add a generic type parameter to AChange to be used as type of field instance:
class AChange<T extends A> {
protected T instance;
public String doSomething() {
return instance.getName();
}
}
and define it in BChange and CChange accordingly
class BChange extends AChange<B> {
public String street() {
return instance.getStreet();
}
}
class CChange extends AChange<C> {
public int number() {
return instance.getNumber();
}
}
You can do the same without generics like so
static class AChange {
private A instance;
public AChange(A instance) {
this.instance = instance;
}
public String doSomething() {
return instance.getName();
}
}
static class BChange extends AChange {
private B instance;
public BChange(B instance) {
super(instance);
this.instance = instance;
}
public String street() {
return instance.getStreet();
}
}
static class CChange extends AChange {
private C instance;
public CChange(C instance) {
super(instance);
this.instance = instance;
}
public int number() {
return instance.getNumber();
}
}
Instead of using a generic instance T, store a reference of the right type

How does Decorator pattern work in Java?

I was trying to understand Decorator Pattern. Below is the code am trying to understand how it works.
public static void main(String[] args)
{
Room myRoom = new CurtainDecorator(new ColorDecorator(new SimpleRoom()));
System.out.println(myRoom.showRoom());
}
Below is my Concrete Class
public class SimpleRoom implements Room{
#Override
public String showRoom()
{
return "show room";
}
}
Below is my abstract Decorator class
public abstract class RoomDecorator implements Room{
public Room roomReference;
#Override
public String showRoom()
{
return roomReference.showRoom();
}
}
Below is my Decorator implementation1
public class ColorDecorator extends RoomDecorator{
#Override
public String showRoom()
{
return addColors(); //How does showRoom() method gets invoked here?
}
public ColorDecorator(Room room)
{
this.roomReference = room;
}
public String addColors()
{
return "Blue";
}
}
Below is my Decorator implementation 2
public class CurtainDecorator extends RoomDecorator{
public CurtainDecorator(Room room)
{
this.roomReference = room;
}
#Override
public String showRoom()
{
return this.roomReference.showRoom() + addCurtains(); //What will showRoom method invoke?
}
public String addCurtains()
{
return "Curtain";
}
}
Output is - BlueCurtain
My question are placed in the comment..
In the end you have:
CurtainDecorator(ref=ColorDecorator(ref=SimpleRoom)))
When you call showRoom from main, it calls the method of CurtainDecorator, which in turn first goes to it's reference (ColorDecorator in this case) that outputs 'Blue', then CurtainDecorator adds it's bit 'Curtain'.

Categories

Resources