Factory method Mode

Source: Internet
Author: User

The fourth chapter: Factory mode

The roles that the factory model contains:

1.Product (product)

2.Creator ( creator )

3.ConcreateProduct (Specific product)

4.ConcreateCreator (Specific creator)

Implementation process: Define abstract products, which can define abstract methods that needto be implemented in abstract products (product), where factory patterns are generally used in polymorphic form, such as:

New Xxxfactory ("XXX"); f.xxx ();

where F.xxx () is the method that you define in the product class, which can be understood to be used when your product is created, then it is defined here.

Then you need the abstract factory, where template mode is used, in the abstract factory class will have create (), this method, in this method can be put in the required parameters, can be understood as in the processing of products is needed to use the ' raw '!

If the factory model is used, then in general, there are more complex logic, in the Create () This method can go to the creation of products and ' processing '.

Specific product classes are necessary to inherit abstract products, in which relationships such as Lenovo ThinkPad inherit the computer so that the computer has a boot method, then ThinkPad to inherit it.

Specific factories, abstract factories only define abstract methods, is the parent of the specific factory, in the specific factory to carry out new objects, complete a series of processes defined in the abstract factory.

Code:

Abstract Factory:

Package example.factory.method;/** * is used to generate the product class and "process" the product @author Administrator * */public abstract class facto ry {public final Product create (String owner) {Product P = createproduct (owner); registerproduct (P); return p;} protected abstract void Registerproduct (Product p);p rotected abstract Product createproduct (String owner);}

  

Abstract Products:

Package example.factory.method;/*** * Defines abstract classes for the extraction method only, use * @author Administrator * */public abstract class Product {public AB stract void Use ();}


Specific Products:

Package example.factory.method;/** * Products that inherit the use usage feature of the product class. * @author Administrator * */public class Idcard extends Product{private String owner; @Overridepublic void Use () {System.ou T.println ("Using the" + owner + "ID card");} Idcard (String owner) {System.out.println ("make" + owner + "ID card"); this.owner = owner;} Public String GetOwner () {return owner;}}


Specific factory:

Package Example.factory.method;import Java.util.arraylist;import Java.util.list;public class IdCardFactory extends Factory{private list<string> owners = new arraylist<> (); @Overrideprotected void Registerproduct (Product p) {Owners.add ((Idcard) p). GetOwner ());} @Overrideprotected Product createproduct (String owner) {return new Idcard (owner);}}

Package Example.factory.method;public class Main {public static void main (string[] args) {Factory factory = new Idcardfact Ory (); Product card1 = factory.create ("test"); Card1.use ();}}

Factory method Mode

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.