Eighth session-factory method mode

Source: Internet
Author: User




I can't figure out why you're so cruel. Let's use the factory method model today.


First, what is the factory method mode?
The so-called factory method model is the upgraded version of the Simple factory model.

The factory method pattern and the simple factory model are the same except for the factory class.
Second, with the simple factory model why still use factory method mode?
The simple factory model seems very easy. Because of this. His factory class violates the principle of open closure in design mode. Take the calculator in the first sentence for example. Change the switch branch in the factory class each time you add a new operation.

This violates the extension of the object-oriented design, not the change-it is also the open and closed principle of previous learning. How to solve the problem? We are able to use the dependency reversal principle to be intact.
Three, Code demonstration sample (on the basis of the first words calculator changes)
First change:
Change the previous Operationfactory.java such as the following
Public interface Operationfactory {
Public Operation CreateOperation ();
}
A second change:
Join the new factory class: Addfactory.java
public class Addfactory implements Operationfactory {
Public Operation CreateOperation () {
TODO auto-generated Method Stub
return new Addoperation ();
}
}
Instantiating operationfactory using Addfactory
Operationfactory factory = new Addfactory ();
Operation operation = Factory.creaoperation ();

Note: This image is from "Big Talk design mode"
Iv. Summary
This mode is very easy because of the basics of the previous simple factory model and the closed open principle. Learn together, come on.

Eighth session-factory method 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.