search

Home  >  Q&A  >  body text

java - 【求喷系列】谁能普及下工厂模式的实际应用?我认为根本用不到?

放假无聊在家看设计模式《head first系列》,今天看了工厂模式,分为三种:

  1. 简单工厂模式

  2. 工厂模式

  3. 抽象工厂模式

首先这3个奇葩的名字,我真是醉了,虽说干了很多年,但是真还没怎么用过,这些东西都是各类框架实现的技术,比如spring,jdbc以及log4j之类的,真实的业务场景我还真没咋用过,谁帮忙普及下。
别整网上那些没营养的例子,什么汽车啊,鸭子啊,披萨啊,真是无聊,难道都不是用数据库表存各种属性吗?非要建立出那么多类,可能是想帮助人理解吧,但实际业务场景压根没有啊,谁说出来我服谁?求喷!

我后续还有若干求喷文,希望大家踊跃的喷我~

怪我咯怪我咯2807 days ago835

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 17:37:54

    It’s understandable that you say this is not used in business. Also, don’t think that only those with class name Factory are factories, such as UUID objects. There are currently 4 versions of UUID, none of which are instantiated by new. Factory produces various versions of UUID. The reason for this design is usually to solve the problem of creating a large number of the same interface classes. After grouping and abstraction, it is convenient for unified management. Similar to encoding charset

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:37:54

    It would be a tragedy if you still haven’t encountered one after working for so many years. First of all, you can consider changing jobs to a big company and you will encounter one.
    The simplest factory mode is relational/non-relational database switching, version switching, and class library switching. Wait, the reason you haven't encountered it is because there is no need for post-maintenance or there is no need to consider version iterations and database updates, right?

    Other design patterns also have their own functions. If you haven’t encountered this thing, it won’t feel important. But if you encounter it, you will feel that it is very easy to use. It uses class encapsulation, inheritance, and polymorphism very cleverly.

    Furthermore, the progress of programmers is to study framework source code, language source code, large concurrency, and code maintainability.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:37:54

    Let’s take an example from jdk. The Executors class has a static method of newFixedThreadPool. The interface is defined as follows:

    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {

    As you can see, the second parameter can be passed in a thread factory, and then the Executors class can create the threads needed by the thread pool based on the factory method. In other words, you can write all the rules for creating threads in ThreadFactory. For example, let the thread names created by the factory have the XXX-prefix, or specify the thread priority, etc.

    This can be said to be the most common scenario of the factory pattern, and it is widely used in various frameworks. Learn design patterns and read more actual codes while reading books, and you will naturally discover the specific functions of patterns and even various clever techniques.

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:37:54

    Whether it is the factory pattern or other creation patterns, they all have one purpose - to initialize an object. In other words, in order to build a data structure model (classes and objects themselves are a custom data structure).

    So, the question is, why can we create an object using new and use design pattern? Essentially, the reason is that we don’t want upper-level users to directly use new to initialize objects. new 这样方式可以创建一个对象,还要使用设计模式。本质上就是一个原因,不想让上层使用者直接使用 new 来初始化对象。

    这样的原因有很多,绝大多数原因就是对上层的使用者隔离对象创建的过程;或者是对象创建的过程复杂,使用者不容易掌握;或者是对象创建要满足某种条件,这些条件是业务的需求也好,是系统约束也好,没有必要让上层使用者掌握,增加别人开发的难度。

    所以,到这时我们应该清楚了,无论是工厂模式,还是上面的战友说的开闭原则,都是为了隔离一些复杂的过程,使得这些复杂的过程不向外暴露,如果暴露了这些过程,会对使用者增加麻烦,这也就是所谓的团队合作。

    面向对象封装的本身也就是为了使得对外的 API 尽可能的简化。

    例如,你定义了一个 Status字段,但这个字段因为某些业务原因,需要使用整数来表示状态。那么,如果数字少了还好办,如果数字多了,上层使用者就不一定能记清楚每个数字代表的状态(比如你要做语音通信系统,那么,语音设备是有很多状态数字的)。这时,如果使用 new来创建对象,然后再对 Status

    There are many reasons for this, most of which are that isolating the object creation process from upper-level users; or the object creation process is complicated and difficult for users to master ; Or object creation must meet certain conditions. These conditions may be business needs or system constraints. There is no need for upper-level users to master them and increase the difficulty of development by others.

    So, by now we should be clear, whether it is the factory mode or the opening and closing principle mentioned by the comrades above, it is to isolate some complex processes so that these complex processes are not exposed to the outside world. If these processes are exposed , which will increase trouble for users. This is also called teamwork.

    Object-oriented encapsulation itself is to make the external API as simple as possible.

    For example, you have defined a Status field, but due to some business reasons, this field needs to use an integer to represent the status. Well, if there are fewer numbers, it's easier to handle. If there are more numbers, upper-level users may not be able to clearly remember the status represented by each number (for example, if you want to make a voice communication system, then the voice equipment has many status numbers) . At this time, if you use new to create an object and then assign a value to Status, it is inevitable that you may have to consult the development documentation, or you may accidentally give an incorrect error message. value. At this time, you may wish to use the factory pattern or other suitable design patterns to construct the code.

    For example, this: 🎜
    public static class Factory
    {
        public static Ixxxxxx CreateWithOpen()
        {
            var obj = new Obj();
            obj.Status = 1;
            return obj;
        }
        public static Ixxxxxx CreateWithClose()
        {
            var obj = new Obj();
            obj.Status = 2;
            return obj;
        }
    }
    
    🎜Of course, you can also use enumerations. To put it bluntly, it depends on the designer’s wishes. 🎜 🎜So, the design pattern does not say in which scenario it must be used. To be more precise, it should be, when you use the design pattern, can it bring convenience to your team members, or improve the code quality and avoid some errors. If so, use it. If it only brings complexity and no benefits, then forget it. 🎜 🎜In a word, there is no need to use it or not, and there is no need to use it. Using it will bring benefits, whether it is to the team, product quality or product maintainability. Use it or not, it must be teamwork and product-oriented. This is the basic requirement for a software designer. 🎜

    reply
    0
  • Cancelreply