Design Model II factory Model

Source: Internet
Author: User

Factory model definition:
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method determines that the instantiation of a class is delayed to its subclass.

The class diagram is as follows:

The common code is as follows:

//抽象产品类public abstract class Product {    // 产品通用的方法.....    public void method1(){            }        //抽象方法    public abstract void method2();}//--------------------------------------产品类------------------------------------------------//具体类 1public class ConcreteProduct1 extends Product {        @Override    public void method2() {        // 处理具体产品1的方法    }}//具体产品类2public class ConcreteProduct2 extends Product {    @Override    public void method2() {        //具体产品2的方法.....    }}//-----------------------------------创建类------------------------------------------//抽象的创建类public abstract class Creator {    //通过反射创建具体的产品类...    public abstract <T extends Product> T  createProduct(Class<T> c);}//具体的抽象类public class ConcreteCreator extends Creator {    @Override    public <T extends Product> T createProduct(Class<T> c) {        //产品类....        Product product = null;                if(product == null){            try {                product = (Product) Class.forName(c.getName()).newInstance();            }            catch (Exception  e) {                e.printStackTrace();            }        }                //返回.....        return (T)product;            }}//---------------------------------------------------------Client 类--------------------------------------------------------------------------public class Client {    /**      * @Title: main      * @Description: 工厂类的Client     * @param @param args    设定文件      * @return void    返回类型      * @throws      */    public static void main(String[] args) {        //工厂创建类        Creator  creator = new ConcreteCreator();        //生成具体产品类1        Product  product = creator.createProduct(ConcreteProduct1.class);                //继续业务处理    }}

 

 

Simple Factory

Remove the abstract class creator class from the class diagram, and change the concretecreator method to static.

Design Model II 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.