Design Model-simple factory Model

Source: Internet
Author: User

Design Model-simple factory Model
Start with reality

I want to raise some small animals and watch them grow up together. I choose cats, dogs, sheep, and chickens.

The general idea is to create four animal classes and instantiate them as follows:

class Dog{    public void shout(){        System.out.prinltn("dog shout");    }}class Cat{    public void shout(){        System.out.prinltn("cat shout");    }}class Chicken{    public void shout(){        System.out.prinltn("chicken shout");    }}class Goat{    public void shout(){        System.out.prinltn("goat shout");    }}public static void main(String[] args){        Dog dog = new Dog();        Cat cat = new Cat();        Goat goat = new Goat();        Chicken chicken = new Chicken();    }

As shown in the above Code, four animals are already very troublesome. The client main needs to be instantiated four times. What should I do if I open a zoo and instantiate the statement? You can't write as many animals as you can. If I hold more than a dozen animals and feed them every day, there is no doubt that the above clients will become messy.
This leads to a simple factory model.

interface Animale{    public abstract void shout();}class Goat implements Animale{    @Override    public void shout() {        System.out.println("goat shout");    }}class Chicken implements Animale{    @Override    public void shout() {        System.out.println("chicken shout");    }}class Dog implements Animale{    @Override    public void shout() {        System.out.println("dog shout");    }}class Cat implements Animale{    @Override    public void shout() {        System.out.println("cat shout");    }}class AnimalFactory{    public static Animale getAnimale(int kind){        Animale animale = null;        switch(kind){            case 0:                animale = new Dog();                break;            case 1:                animale = new Cat();                break;            default:                animale = null;        }        return animale;    }}

Therefore, the client can handle such a large group of animals as follows:

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....        }    }

Through the simple factory model, I can hold a large number of animals in my home, feed them together when I open meals, and ask for sleeping together when I go to bed.

This is a simple factory model that allows similar objects to come from the same abstract and produce these objects through factory classes, when operating these objects, you can use their parent class for unified operations. Of course, the client is needed to decide what type of objects to produce.
WeaknessesIn other words, every time a new species is added, the AnimalFactory factory class needs to be modified, and the switch selection statement needs to be modified to solve this problem.Abstract FactoryFor more information, see the factory 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.