Analysis on the factory mode of JAVA design mode (1)

Source: Internet
Author: User

Analysis on the factory mode of JAVA design mode (1)
1 factory model Introduction

Definition of the factory mode: in short, it is used to instantiate an object and replace the new operation.

The factory mode is dedicated to instantiating a large number of classes with common interfaces. The working mode dynamically determines which class to instantiate. You do not need to know which class to instantiate each time.

The factory model can be divided into three forms:

Simple Factory mode: Also known as StaticFactory ).

Factroy Method: Also known as Polymorphic Factory ).

Abstract Factory mode: Also known as kit ).

1.1 simple factory mode (static factory mode ):

The simple factory model has three roles: 1. Abstract product interface 2. Specific product class 3. factory class.

1.2 Implementation of the simple factory mode (create a simpleFactory package and put all programs under this package ):

(1) first, create an abstract product interface (Computer. java ).

package simpleFactory;public interface Computer {public void run();}

(2) create three specific product categories (Dell. java, Lenovo. java, and Asus. java) and implement the Computer interface.

Package simpleFactory; public class Dell implements Computer {public void run () {System. out. println (Dell Computer running);} package simpleFactory; public class Lenovo implements Computer {public void run () {System. out. println (Lenovo Computer is running);} package simpleFactory; public class Asus implements Computer {public void run () {System. out. println (the Asus computer is running );}}

(3) create a factory class (ComputerFactory. java ).

package simpleFactory;public class ComputerFactory {public  static Computer makeComputer(String ComputerName)throws Exception {if(ComputerName.equals(dell)){  return new Dell();}else if(ComputerName.equals(lenovo)){  return new Lenovo();        }else if(ComputerName.equals(asus)){          return new Lenovo();        }else{  throw new Exception();        }}}

(4) create a client program (TestSimpleFactory. java) for testing.

package simpleFactory;public class TestSimpleFactory {public static void main(String []args)throws Exception{Computer computer=ComputerFactory.makeComputer(dell);computer.run();}}

Running output:

Dell Computer is running

 

The output shows that the factory class generates different objects based on different parameters and does not need to create new objects. This is the simple factory mode. In the simple factory mode, the client does not have to assume the responsibility for creating objects, but is only responsible for using objects. In this mode, the product is open and closed (open for expansion and closed for modification), but the factory does not comply with this principle, every time a brand computer is added, the code of the factory class should be modified, and the factory class has done everything. It is a god class. When it goes wrong, all things cannot run because they provide static methods and cannot be inherited. Therefore, they cannot provide factory roles with inherited hierarchical structures and correspond to products with hierarchical structures, so we need to improve the factory part, that is, the factory method model to be analyzed next.

(7) Finally, let's look at the UML diagram of the simple factory model.


 

Figure 1.1 simple factory 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.