• 周日. 11月 27th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

The design pattern is selected from the book “you are happy when you hear defects”, which can be dow

[db:作者]

1月 6, 2022

formula , It’s just convenient to remember :

The original work order is

Bridge agent composition adapter , Enjoy yuan’s decorative appearance

Visitors write strategy memos , Observe the state of the template iteration , Order the intermediary to explain the chain of responsibility .

Many design patterns ,GOF Just list it 23 Kind of , This paper only gives three examples .

1.1.1 Factory method

Defines an interface for creating objects , Let the subclass decide which class to instantiate . Customize a message box , Initially, you can only set the title and content . later : You can choose whether there are buttons , Is there an icon . Stupid way :4 Two classes correspond to 4 In this case . The simplest solution : Add two member variables to record relevant information . later : Icons can be changed , And the number is variable ; Click a button and the dialog doesn’t need to close . Feasible solution , Two factory method modes and one bridge mode : Two virtual functions are added to the message box class : Create button groups and icon groups . It is implemented by derived classes .

 

1.1.2 Decoration mode

Dynamically add some additional responsibilities to an object . Suppose a company defines a class level interface : Show logo, There’s only one function level interface : Show . Suppose Weibo 、 Blog 、 The forum supports this interface , Several developers have implemented different ways of display logo, such as : Text logo、 picture logo、 Reverse text logo. One developer wants to add copyright , Support already exists 、 There is no indication of logo The way . Define a class “ Display copyright logo” Realization “ Show logo” Interface , Constructors have arguments “ Basic display logo”, Show logo when : First call “ Basic display logo” Display of , Then show the copyright in the lower right corner .

1.1.3 Visitor mode

Represents an operation on each element of an object structure . It allows you to do this without changing the classes of the elements

Define new operations that act on these elements . some CAD, There are image interface classes , This interface has two functions : Find the area and perimeter . A developer wants to add an unusual operation : Seeking the center .CAD It’s impossible to add unusual operations . If CAD Developers use visitor mode , There would be no such problem . Let’s assume that there are limited kinds of figures , The following source code only rectangle and circle ; Let’s assume that the calculation is infinite , This source code only : Find the area 、 Perimeter . It’s not convenient to dictate , Code up :

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注