Design Mode-simple factory Mode

Source: Internet
Author: User

Recently, we have seen the shadows of design patterns in many occasions. We have been investing in algorithms and data structures for a long time. It is very important to find the design patterns. Sometimes code maintenance, reuse, and scalability are better than pure algorithms. So I picked up Daniel's book "big talk Design Patterns" and referred to the blogs of various big cows on the Internet to start my design patterns journey.

Today, we will introduce the simple factory model.

Overview of simple factory models:

Define a class to create instances of other classes. The created instance usually has a common parent class (generally abstract class or interface), Simple Factory) mode is also called the Static Factory Method mode.


Step 1: Abstract The product parent class (interface or abstract class as parent class)
Step 2: specific product subclass (Implementation class, implementation Interface)

Step 3: factory class (core, return object)

Advantages and disadvantages:

In the simple factory model, the factory class is the key to the entire model. It contains the necessary judgment logic and determines which class of instance to create based on the information given by the outside world, you do not need to pay attention to the creation of objects. You only need to be responsible for "consuming" objects. This clearly distinguishes responsibility and facilitates structure optimization.

However, the disadvantages of the simple factory model are also mentioned in the factory class, which integrates the creation logic of all instances and violates the High Cohesion responsibility allocation principle, when the number of product categories of the system increases, there are too many condition judgments in the factory category, which is not conducive to expansion and maintenance. the disadvantages of the simple factory model can be overcome by using the factory method model. (I think it makes sense to extract it from Thank you !)

Sample Code:

Below is a piece of Java code

Package Pattern; interface Car // parent class interface {public void CarName ();} class DaZhong implements Car {public void CarName () {System. out. println ("this is a Volkswagen Car! ") ;}} Class BenChi implements Car {public void CarName () {System. out. println (" this is a Mercedes-Benz! ") ;}} Class BaoMa implements Car {public void CarName () {System. out. println (" this is a BMW Car! ") ;}} Class FactoryMethod {public static Car WhichCar (String s) throws Exception {if (s. equals ("DaZhong") {return new DaZhong ();} else if (s. equals ("BenChi") {return new BenChi ();} else if (s. equals ("BaoMa") {return new BaoMa () ;}else {throw new Exception () ;}} public class Pattern {public static void main (String [] args) {try {Car car = FactoryMethod. whichCar ("BenChi"); car. carName ();} catch (Exception e) {e. printStackTrace ();}}}

Related Article

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: 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.