Some thoughts on the factory method pattern (Java syntax representation)

Source: Internet
Author: User

The same 创造型 design pattern 简单工厂模式 can be understood as the substitution of the new keyword.

With the principle of repeating three times and refactoring, if an object is new more than two times in a different place, consider using it. So why do we use the simple factory model instead of new? Just as we use Getter Setters instead of direct read and write, it's plain to add an indirection layer to buffer the process changes (for example, when you get a field, add 100 to it, and write it in a getter without the need for a shotgun modification).

The factory method pattern is undoubtedly a lot more complicated than that. Cite a Wikipedia explanation of its use:

Deal with the problem of creating objects without have to specify the exact class of the object that would be created

In other words 不想直接引用精确类型来创建对象 , when used. And so on, not referencing the exact type. That's not polymorphic, so we'll use it directly.

AInterface a;//TODO: 对a依赖注入a.work();AInterface b;//TODO: 对b进行不同精确类型的依赖注入b.work();

Isn't that the way it is?

If this can solve the problem, of course, it can be used, after all, refactoring is only necessary to do. However, the work of a dependency injection is definitely not done automatically, and a large number of dependency injection frameworks have emerged to address this problem. If you don't want to use a bulky frame, then you have to create an instance of a, and then you have to know the exact type of a, after all, you can't just make a new interface, at least in Java. So you have this code:

newAInterfaceImpl();a.worknewBInterfaceImpl();b.work();

In fact, the disgusting work will not disappear, at most can be the nausea of the work delayed completion, or our program cannot run. In the above requirements, you can not always ignore the exact type of a, even with the dependency injection framework is the same, somewhere in the program you have to give it a definite type. But the virtue of programmers is that complex work is always deferred to the back, at least in the code above, I want to hide the exact type, like this:

Factory factory;//TODO: 对factory依赖注入//看,用起来很简单对吧AInterface a;a = factory.getIt();a.work();//TODO: 对factory进行不同的依赖注入//用多态的特性做不同的工作AInterface b;b = factory.getIt();b.work();

Well, we solved one problem and then introduced another. Now we have to care about the exact type of factory. We kept throwing rubbish at others to keep ourselves clean.

I'm tired of passing on rubbish, I don't want to write it, but the factory approach mode is not over yet.

Some thoughts on the factory method pattern (Java syntax representation)

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