Design Model-factory Model

Source: Internet
Author: User

Design Model-factory Model
Start with reality

Remember what I mentioned earlierSimple factory Mode?
It still needs to be used in the client (main ).AnimalFactory.getAnimale(0)To select the animal type, when the new animal type is addedAnimalFactoryTo modify the code, which is equivalentBackgroundModify
.
Now we need to solve the problem by removing the switch statement in the Factory and any judgment logic, and simply using inheritance or polymorphism on the client to achieve animal selection.

First, define the basic nature of an animal (EAT, call, run, and sleep ). Interface Animal {public void eat (); public void shout (); public void run (); public void sleep ();}

Then achieve species diversification

class Dog implements Animal{    @Override    public void eat() {        System.out.println("dog eat");    }    @Override    public void shout() {        System.out.println("dog shout");    }    @Override    public void run() {        System.out.println("dog run");    }    @Override    public void sleep() {        System.out.println("dog sleep");    }}class Cat implements Animal{    @Override    public void eat() {        System.out.println("cat eat");    }    @Override    public void shout() {        System.out.println("cat shout");    }    @Override    public void run() {        System.out.println("cat run");    }    @Override    public void sleep() {        System.out.println("cat sleep");    }}

So far, there is no difference with the simple factory model. Focus:
Virtualize factories so that specific factories can produce specific animals, and all specific factories should have abstract factory features (attributes or methods ). The abstract factory is as follows:

interface Factory{    public Animal getAnimal();}

For the above animals, a corresponding animal factory is required:

class DogFactory implements Factory{    @Override    public Animal getAnimal() {        return new Dog();    }}class CatFactory implements Factory{    @Override    public Animal getAnimal() {        return new Cat();    }}

In the end, the client implements the following:

public static void main(String[] args){        Factory dogFac = new DogFactory();        Animal dog = dogFac.getAnimal();        Factory catFac = new CatFactory();        Animal cat = catFac.getAnimal();    }

Rather than in the simple factory method:

public static void main(String[] args){        List zoo = new ArrayList();        for(int i=0;i<4;i++)            zoo.add(AnimalFactory.getAnimale(0));        for(int i=0;i<2;i++)            zoo.add(AnimalFactory.getAnimale(1));        for(int i=0;i<4;i++)            zoo.add(AnimalFactory.getAnimale(2));        for(int i=0;i<2;i++)            zoo.add(AnimalFactory.getAnimale(3));        for(Animale animale:zoo){            //animale....        }    }

The advantage of doing so is that it fully reflects interface-oriented programming rather than implementation-oriented programming. This interface is equivalent to inserting a specific implementation into this interface. Even if something is wrong (DogFactory or CatFactory), Replace it directly, or modify the corresponding implementation class. If you need to add new species (goat, hen, etc ), you only need to insert GoatFactory or ChickenFactory on the Factory interface.
In fact, according to this idea,AnimalThe implementation of this interface can also be understood as inserting various specific animals on the Animal interface.

For example, on the computer's motherboard, there are various plug-ins, memory, hard disk, cpu, and so on. These are interfaces. interfaces are implemented accordingly, for example, memory stick, hard disk, and cpu of different specifications. When these implementations are broken, you can directly pull them down and change the plug-in, which is equivalent to the above example.FactoryInterfaceDogFactoryIf there is a problem, you can directly modify the DogFactory instead of the CatFactory and the main method of the motherboard.
Imagine if the computer motherboard is not a plug-in, but directly welded to the original, then when any bad, all need to repair the motherboard, then this loss will be huge. In other words, in the simple factory mode, modifying any animal (add or delete) will makeFactoryClass, the logic in the client main may also need to be modified.

To sum up, the factory model does reflect the idea of interface-oriented programming. It also makes better use of polymorphism and effectively decouples classes. It is a classic design model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.