Simple Factory/Factory method/abstract factory of design pattern

Source: Internet
Author: User
Tags acer

These three design patterns belong to the creation mode, which is related to each other, and is explained together. In fact, the simple factory model is a special case of the factory method, not one of the 23 design patterns.

These design patterns are written using java. The Java file directory tree looks like this:

[Email protected]:~/code/designpattern/simplefactory/src$ tree.├──client.java└──zy    ├──abstractfactory    │ ├──accerfactory.java    │├──applefactory.java    │└──computerabstractfactoy.java    ├──factory    │├──co Mputerfactory.java    │├──desktopcomputerfactory.java    │└──laptopfactory.java    ├──product    │├──acce Rdesktopcomputer.java    │├──accerlaptop.java    │├──appledesktopcomputer.java    │├──applelaptop.java
   │├──computer.java    │├──desktopcomputer.java    │└──laptop.java    └──simplefactory        └──compu Tersimplefactory.java5 directories, files

Describe the motivations and the corresponding pros and cons by buying a computer scene from a department office.

A department will buy a certain number of computers to let students work, compared to the earlier purchase of the desktop,

1. Look at the common creation method below.

Computer.javapackage Zy.product;public interface Computer {void uname ();}

Laptop.javapackage Zy.product;public class Laptop implements computer {public void uname () {System.out.println ("I am a notebook , more portable ");}}


Desktopcomputer.javapackage Zy.product;public class Desktopcomputer implements computer {public void uname () { System.out.println ("I am a desktop, the screen is larger");}}

General call test method public static void Normaltest () {Computer labComputer1 = new Laptop (); Labcomputer1.uname (); Computer LabComputer2 = new Laptop (); Labcomputer2.uname (); Computer LabComputer3 = new Laptop (); Labcomputer3.uname (); Computer labComputer4 = new Laptop (); Labcomputer4.uname (); Computer LabComputer5 = new Laptop (); Labcomputer5.uname ();}
Here, the Department needs 5 notebooks, but if the department needs 5 desktops in two years, you will need to change each statement that created the object. The readability and maintainability of this is not good.


2. Simple Factory mode

The idea is to use a simple factory to create a computer, so that the client directly call the factory to get the computer, so that they do not have to control how the computer is produced. Look at the following code:

