In this way, what should we do if we require air transportation or vehicle transportation next time? ContinueCarryoilAdd new methods to adapt to changes in new requirements. This violates the "open and closed principle" (open to extensions and closed to modifications). We will observe these methods carefully, whether using trains, cars, airplanes, or pipelines to transport oil, you can abstract a method: transportation of oil. Therefore, our design is as follows:
At this point, if we add a new transportation method, we only need to inherit the carryoil class and implement the carry method. at this time, we do not need to worry about how the other two transport modes are implemented. in line with the single Responsibility Principle in the object-oriented design, but also well applied the "open and closed principle ". the code for client calling is as follows:
Protected Void Page_load ( Object Sender, eventargs e) {carryoil C = New Carryoilwithtrain (); display (c );} Void Display (carryoil c) {response. Write ( " Method: " + C. Carry () + " <HR/> " );}
It seems that there is no problem with the design. If the pipeline is used for transportation, you can simply change it to carryoilwithpiping. but what if the above Code is called multiple times on the page and all of them need to be changed to carryoilwithpiping?
For example:
Protected VoidPage_load (ObjectSender, eventargs e) {carryoil C1 =NewCarryoilwithtrain (); display (C1); carryoil C2 =NewCarryoilwithtrain (); display (C2); carryoil C3 =NewCarryoilwithtrain (); display (C3 );}VoidDisplay (carryoil c) {response. Write ("Method:"+ C. Carry () +"<HR/>");}
In this case, if you want to use a pipeline for transportation, the three carryoilwithtrain will be modified. So the factory method mode appears:
Code:
Public InterfaceItransport {carryoil gettraffic ();}Public ClassTransportwithtrain: itransport {PublicCarryoil gettraffic (){Return NewCarryoilwithtrain ();}}Public ClassTransportwithpiping: itransport {PublicCarryoil gettraffic (){Return NewCarryoilwithpiping ();}}
In this case, the page is called as follows:
Protected VoidPage_load (ObjectSender, eventargs e) {itransport factory =NewTransportwithtrain (); carryoil C1 = factory. gettraffic (); display (C1); carryoil C2 = factory. gettraffic (); display (C2); carryoil C3 = factory. gettraffic (); display (C3 );}VoidDisplay (carryoil c) {response. Write ("Method:"+ C. Carry () +"<HR/>");}
In this case, you only need to modify transportwithtrain to transportwithpiping to make one modification, instead of modifying it as shown in the preceding three steps.
In addition, no specific class is used during page calling (the specific product class here ). the transportwithtrain class is called in multiple pages. What should I do if multiple pages call this class?
Now you can set itransport factory =NewTransportwithtrain (); put it in a class, as follows:
Public ClassCreatefactory {PublicCreatefactory (){//// Todo: add the constructor logic here//}PublicItransport createtransportfactory () {itransport TRAN =NewTransportwithtrain ();ReturnTran ;}}
Page call time:
Protected VoidPage_load (ObjectSender, eventargs e) {createfactory cf =NewCreatefactory (); itransport factory = Cf. createtransportfactory (); carryoil c = factory. gettraffic (); display (c );}VoidDisplay (carryoil c) {response. Write ("Method:"+ C. Carry () +"<HR/>");}
You can call such a statement on multiple pages. If you need to modify it, you only need to replace transportwithtrain in createtransportfactory in the createfactory class with transportwithpiping.