Personal opinion-simple factory design model

Source: Internet
Author: User
Tags define abstract

Some time ago I read some articles about the design pattern and want to improve my own realm. The result is not very satisfactory (but I still want to spray it here and I will always learn a little ). The general feeling is that these things are very abstract. My brain does not have any crazy design patterns, but only my personal opinions (such as similarities and various reasons ).

In the whole design model, the simplest is the simple factory design model (this is not a design model, which is + _ + |). The real content is easy to understand, however, it fully demonstrates the essence of oo and makes the code more concise and clear.

Simple factory design pattern class diagram:

The simple factory design model is basically the three parts:

1. Simple Factory: You must call the abstract class to instantiate its subclass to implement the function. Different requirements have been fulfilled.

2. Abstract Product: define abstract classes and abstract methods (sub-classes must implement methods in this class ).

3. ConcreteProduct: defines the subclass of the inherited abstract class and overwrites the method of the parent class. (You can understand this)

Actual project requirements in my previous project: in the fast food warehouse project, such as warehouse receiving, return, pick up... And other details of the content, they all need to be queried, while querying the column header is different, and I want to use a form to query the details of all the content, what should I do? One is that every detail uses a dview, and then hides the datagridview. When the dview is used, the others are hidden, and the other is a datagridview dview, however, the header column is dynamically generated.

The former method can be used, but if there are more details, the amount of code will increase, and it looks bloated. It is a headache to maintain and locate bugs (the key is what others think, this writing is not professional ....). The latter method is actually very difficult to write, but I wrote it in a simple factory design mode:

1. Simple Factory: the abstract class is called to instantiate its subclass. The method returns a datatable with the attributes of the datagridview, such as columnName, Name, and width.

2. Abstract Product: an abstract method is written here, and a datatable is returned (these are all nonsense)

3. Specific product (ConcreteProduct): Here I am an array to save a column attribute and then add it to the able.

(No sample code is available if the Code cannot be obtained)

Personal summary:

1. Low code coupling is implemented in actual projects. each subclass method implements its own functions to facilitate debugging.
2. clarify their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture.
3. But it increases the complexity of the Program (at least here I feel that I have been writing it for half a day, but it can improve my understanding of OO ).
4. In violation of the High Cohesion responsibility allocation principle, the relationship between classes is too close. For example, to add a function, modify the factory class and subclass (this is troublesome ).

In general, the simple factory design model is a little difficult to use, but it is a foundation for yourself. After that, the abstract factory, method factory and other design models are expanded on this basis.

 

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.