Computersimplefactory.javapackage Zy.simplefactory;import Zy.product.computer;import Zy.product.DesktopComputer Import Zy.product.laptop;public class Computersimplefactory {public static computer Createcomputer (String ComputerName) {if ("Laptop". Equals (ComputerName)) {return new Laptop (); else if ("Desktopcomputer". Equals (ComputerName)) {return new Desktopcomputer ();} Elsereturn new Laptop ();}}

Test method for simple factory public static void Simplefactorytest () {//Lab to 5 notebooks/*computer LabComputer1 = Computerfactory.createcomputer ("Labtop"); Labcomputer1.uname (); Computer LabComputer2 = Computerfactory.createcomputer ("Labtop"); Labcomputer2.uname (); Computer LabComputer3 = Computerfactory.createcomputer ("Labtop"); Labcomputer3.uname (); Computer labComputer4 = Computerfactory.createcomputer ("Labtop"); Labcomputer4.uname (); Computer LabComputer5 = Computerfactory.createcomputer ("Labtop"); Labcomputer5.uname (); *///Lab change requirements, 5 desktops Computer labComputer1 = Computersimplefactory.createcomputer (" Desktopcomputer "); Labcomputer1.uname (); Computer LabComputer2 = Computersimplefactory.createcomputer ("Desktopcomputer"); Labcomputer2.uname (); Computer LabComputer3 = Computersimplefactory.createcomputer ("Desktopcomputer"); Labcomputer3.uname (); Computer labComputer4 = Computersimplefactory.createcomputer ("Desktopcomputer"); Labcomputer4.uname (); Computer LabComputer5 = Computersimplefactory.createcomputer ("DesktopcomputEr "); Labcomputer5.uname ();}
As you can see, it is possible to directly invoke the static method of the factory class to produce a notebook. A unified interface is more readable. and easy to maintain.


3. Factory Method Mode

One drawback of the simple factory model above is the addition of a type of computer, such as hyper-polar. It is necessary to modify the static production method of the factory, violate the open-closed principle, and open the modification.

For this disadvantage, it is convenient to create objects by using the polymorphism of virtual function between parent class and subclass, dynamic binding. The code is as follows:

Computerfactory.javapackage Zy.factory;import Zy.product.computer;public interface Computerfactory {public Computer Createcomputer ();}

Laptopfactory.javapackage Zy.factory;import Zy.product.computer;import Zy.product.laptop;public class Laptopfactory implements Computerfactory{public computer Createcomputer () {return new Laptop ();}

Desktopcomputerfactory.javapackage Zy.factory;import Zy.product.computer;import Zy.product.DesktopComputer; public class Desktopcomputerfactory implements Computerfactory{public computer Createcomputer () {return new Desktopcomputer ();}}

Test method for factory methods public static void Factorytest () {//original demand Computerfactory factory = new Laptopfactory ();//demand change:// Computerfactory factory = new Desktopcomputerfactory (); Computer LabComputer1 = Factory.createcomputer (); Labcomputer1.uname (); Computer LabComputer2 = Factory.createcomputer (); Labcomputer2.uname (); Computer LabComputer3 = Factory.createcomputer (); Labcomputer3.uname (); Computer labComputer4 = Factory.createcomputer (); Labcomputer4.uname (); Computer LabComputer5 = Factory.createcomputer (); Labcomputer5.uname ();}

As you can see, if you add a super-high-class, you don't have to modify the existing code to add a super-large factory directly. And the client's requirements code, but also only to replace a factory. Better scalability than a simple factory. The factory method pattern, also called the fictional model, is the substitution of the constructor function by this method.


4. Abstract Factory mode

The department was originally using Acer's computer, but now it has become a local tyrants, to be replaced by an Apple computer. If you use the factory method, you need to build a factory twice times more, the code is much larger. At this point, an abstract factory model can be used to place several related plant methods in a factory, such as the production of computers (including laptops/desktops) in the Apple plant and the Acer plant. This reduces the amount of code. Abstract some specific related products into a factory.

Applelaptop.javapackage Zy.product;public class Applelaptop extends Laptop{public void uname () {System.out.println (" I am an apple notebook, more portable ");}}

Appledesktopcomputer.javapackage Zy.product;public class Appledesktopcomputer extends Desktopcomputer{public void Uname () {System.out.println ("I am an apple desktop, the screen is larger");}}

Accerlaptop.javapackage Zy.product;public class Accerlaptop extends Laptop {public void uname () {System.out.println (" I am a Acer notebook, more convenient to carry ");}}

Accerdesktopcomputer.javapackage Zy.product;public class Accerdesktopcomputer extends Desktopcomputer {public void Uname () {System.out.println ("I am a Acer desktop, the screen is larger");}}


Computerabstractfactoy.javapackage Zy.abstractfactory;import Zy.product.desktopcomputer;import Zy.product.laptop;public interface Computerabstractfactoy {public Laptop createlaptop ();p ublic Desktopcomputer Createdesktopcomputer ();}

Applefactory.javapackage Zy.abstractfactory;import Zy.product.appledesktopcomputer;import Zy.product.applelaptop;import Zy.product.desktopcomputer;import Zy.product.laptop;public class AppleFactory Implements Computerabstractfactoy{public Laptop Createlaptop () {return new applelaptop (); Public Desktopcomputer Createdesktopcomputer () {return new Appledesktopcomputer ();}}

Accerfactory.javapackage Zy.abstractfactory;import Zy.product.accerdesktopcomputer;import Zy.product.accerlaptop;import Zy.product.desktopcomputer;import Zy.product.laptop;public class AccerFactory Implements Computerabstractfactoy{public Laptop Createlaptop () {return new accerlaptop (); Public Desktopcomputer Createdesktopcomputer () {return new Accerdesktopcomputer ();}}


test method of the abstract factory public static void Abstratfactorytest () {//original demand, Apple's notebook Computerabstractfactoy factory = new Applefactory (); /current demand, Acer's notebook//computerabstractfactoy factory = new Accerfactory (); Computer LabComputer1 = Factory.createlaptop (); Labcomputer1.uname (); Computer LabComputer2 = Factory.createlaptop (); Labcomputer2.uname (); Computer LabComputer3 = Factory.createlaptop (); Labcomputer3.uname (); Computer labComputer4 = Factory.createlaptop (); Labcomputer4.uname (); Computer LabComputer5 = Factory.createlaptop (); Labcomputer5.uname ();}
You can see that the abstract factory has a smaller scale than the factory method. When you need to replace some of the listed products, you only need to replace a factory class. However, if a new product comes in, you need to modify the design of the abstract factory class and modify the existing factory class code.



Note:

Code package in: Click to open the link


Simple Factory/Factory method/abstract factory of design pattern

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.