Design mode - build mode

xiaoxiao2021-03-06  19

2004-05-25

Every time I see the design pattern, I have the same feeling. I didn't understand the last time. This time I understand, but it is still a little bit. People are quite strange, these models are in programming, whether they are in specific projects or their own exercises, have used a lot, but they have not been summarized as a model, but in the form of experience. In the mind, the use of casual use, will forget. Maybe it is an inevitable learning process, free use - forming mode - breakthrough mode flexible application. And the model plays a role in experience, reducing the possibility of forgetting. Therefore, the real master mode, in addition to the performance of understanding the model, it is necessary to understand the problem that the pattern is solved, understand the defects and advantages of the model, in order to be finally flexible and flexible and flexible, this may be me There is no recruiting realm pursuing in the pattern field.

Most of the cores of the pattern are one of two: reuse and decoupling

The words returned to do, or let's talk about specific modes: Design Patterns

Mode Classification: Building mode Mode 1: Abstract factory (Abstract factory) Factory is understood to generate a series of object groups. The Abstract Factory is the concept of Factory, with the aim of extracting a commonality of multiple series of object groups, shielding the difference between the objects between different series.

Series Motif Windows Object Group Button Motifbtn Wbtnbar Motifbar WbartextBox MotiftB WTB

Class AbstractFactory {Button getButton (); bar getBar (); TextBox getTextBox ();

Button, bar, Textbox is a set of objects, and we need to implement different series of object groups such as Motif, Windows, in the way of Abstract Factory, and we are in most applications, the mouth of the operation is AbstractFactory, and Button, Bar These object groups, without the need for a MotiF style or Windows style, and the designation of the style is only determined to implement the specific factory class of AbstractFactory.

This method is convenient for this series of series, but it does not benefit the increase in the object group. For example, to add a Label object, we need to add an abstract object Lable, and you need to modify AbstractFactory and all his subclasses.

So when using this method, the most important thing is the planning and design of the object group.

Mode 2: The concept of the builder generator is to provide a series of methods to generate different parts of the object, and finally return a complete object, focusing on problems with separation of constructors and construction steps, Builder internal packaging is constructing Methods rather than the process. After doing this separation, make Builder an abstraction, so that different build methods and build processes are separated.

Suppose a string storage management: class builder () {add (key, value); addstr (str);}

The same operation set, add (key, value), AddStr (STR) has a variety of results that may output for different Builders: key = value; str

Another possibility:

Value Str

Mode 3: Factory Method (Factory) In fact, his other name is more capable of explaining the problem (fiction) that is to avoid hard coding when the common method of abstract classes, such as: Button object has a getBTN method, You can avoid writing Button btn = new motifbutton (); this code is to encapsulate such code to the child itself. Mode 4: Prototype is not created a new object when the application is created, but a copy from existing objects. This way I used it when you configure the pool, each business program copies an object from my pool, and then performs relevant modifications and applications. This approach is also a way to dynamically create classes, focusing on resolving situations where members of the object group are unstable, not multiple series of issues.

Compared with Factory, he doesn't matter of every prototype. Disadvantages: The difficulty of CLONE itself is implemented.

Mode 5: Singleton This model is countless, no more. Used to ensure the uniqueness of the instance.

转载请注明原文地址:https://www.9cbs.com/read-46381.html

New Post(0)