我对简单工厂模式的理解

    单工厂模式就是用一个工厂来生产(实例)有共同属性和方法但又不相同的事物.可以很方便的增加有共同的方法和属性的事物.
个人认为的好处有:
    1,增加有共同的方法和属性的事物,不会修改已有的事物.
    2,结构清晰明了.

    从一个计算器的例子里我们可以看出:
    一个抽象类,加上四个操作运算类,最后一个工厂类.
Operation
    public abstract class Operation
    
{
        
protected double numberA;
        
protected double numberB;

        
private double result = 0;

        
public Operation()
        
{
        }


        
public Double NumberA
        
{
            
set
            
{
                
this.numberA = value;
            }

        }


        
public  Double NumberB
        
{
            
set
            
{
                
this.numberB = value;
            }

        }



        
public virtual Double Result
        
{
            
get 
            
{
                
return result;
            }

        }

    }

Operator
public class OperationAdd:Operation
    
{
        
public override double Result
        
{
            
get
            
{
                
return base.numberA + base.numberB;
            }

        }

    }


    
public class OperationSub : Operation
    
{
        
public override double Result
        
{
            
get
            
{

                
return base.numberA - base.numberB;
            }

        }

    }


    
public class OperationMul : Operation
    
{
        
public override double Result
        
{
            
get
            
{
                
return base.numberA * base.numberB; 
            }

        }

    }


    
public class OperationDiv : Operation
    
{
        
public override double Result
        
{
            
get
            
{
                
if (base.numberB == 0)
                
{
                    
throw new Exception("The divisor can not zero");
                }

                
return base.numberA / base.numberB; 
            }

        }

    }


Factory
 public class OperationFactory
    
{
        
public static Operation CreateOperationFactory(string operate)
        
{
            Operation operation 
= null;
            
switch (operate)
            
{
                
case "+":
                    operation 
= new OperationAdd();
                    
break;
                
case "-":
                    operation 
= new OperationSub();
                    
break;
                
case "*":
                    operation 
= new OperationMul();
                    
break;
                
case "/":
                    operation 
= new OperationDiv();
                    
break;
            }

            
return operation;
        }

    }

  static   void  Main( string [] args)
        
{
            Console.WriteLine(
"Please enter a  digit A:");
            
string strNumberA = Console.ReadLine();
            Console.WriteLine(
"Please enter a operator(+,-,*,/):");
            
string strOperator = Console.ReadLine();
            Console.WriteLine(
"Please enter a digit B:");
            
string strNumberB = Console.ReadLine();
Operation operation 
= OperationFactory.CreateOperationFactory(strOperator);
operation.NumberA 
= Double.Parse(strNumberA);
            operation.NumberB 
= Double.Parse(strNumberB);
Console.WriteLine(
string.Format("{0} {1} {2} = {3}", strNumberA, strOperator,strNumberB, operation.Result));
}

转载于:https://www.cnblogs.com/zhibozhang/articles/1216524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值