In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of ". Net simple factory model explanation". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The simple factory pattern uses object-oriented method to reduce the coupling degree of the program through inheritance, encapsulation and polymorphism, and the design pattern makes the program more flexible, easy to modify and easy to reuse.
Here is the server calculator code:
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Namespace DesignModel
{
/ / /
/ / calculator
/ / /
Public class Calculator / / the base class for creating a calculator can accept two parameters, and any algorithm only needs to override the calculation result method.
{
Private double _ numberA
Private double _ numberB
Public double NumberA
{
Get {return this._numberA;}
Set {this._numberA = value;}
}
Public double NumberB
{
Get {return this._numberB;}
Set {this._numberB = value;}
}
Public virtual double GetResult ()
{
Double result = 0
Return result
}
}
/ / /
/ / addition
/ / /
Public class Add: Calculator / / for each calculation method added, you only need to add a calculation class and override the base class method
{
Public override double GetResult ()
{
Return NumberA + NumberB
}
}
/ / /
/ / subtraction
/ / /
Public class Sub: Calculator
{
Public override double GetResult ()
{
Return NumberA + NumberB
}
}
/ / /
/ / Calculator Factory
/ / /
Public class CalculatorFactory
{
Public static Calculator GetResult (string oper)
{
Calculator calcu = null
Switch (oper)
{
Case "+":
Calcu = new Add ()
Break
Case "-":
Calcu = new Sub ()
Break
}
Return calcu
}
}
}
The copy code is as follows:
Static void Main (string [] args)
{
Console.WriteLine ("Please enter the number A:")
String numbera = Console.ReadLine ()
Console.WriteLine ("Please enter operator:")
String oper = Console.ReadLine ()
Console.WriteLine ("Please enter the number B:")
String numberb = Console.ReadLine ()
Calculator c = CalculatorFactory.GetResult (oper)
C.NumberA = Convert.ToDouble (numbera)
C.NumberB = Convert.ToDouble (numberb)
Console.WriteLine (string.Format ("{0} {1} {2} = {3}", numbera, oper, numberb, c.GetResult ())
Console.ReadLine ()
}
If the basic verification is not added, the students who study and practice can add it themselves.
Follow-up updates of 28 design patterns
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Namespace DesignModel
{
/ / /
/ / calculator
/ / /
Public class Calculator / / the base class for creating a calculator can accept two parameters, and any algorithm only needs to override the calculation result method.
{
Private double _ numberA
Private double _ numberB
Public double NumberA
{
Get {return this._numberA;}
Set {this._numberA = value;}
}
Public double NumberB
{
Get {return this._numberB;}
Set {this._numberB = value;}
}
Public virtual double GetResult ()
{
Double result = 0
Return result
}
}
/ / /
/ / addition
/ / /
Public class Add: Calculator / / for each calculation method added, you only need to add a calculation class and override the base class method
{
Public override double GetResult ()
{
Return NumberA + NumberB
}
}
/ / /
/ / subtraction
/ / /
Public class Sub: Calculator
{
Public override double GetResult ()
{
Return NumberA + NumberB
}
}
/ / /
/ / Calculator Factory
/ / /
Public class CalculatorFactory
{
Public static Calculator GetResult (string oper)
{
Calculator calcu = null
Switch (oper)
{
Case "+":
Calcu = new Add ()
Break
Case "-":
Calcu = new Sub ()
Break
}
Return calcu
}
}
}
This is the end of the introduction of. Net simple Factory Model. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.