Design Patterns---Simple Factory Pattern

     I recently read the book Dahua Design Patterns, and I fell in love with it as soon as I read it. So I plan to follow the code.

     Is it necessary to learn all design patterns?

     The answer is, Yes! Don't be swayed by those who say that design patterns are mostly unusable and don't need the whole school at all. Although there are far more than 23 design patterns, it is not easy to study all of them, but like the author himself, in the process of learning the 23 design patterns summarized by GOF, you will be carried out by those programming masters. Baptism of technical ideas, and constantly increase their in-depth understanding of object-oriented, so as to better carry forward this idea.

     The important thing is not whether you will use these patterns in the future, but through these patterns, you can find the feeling of "encapsulating changes", "loose coupling between objects", and "programming against interfaces", so as to design easy-to-maintain, easy-to-extend, and easy-to-use patterns. Reusable and flexible programs. After becoming poets, they may not need to deliberately create according to a certain pattern, but before becoming poets, they must have seriously studied hundreds of Tang poems and Song lyrics, ancient and modern famous sentences.

   If mathematics is the gymnastics of thinking, then design patterns are the gymnastics of object-oriented thinking.

                                                                                                                                    ---- "Dahua Design Patterns"




    If you give a question such as: There are several people now, and you find different people who have different answers, please implement it with code.


    If you think about the code in a few minutes, you will have a general idea, and then turn the idea into code:


Then run the test and you can use it. You can easily hand in tasks.

    But if the topic changes at this time, please implement it in object-oriented form. When learning object-oriented, remember 6 words encapsulation inheritance polymorphism .

         Encapsulation: refers to a method of wrapping and hiding the implementation details of an abstract functional interface .

        Inheritance: The subclass inherits the characteristic behavior methods of the parent class, so that the subclass object (instance) has the instance domain method behavior of the parent class object.

       Polymorphism: The ability of the same behavior to have different manifestations or forms, and the parent class reference points to the child class object.

    At this time, if I think according to this idea, then I judge that the method of reply needs to be encapsulated separately from the user input (encapsulation), and then look at the conditions in which they are just different people who have different answers, then can we extract a common The parent class (inheritance), and then according to different instructions to do different answers (polymorphism).

    Extract a parent class:

    Subclass inherits parent class and overrides:


     Then we extract the judgment method:

   Test class:


    In this case, a simple factory pattern is written.

    Click to see the benefits of the factory pattern

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325964308&siteId=291194637