Java design pattern-simple factory pattern

Source: Internet
Author: User

In a simple factory mode, the factory class dynamically determines which product class instance should be created based on the parameters passed in by the factory method.

Code:

// Abstract product class TVpublic interface TV {public void play ();}

// Specific product class public class HaierTV implements TV {@ Overridepublic void play () {System. out. println ("Haier TV playing .......... ");}}

// Specific product class: public class HisenseTV implements TV {public void play () {System. out. println ("Hisense TV playing ........ ");}}

// The factory class produces different sets of public class TVFactory {public static TV producedTV (String brand) throws Exception {if (brand. equalsIgnoreCase ("Haier") {System. out. println ("the TV factory produces Haier TV sets! "); Return new HaierTV ();} else if (brand. symbol signorecase (" Hisense ") {System. out. println (" TV factory produces Hisense TV! "); Return new HisenseTV ();} else {try {throw new Exception (" sorry, this brand of TV cannot be produced now! ");} Catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} return null ;}}

Import java. io. file; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. w3c. dom. document; import org. w3c. dom. node; import org. w3c. dom. nodeList; public class XMLUtilTV {// XML operation tool class public static String getBrandName () {try {// create Document Object DocumentBuilderFactory dFactory = DocumentBuilderFactory. newInstance (); DocumentBuilder dBuilder = dFactory. newDocumentBuilder (); Document doc; doc = dBuilder. parse (new File ("configTV. xml "); // obtain the text node NodeList nl = doc that contains the brand name. getElementsByTagName ("brandName"); Node classNode = nl. item (0 ). getFirstChild (); String brandName = classNode. getNodeValue (). trim (); return brandName;} catch (Exception e) {// TODO: handle has tione. printStackTrace (); return null ;}}}

Public class Client {// test class public static void main (String [] args) {try {TV TV; // abstract type definition // obtain the brand String brandName = XMLUtilTV. getBrandName (); // obtain the specific product TV = TVFactory. producedTV (brandName); TV. play () ;}catch (Exception e) {// TODO: handle exception }}}


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.