1. Mode:
The simple Factory mode is the class creation mode, also called the Static Factory Method mode.
It is determined by a factory object to create a product instance.
2. Three roles:
1. Creator role: create a product object under direct calls from the client
2. Abstract Product role: it can be implemented using an interface or an abstract class.
3. Specific product role: Any object created in the factory method mode is an instance of this role.
3. If the factory method always uses the same product object cyclically, this factory object can use an attribute to store this product object.
Each time a client calls a factory method, the factory method always provides the same object.
If the factory method always uses a fixed number of product objects, and the number of such product objects is not large,
You can use private attributes to store references to these product objects. For example, a factory object that always provides only one product object
You can use a static variable to store references to this product object.
If the number of factory methods is uncertain or a large number of product objects are used, it is inconvenient to use Attribute variable storage to reference these product objects,
In this case, we should use clustered object storage to reference product objects.
Java implementation code:
Package com. easyfactory. model; /*** define an interface for takeout ** @ author Administrator **/public interface WaiMai {/*** to define a method for eating and drinking */public void eat (); public void drink ();}
Package com. easyfactory. model;/*** model 1 * @ author Administrator **/public class Model1 implements WaiMai {public void eat () {System. out. println ("fruit bar"); return;} public void drink () {System. out. println ("juice"); return ;}}
Package com. easyfactory. model;/*** model 2 * @ author Administrator **/public class Model2 implements WaiMai {public void eat () {System. out. println ("Fried Rice Flour"); return;} public void drink () {System. out. println ("beer"); return ;}}
Package com. easyfactory. model;/*** model 3 * @ author Administrator **/public class Model3 implements WaiMai {public void eat () {System. out. println ("beef noodle"); return;} public void drink () {System. out. println ("Black Tea"); return ;}}
Package com. easyfactory. model;/*** Factory class. Create an instance of the target object * @ author Administrator **/public class Factory {public static WaiMai newWaiMai (String s) {if (s. equals ("dongmen") return new Model1 (); else if (s. equals ("South Gate") return new Model2 (); else return new Model3 ();}}
Package com. easyfactory. model;/*** customize an Exception class * @ author Administrator **/public class WaiMaiException extends Exception {public WaiMaiException (String s) {super (s );}}
Test Cases
Test Case: package com. easyfactory. model;/*** Test * @ author Administrator **/public class Test {public static void main (String [] s) throws WaiMaiException {WaiMai obj = null; System. out. println ("how to do tonight's supper"); obj = Factory. newWaiMai ("North Gate"); obj. eat (); obj. drink ();}}
Result:
What should I do if I have beef noodles and black tea tonight?