C # Design pattern (1)----Simple Factory mode

Source: Internet
Author: User

1. Preface

A summary of the design pattern in succession one months, but also gradually some of the code in the project to some optimization. In fact, this summary of the processing so that they have a new understanding of the design pattern, the C # object-oriented, reflective, UML, design principles have a new understanding. So I hope that the small partners can take time to do a comprehensive study of design patterns, I think as long as the seriously learned people will have a certain improvement. Let's take a look at the simple factory model.

2. What is simple Factory mode?

The Simple Factory mode "Simplefactory" is not one of the 23 design modes. Calls a class based on the data supplied to it to obtain one of the derived classes in multiple derived classes. Here is the process for me to implement a simple factory.

Calling the implementation class

         ///<summary>/// get Wendy ///</summary> ///<param name= "Gatapower" > </param> ///<returns></returns> Span style= "color: #0000ff;" >publicvar unit = unit_factory.create_unit_factory ( " Building "); Unit.fee_power_query = Fee_power_query; return Unit.get_info ();}      

Simple Factory class

//Unit Information Factory modePublicClassunit_factory {PublicUnit Unit {Get{ThrowNewSystem.NotImplementedException (); }Set{ } }PublicStatic Unit Create_unit_factory (Stringunitname) {Unit unit =Null;Switch(Unitname) {Case"The": {NewUnti_rooms ();Break; }Case"Floor": {New Unti_floor (); breakcase building ": {new unti_building (); breakcase area ": {new Unti_areas (); breakreturn Unit;}}        

Base class

Class Unit:wx_sbs_redis    {public         fee_power_query fee_power_query;        virtualnullreturn ret;}}      

Derived classes

//Floor InformationClassUnti_floor:unit {PublicOverrideResult Get_info () {String sign = Fee_power_query.orgid +"|"+ Fee_power_query.uid +"|"+ fee_power_query.area_id +"|"+ fee_power_query.building_id +"|"+ Fee_power_query.cardno +"|" +Fee_power_query.code;if (!Fee_power_query.valid_floors ())Return Result ((Int) Errcode. Interval, fee_power_query.msg); list<cs_power> list =New list<cs_power>();var recents =New Fee_recents (). Pick ("Fee"+ Fee_power_query.uid,"Power_floor");var ret =New Wxwebapi<i_gate_fee_power> (). Invoke (I-=I.floors, Fee_power_query);ReturnRet } }//Wendy InformationClassUnti_building:unit {PublicOverrideResult Get_info () {if (!Fee_power_query.valid_buildings ())Return Result ((Int) Errcode. Interval, fee_power_query.msg);var ret =New Wxwebapi<i_gate_fee_power> (). Invoke (I-=I.buildings, Fee_power_query);return ret;}} // campus information class Unti_areas:unit {public override result Get_info () {if (! Fee_power_query.valid ()) return Result (int) Errcode. Interval, fee_power_query.msg); var ret = new wxwebapi<i_gate_fee_power> (). Invoke (I => I.areas, fee_power_query); return ret;}}       

C # Design pattern (1)----Simple Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.