Simple Design Mode

Source: Internet
Author: User

I recently started to learn the big talk Design Model of instructor Cheng Jie. First, I want to study basic object-oriented knowledge, such as classes, encapsulation, inheritance, polymorphism, interfaces, and events; then I began to learn the first design model-simple factory model. Starting learning is caused by the failure of the food interview. The entire chapter focuses on code standardization, including maintenance, reusability, scalability, and flexibility. I am wondering how the design model is talking about this? The simple factory model cannot be described as a design model. It may be more appropriate to say that it is a programming habit. Because it is at least not one of the Gof23 design patterns. But it is often used in actual programming, and its idea is also very simple. It can be said that it is a guide of the factory method model. So what is a simple factory model? My understanding is: Use a separate factory class to create instances of other classes. The simple factory model consists of three main components: 1. The core of the simple factory model (the factory class in concept), which is responsible for implementing the internal logic of creating all instances. The factory class can be directly called by the outside world to create the required product objects. 2. Abstract The parent class of all objects created by the Product role (other classes in the concept) in simple factory mode, which describes the common public interfaces of all instances. 3. The Concrete Product role (an instance in concept) is the creation target of the simple factory model. All created objects are instances of a specific class that acts as the role. Sample Code: [csharp] public class OperationFactory // simple Operation factory class {public Operation CreateOperate (string operate) // return the CreateOperate method statement of the Operation type {Operation operator = null; switch (operate) {case "+": condition = new OperateAdd (); break; case "-": condition = new OperateSub (); break; case "*": condition = new OperateMul (); break; case "/": condition = new OperateDiv (); break;} return condition;} the client code is: [csharp] Operation Condition; Activities = OperationFactory. CreateOperate ("+"); Activities. NumberA = 1; activities. NumberB = 2; double result = outcome. GetResult (); so far, the Code has ended. Through the above code, we can find that the simple factory model is also advantageous and has some disadvantages. Benefit: 1. by using the factory class, the outside world can get rid of the embarrassing situation of directly creating specific product objects, and clarify their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture; 2. because the factory class integrates the creation logic of all instances, it violates the High Cohesion responsibility allocation principle; 3. all the creation logic is centralized into a factory class. The classes that can be created can only be considered in advance. To add a new class, you need to change the factory class. Disadvantages: 1. when the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type. It is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion. Recommended usage: 1. the factory class is responsible for creating fewer objects; 2. the customer only knows the parameters passed into the factory class and does not care about how to create the object (logic); 3. because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases. Although the simple factory model is not one of the 23 design patterns, it is still enlightening for the code specifications of programmers. We can't just finish writing the correct code execution results. programming is a technology and an art, writing code that is maintainable, reusable, scalable, and flexible, it should be our constant pursuit.

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.