Bookstore Member Sales System (3)

xiaoxiao2021-03-17  214

Bookstore Member Sales System (3)

-

OO

,

Refactoring and design Pattern

This show:

1.

Study

Abstract Factory

mode.

2.

Study

Factory Method

mode.

3.

Use reconstruction techniques.

Customer: "You are very good, we are very satisfied." Project Manager: "Thank you for your affirmation." Customer: "But the function I want the next version, should I do?" Project Manager: "You Is printing two modes to support screen printing and paper printing? "Customer:" Very Good! I want to do this, you don't remind me, I may have forgotten. "Project Manager:" Okay, next The version we will implement this feature. "After the customer gone, the project manager's face shed:" It's awkward, I praised it, I don't have a good heart, this is not, live again. " Walking to the programmer. Customers are God, this is not doubtful. The most bitter or programmer, this is not doubtful. First analyze, here there are two printing methods: POS screen printing, paper printing, and it is necessary to build an abstract class, and then build two subclasses. For convenience, we only print the cumulative points and amount here.

Add the following code in the main function:

CPRINT

*

Pprint

=

New

Cscreenprint;

Bool

Brel

=

Pprint

->

PRINT (Pmember

->

GetPoint (), fconsumesum; assert (BREL); delete pprint; pprint

=

New

CPAPRINT; BREL

=

Pprint

->

PRINT (Pmember

->

GetPoint (), fconsumesum; assert (BREL); delete pprint;

Class

CPRINT

{public: Virtual ~ cprint (); Virtual Bool Print (int npoint, float fsum) = 0;}

;

The CPAperPrint class code is as follows:

Class

Cscreenprint:

public

CPRINT

{public: cscreenprint (); ~ cscreenprint (); bool print (int npoint, float fsum);}

;

Bool

Cscreenprint :: Print

int

NPOINT,

Float

FSUM)

{Printf ("*** Print On Screen: Point =% D, ConsumeSum =% f *** / n", npoint, fsum); Return True;

Compiled pass, test cases pass. The program is working very well, there is no new demand, let's let it like this, why bother to find trouble? However, there is such a good thing in the world, how long, the customer starts to complain: "I have two branches, I think the print format of each branch is different." It is undeniable that this is indeed a reasonable requirement, then Let's do it, start reconstruction, making new features easier to join. First, the rename is made, the UML map is as follows:

Class

CPAperPrint: PublicPrint: Public

CPRINT

{public: cpaperPrint (); ~ cpaperprint (); BOOL Print (int npoint, float fsum);

;

Bool

CPAperPrint :: Print

int

NPOINT,

Float

FSUM)

{Printf ("$$$ Print On Paper: Point =% D, ConsumeSum =% F $$$ / N", NPOINT, FSUM); Return True;}

Modify the cscreenprintshop1 :: Print function is:

Bool

CscreenPrintshop1 :: Print

int

NPOINT,

Float

FSUM)

{Printf ("*** Shop 1 Print On Screen: Point =% D, ConsumeSum =% f *** / n", npoint, fsum); Return True;

Modify the CPAperPrintshop1 :: Print function is:

Bool

CPAperPrintshop1 :: Print

int

NPOINT,

Float

FSUM)

{Printf ("$$$ SHOP 1 Print On Paper: Point =% D, ConsumeSum =% F $$$ / N", NPOINT, FSUM); Return True;}

I will not be posted, and the Shop1 is almost. Calling CPAPRINTSHOP1, CSCREENPRINTSHOP1 is modified to Shop2, compiled, and test cases are passed. I will ask if I have a friend, this is not a clear Factory Method design mode? Yes, this is the application of Factory Method design mode, but we only have a call, why bother to write the Factory class? After all, use some modes to increase a lot of code. Remember a little: Not necessarily the design pattern is perfect, practical is the last word. But as you said, with the changes in the demand, there are two or more in the program, and we must modify it. The UML map is as follows:

No panic, here we can also analyze, Screen Print and Paper Print use, why don't we merge? The UML map is as follows:

What is this mode? In fact, this is a simplified version of Abstract Factory. Now initialize the print now:

CPrintFactory

*

PprintFactory

=

New

Cshop1printFactory;

Start writing code, using the time of writing code is actually relatively small, most of the time is on design and debugging, and the time of debugging accounts for most, we have to increase programming efficiency, shorten the time of debugging It is the most effective, and the establishment of test use is a very effective way. Written using C language, commissioning at VC 6.0 environment. Code Download References: "Refactoring: Improving The Design of EXISTING CODE" - Martin Fowler "Design Patterns - Elements of Reusable Object-Oriented Software" --gof

CPRINT

*

Pprint

=

PprintFactory

->

CreateScreenPrint (); BOOL

Brel

=

Pprint

->

Print (Pmember

->

GetPoint (), fconsumesum; assert (BREL); delete pprint; pprint

=

PprintFactory

->

CreatePaperPrint (); Brel

=

Pprint

->

Print (Pmember

->

GetPoint (), fconsumesum; assert (BREL); delete pprint;

Http://goodcandle.cnblogs.com/archive/2006/03/14/book3.html

Then write the following code in the call: Of course, the main function will be changed accordingly. Compiled pass, test cases pass. You can now increase Shop 2, the UML map is as follows:

The CScreenprint class code is as follows: CPRINT class code is as follows:

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

New Post(0)