The factory method pattern for Java Design patterns

Source: Internet
Author: User

LFactory Method Mode Overview?in Factory method mode, the abstract factory class is responsible for defining the interfaces that create objects, and the creation of specific objects is implemented by the concrete classes that inherit the abstract factory. LAdvantages?The client does not need to be responsible for the creation of objects, thus clarifying the responsibilities of each class, if there are new objects added, only need to add a specific class and the specific factory class, do not affect the existing code, late maintenance easy, enhance the system extensibilityLDisadvantages?Additional writing code required, increased workload

Animal abstract class: publicabstract Animal {public abstract void Eat ();}

Factory Interface: publicinterface Factory {public abstract Animalcreateanimal();}

Specific Dog Categories: Publicclass Dog extends Animal {}

Specific cat categories: Publicclass Cat extends Animal {}

at first, each specific content in the test class is created by itself, but the work of creating objects is more troublesome and requires someone to do it, so Has created a Special class to create objects. Find every modification ( mainly post-add ) Factory code too cumbersome, with factory method improvement, for each specific implementation to provide a specific factory.

Dog Factory: Publicclass dogfactory implements Factory {

public Animal createanimal() {...}

        }

Cat Factory: Publicclass catfactory implements Factory {

public Animal createanimal() {...}

        }


The specific code is as follows:
Animal Abstract class:
Package Creation _ factory method mode; public abstract class Animal {public abstract void Eat ();}

Dog class:
Package Creation _ factory method mode; public class Dog extends Animal {@Overridepublic void Eat () {System.out.println ("Dog eats Meat");}}

Cat class:
Package Creation _ factory method mode; public class Cat extends Animal {@Overridepublic void Eat () {System.out.println ("Cat eats Fish");}}

Factory Interface:
Package Creation _ factory method mode; public interface Factory {public abstract Animal createanimal ();}

The Dogfactory class that specializes in creating dog factories:
Package Creation _ factory method mode;//This factory mainly makes dogs, but others do not know, because is returned Animalpublic class Dogfactory implements Factory {@Overridepublic Animal Createanimal () {return new Dog ();}}
The Catfactory class that specializes in creating cat factories:
Package Creation _ factory method mode;//specially Created cat factory public class Catfactory implements Factory {@Overridepublic Animal createanimal () {return New Cat ();}}

Main method Animalmain class:
Package Creation _ factory method mode; public class Animalmain {public static void main (string[] args) {//requirement: I want to buy a dog. Factory factory = new DOGFAC Tory (); Animal Animal = Factory.createanimal (); Animal.eat (); System.out.println ("------------");//demand: I want to buy a cat factory = new catfactory ();//To Buy a cat, here the program will replace the factory with the factory to build the cat animal = Factory.createanimal (); Animal.eat ();}}




The factory method pattern for Java Design patterns

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.