Design Model-simple factory model (study notes)

Source: Internet
Author: User

A simple factory is not actually a design pattern. It is more like a programming habit!

First, let's take a look at the code to understand this programming habit from the code.

For example:

Pizza class code:

public interface Pizza {    void prepare();    void box();    void bake();    void cut();}
Specific pizza CheesePizza:

public class CheesePizza implements Pizza {    private String tag="CheesePizza:";    @Override    public void prepare() {        System.out.println(tag+"pripare()");    }    @Override    public void box() {        System.out.println(tag+"box()");    }    @Override    public void bake() {        System.out.println(tag+"bake()");    }    @Override    public void cut() {        System.out.println(tag+"cut()");    }}
Specific pizza ClamPizza:

public class ClamPizza implements Pizza {    private String tag="ClamPizza:";    @Override    public void prepare() {        System.out.println(tag+"pripare()");    }    @Override    public void box() {        System.out.println(tag+"box()");    }    @Override    public void bake() {        System.out.println(tag+"bake()");    }    @Override    public void cut() {        System.out.println(tag+"cut()");    }}

Simple Factory:
public class SimplePizzaFactory {    public Pizza createPizza(String tpye){        Pizza pizza = null;        if (tpye.equals("cheese")){        pizza=new CheesePizza();        }else if (tpye.equals("clam")){         pizza=new ClamPizza();        }        return pizza;    }}
Pizza order type PizzaStore:

public  class PizzaStore {    SimplePizzaFactory simplePizzaFactory;    protected PizzaStore(SimplePizzaFactory simplePizzaFactory) {        this.simplePizzaFactory = simplePizzaFactory;    }    protected final Pizza orderPizza(String type){//        Pizza pizza=pizzafactory.createP(type);        Pizza pizza=simplePizzaFactory.createPizza(type);        pizza.prepare();        pizza.bake();        pizza.cut();        pizza.box();        return pizza;    }}

Test code:
  public static void main(String[] args){    PizzaStore chijiaPizzaStore=new PizzaStore(new SimplePizzaFactory());     chijiaPizzaStore.orderPizza("cheese");     chijiaPizzaStore.orderPizza("clam");    }

Print result:
CheesePizza:pripare()CheesePizza:bake()CheesePizza:cut()CheesePizza:box()ClamPizza:pripare()ClamPizza:bake()ClamPizza:cut()ClamPizza:box()
From the code, we can see that before creating an object, we abstract the common operation methods of the object to prepare for extension of the object. This interface can be an interface class or an abstract class, implemented or inherited by a specific subclass. However, the method in a simple factory only determines the specific object to be created.


Let's take a look at a common factory model framework code:

Abstract product category:

public abstract class Product {    public void method1(){    };    public abstract  void method2();}
Product

public class ConcreteProduct1 implements Product{        @Override        public void method2() {                    }    }public class ConcreteProduct2 implements Product{        @Override        public void method2() {                    }    }

Abstract Factory class:

Public abstract class Creator {/***** @ param c * @ param
 
  
The parameter type can be set by yourself. It is usually String, emum, Class, etc. It can also be blank * @ return */public abstract
  
   
T createProducts (Class
   
    
C );}
   
  
 
Specific factory type:

 public class ConcreteFactory extends Creator {               @Override        public 
 
   T createProducts(Class
  
    c) {            Product product=null;            try {                product= (Product) Class.forName(c.getName()).newInstance();            } catch (InstantiationException e) {                e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (ClassNotFoundException e) {                e.printStackTrace();            }            return (T) product;        }    }
  
 

Code test class:

 Creator creator=new ConcreteFactory();  Product product=  creator.createProducts(COncreteProduct2.class);

If a module only needs a factory class and there is no need to produce it, you can simply use the static method and directly replace the abstract factory class with a specific factory class, and use the static method.Simple factory Mode, OrStatic factory Mode.

I am not writing well. I hope to give more comments and learn together! Thank you.

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.