"Open Treatment": A module should be easy to expand, free from modification
Question: Consider the example of the previous chapter, if you add a new specific fruit "Watermelon" what work needs to do.
This chapter completes the following: 1. Code is compiled and passed by the Delphi 2005 that supports Chinese, and removes some insignificant places, such as abnormal processing, etc. Method mode "two methods are realized, please experience the differences; Understand what interface, polymorphism, static functions, etc., this is not explained, please see the first chapter or find relevant information; 5, this chapter is similar to the last chapter, just put the factory from "orchard" to "fruit "The same three kinds of fruits: apples, grapes, strawberries; each fruit has encapsulated two logic, and the two logic will be used when" transaction "outside. Finally, please re-review the "opening and closing principle"
Let's start below. . . . . . .
Here is the implementation method of simple factory model, in this model: 1, a hawker is responsible for the transaction of all three fruits, this is a big challenge for him, don't believe you! 2. Customers must have an accurate description of the name of the fruit, so that fruit will be sold to you, this is impact business!
// Simple factory class and fruit class unit file unit simpleFactory; InterfaceType interface _ fruit = interface (ie upterface) function tips (): string; function is evaluated (): string; end; class _ Apple = Class (TinterFaceDObject, Interface _ fruit Function Tips (): String; Function is evaluated (): string; end; class _ grape = Class (TinterFaceDObject, interface _ fruit) Function Tips (): string; function is evaluated (): string; end; class _ strawberry = Class (TinterFaceDObject, Interface _ Fruit) Function Tips (): String; Function is evaluated (): string; end; factory class _ haler = class (TOBJECT) PUBLIC CLASS FUNCTION factory (fruit name: string): Interface _ fruit; End; importation {***** class _ Apple *****} Function class _ Apple. Tip (): string; begin result: = 'Put the skin again! '; End; Function class_Apple. Rated (): string; begin result: =' grace, not bad, very sweet! '; end; {***** class _ grape *****} Function class _ grapes. Tip (): string; begin result: =' Don't put the nucleus! '; End; Function class_ grapes. Rated (): string; begin result: =' did not check? ? ? '; End; {***** class _ strawberry *****} Function class _ strawberry. Tip (): string; begin result: =' Don't blame, I didn't tell you, very acid! '; End; Function class_ strawberry. Evaluation (): string; begin result: =' Try? Wow, my teeth are getting sour! '; End; {***** factory class _ hawker *****} Class Function factory class _ hawker. Factory (fruit name: string): interface _ fruit; begin if (fruit name =' apple ') THEN Result: = Class_Apple .create () Else IF (Fruit Name = 'Grape') Then Result: = Class_ Grape .Create () else IF (Fruit Name = 'Strawberry') Then Result: = Class _ Strawberry .CREATE (); END;
// form unit files unit MainForm; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SimpleFactory; type TForm1 = class (TForm) RadioButton1: TRadioButton; RadioButton2: TRadioButton; RadioButton3: TRadioButton ; procedure RadioButton3Click (Sender: TObject); procedure RadioButton2Click (Sender: TObject); procedure RadioButton1Click (Sender: TObject); private procedure transaction (fruit name: string); end; var Form1: TForm1; implementation {$ R * .dfm} Procedure TFORM1. Transaction (Fruit Name: String); Var I bought the fruit: interface _ fruit; Begin I bought the fruit: = factory class _ hawker. Factory (fruit name); ShowMessage (I bought fruit. Tip); showMessage (I bought the fruit. I was evaluated); End; Procedure TFORM1.Radiobutton1Click (Sender: TObject); Begin Trading ('Apple'); End; ProCedure TFORM1.Radiobutton2Click (Sender: TOBJECT); Begin Trading ('grape'); End; Procedure TFORM1.Radiobutton3Click (Sender: TOBJECT); Begin Trading ('Strawberry'); End; End. Here is a method of implementation of factory method mode, in this mode 1, each fruit corresponds to a hawker In this way, they will make more business, huh, huh! 2. Customers are directly translated by the hawkers. He knows what you want, so you won't eat fruit because you can't say the nasty fruit name.
// Factory class and fruit class file UNIT FACTOREMETHOD; InterfaceType interface _ fruit = interface (ie internalface) function tips (): string; function (): string; end; class _ Apple = Class (TinterFaceDObject, Interface _ fruit) Function Tips (): String; Function is evaluated (): string; end; class _ grape = Class (TinterFaceDObject, interface _ fruit) Function Tips (): string; function is evaluated (): string; end; class _ strawberry = Class (TinterFaceDObject, Interface _ Fruit) Function Tips (): string; function is evaluated (): string; end; interface _ hawker = interface (ieterface) Function factory (): interface _ fruit; end; class _ Apple hawker = Class (TinterFaceDObject, interface _ hawker) Function factory (): interface _ fruit; end; class _ grape hawker = Class (TinterfacedObject, interface _ hawker) Function factory (): interface _ fruit; end; class _ strawberry hawker = class (TinterfacedObject , Interface _ hawker) Function factory (): interface _ fruit; end; importation {****** class _ Apple ******} Function class _ Apple. Tip (): string; begin result: = ' Put the skin and eat! '; End; Function class_Apple. Rated (): string; begin result: =' grace, not bad, very sweet! '; End; {****** class _ grape ******} Function class _ grapes. Tip (): string; begin result: =' Don't put the nucleus! '; End; Function class_ grapes. Rated (): string; begin result: =' did not check? ? ? '; End; {****** class _ strawberry ******} Function class _ strawberry. Tip (): string; begin result: =' Don't blame, I didn't tell you, very acid! '; End; Function class_ strawberry. Evaluation (): string; begin result: =' Try? Wow, my teeth are getting sour! '; End; {***** class _ Apple hawker *****} Function class _ Apple hawker. Factory (): interface _ fruit; Begin Result: = class _ Apple.create () end; {** *** class _ grape hawker *****} Function class _ grape hawker. Factory (): interface _ fruit; Begin Result: = class _ grape .create () end; {***** class _ strawberry hawker *****} Function class _ strawberry hawker. Factory (): interface _ fruit; begin result: = class _ strawberry .create () end; end;
// form unit files unit MainForm; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, FactoryMethod; type TForm1 = class (TForm) RadioButton1: TRadioButton; RadioButton2: TRadioButton; RadioButton3: TRadioButton ; procedure RadioButton3Click (Sender: TObject); procedure RadioButton2Click (Sender: TObject); procedure RadioButton1Click (Sender: TObject); private procedure transaction (hawker: _ hawker Interface); end; var Form1: TForm1; implementation {$ R * .dfm } Procedure TFORM1. Transaction (Handlers: Interface _ hawker); Var I bought the fruit: interface _ fruit; Begin I bought the fruit: = hawker. Factory (); showMessage (I bought fruit. Tip); showMessage Fruit. Evaluation); End; Procedure TFORM1.Radiobutton1Click (Sender: Tobject); Begin Trading (class _ Apple hawker .create); end; procedure tform1.radiobutton2click (sender: TOBJECT); Begin trading (class _ grape hawker. Create); end; procedure tform1.radiobutton3click (sender: Tobject); Begin trading (class _ strawberry hawker .create); end; end. Summer is coming, watermelon is listed; in simple factory model, because only one hawker, in order to He has introduced watermelon to modify its factory; in the factory method model, because each hawker is responsible for a fruit, only need to introduce a hawker selling watermelon, and the sales will not affect other hawkers.
Let's take a look at how to do in simple factory model: 1. In the factory class and fruit class file, introduce a new watermelon class (here is expanding, will not affect existing code)
/ / =========================================================================================================================================================================================== ============================= class _ watermelon = Class (TinterFaceDObject, interface _ fruit) Function Tips (): String; function Evaluation (): String; end; {****** class _ watermelon ******} Function class _ watermelon. Tip (): string; begin result: = 'just listed Sanda Watermelon, 2 Yuan money a pound! '; End; Function class _ watermelon. Evaluation (): string; begin result: =' relying on, being cheated, not familiar! '; end; / / ============================================= ================================= 2, in the factory class and fruit class unit file, modify the vendor factory method (Here is a modification, it has violated "opening and closing principles")
Let's take a look at how to do in the factory method mode: 1, this step and in the simple factory model, in the factory class and fruit unit file, introduce a new watermelon class (here is expanding, not Will affect existing code)
/ / =========================================================================================================================================================================================== ============================= class _ watermelon = Class (TinterFaceDObject, interface _ fruit) Function Tips (): String; function Evaluation (): String; end; {****** class _ watermelon ******} Function class _ watermelon. Tip (): string; begin result: = 'just listed Sanda Watermelon, 2 Yuan money a pound! '; End; Function class _ watermelon. Evaluation (): string; begin result: =' relying on, being cheated, not familiar! '; end; / / ============================================= ================================== 2, the difference is here, in the factory class and fruit class unit file Introducing a new watermelon trafficking (here is expanding, will not affect existing code)
/ / =========================================================================================================================================================================================== ============================= 类 _ 水瓜 小 = Class (TinterfaceDObject, Interface _ hawker) Function factory (): interface _ fruit ; {***** class _ watermelon hawker *****} Function class _ watermelon hawker. Factory (): interface _ fruit; Begin Result: = class _ watermelon.create () end; // == ============================================================================================================================================================================================================= ========================= 3, in the form unit file, add a new event processing process (here is extension, will not affect Existing code)
/ / =========================================================================================================================================================================================== ============================= Radiobutton4: tradiobutton; Procedure Radiobutton4Click (Sender: TOBJECT); Proceder TFORM1.Radiobutton4Click (Sender: Tobject) Begin trading (class _ watermelon hawker .create; end; // ==================================== ===================================================
/ / =========================================================================================================================================================================================== ================================================ (Fruit Name = 'Apple') Then Result: = Class_Apple.create () Else IF (Fruit Name = 'Grape') Then Result: = Class_ Grape .create () Else IF (Fruit Name = 'Strawberry') Then Result: = class _ strawberry .create () // Please note that the following statement is new, the factory is modified! ! ! ! Else IF (Fruit Name = 'Watermelon') Then Result: = Class _ Watermelon .create (); End; / / ======================== ============================================================================================================================================================================================================= === 3, in the form unit file, add a new event processing process (here is extended, will not affect the existing code)