Friday, June 28, 2019

Latest Interview Questions And Answers

What are the differences between Abstract Factory and Factory design patterns?

Factory - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface. Is a simplified version of Factory Method
Factory Method - Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.
Abstract Factory - Offers the interface for creating a family of related objects, without explicitly specifying their classes.


The Difference Between The Two

The main difference between a "factory method" and an "abstract factory" is that the factory method is a single method, and an abstract factory is an object. I think a lot of people get these two terms confused, and start using them interchangeably. I remember that I had a hard time finding exactly what the difference was when I learnt them.
Because the factory method is just a method, it can be overridden in a subclass, hence the second half of your quote:
... the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.
The quote assumes that an object is calling its own factory method here. Therefore the only thing that could change the return value would be a subclass.
The abstract factory is an object that has multiple factory methods on it. Looking at the first half of your quote:
... with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition ...
What they're saying is that there is an object A, who wants to make a Foo object. Instead of making the Foo object itself (e.g., with a factory method), it's going to get a different object (the abstract factory) to create the Foo object.

Code Examples

To show you the difference, here is a factory method in use:
class A {
    public void doSomething() {
        Foo f = makeFoo();
        f.whatever();   
    }

    protected Foo makeFoo() {
        return new RegularFoo();
    }
}

class B extends A {
    protected Foo makeFoo() {
        //subclass is overriding the factory method 
        //to return something different
        return new SpecialFoo();
    }
}

And here is an abstract factory in use

class A {
    private Factory factory;

    public A(Factory factory) {
        this.factory = factory;
    }

    public void doSomething() {
        //The concrete class of "f" depends on the concrete class
        //of the factory passed into the constructor. If you provide a
        //different factory, you get a different Foo object.
        Foo f = factory.makeFoo();
        f.whatever();
    }
}

interface Factory {
    Foo makeFoo();
    Bar makeBar();
    Aycufcn makeAmbiguousYetCommonlyUsedFakeClassName();
}

//need to make concrete factories that implement the "Factory" interface here
Factory Method defines an interface for creating an object, but lets sub classes decide which of those to instantiate. A factory method lets classes defer instantiation to sub classes.
By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
The Abstract Factory looks very similar to the Factory Method. I have drawn a few UML classes to illustrate my point.
Note:
  • The diagram are from www.yuml.com so they are not perfectly oriented. But its a free service :).
  • The diagrams may not be perfect. I am still learning the GoF design patterns.
Factory Method:
Factory Method
Abstract Factory (only 1 member):
Abstract Factory (only 1 member)
Abstract Factory (more members):
alt text
Questions:
  1. If the Abstract Factory has only one creator and one product, is it still the Abstract Factory pattern? (an interface for creating families)
  2. Can the Factory Method concrete creator be created from an Interface or does it have to be from a class? (classes defer instantiations to sub classes)
  3. If the Abstract Factory can have only one creator and one product, is the only difference between the Abstract Factory and the Factory Method that the creator for the former is an Interface and the creator for the latter is a Class?


About EUREKA Server in Micro Services? 
Eureka Server is an application that holds the information about all client-service applications. Every Micro service will register into the Eureka server and Eureka server knows all the client applications running on each port and IP address.Eureka Server is also known as Discovery Server.
Zuul server  use in microservices? 
A common challenge when building microservices is providing a unified interface to the consumers of your system. ... To solve this problem, Netflix (a major adopter ofmicroservices) created and open-sourced its Zuul proxy serverZuul is an edge service that proxies requests to multiple backing services.


No comments:

Post a Comment

All Java 8 / J2EE Interview for Experienced

  1.       Functional Interface, Stream API? Functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces). As ...