• 31、面向对象设计模式之工厂方法模式概念的提升


    例子如下:

    public abstract class Product{
        public string productName;
    }
    public abstract class ProductFactory{
        public Product FactoryMethod(string producTypeName){
            Product product;
            product = CreateProduct(producTypeName);
            return product;
        }
        
        public abstract CreateProduct(string productName);
    }
    
    public class ProductA : Product{
        public ProductA(){
            
            productName = "ProductA";
        }
    }
    
    public class ProductB : Product{
        public ProductB(){
            
            productName = "ProductB";
        }
    }
    public class ProductC : Product{
        public ProductC(){
            
            productName = "ProductC";
        }
    }
    public class ProductD : Product{
        public ProductD(){
            
            productName = "ProductD";
        }
    }
    public class ProductFactoryA : ProductFactory{
        
        public override Product CreateProduct(string producName){
            switch(producName){
                case "A":
                    return new ProductA();
                case "B":
                    return new ProductB();
            }
            return null;
        }
    }
    public class ProductFactoryB :ProductFactory{
        public override Product CreateProduct(string producName){
            switch(producName){
                case "C":
                    return new ProductC();
                case "D":
                    return new ProductD();
            }
            return null;
        }
    }
    class Program{
        
        static void Main(string[] args){
            ProductFactory myProductFactory = new ProductFactoryA();
            Product myProduct = myProductFactory.FactoryMethod("A");
            Console.WriteLine(myProduct.productName);
            myProduct = myProduct.FactoryMethod("B");
            Console.WriteLine(myProduct.producName);
            ProductFactory myProductFactoryB = new ProductFactoryB();
            Product myProductB = myProductFactoryB.FactoryMethod("C")
            Console.WriteLine(myProductB.producName);
            myProductB = myProductFactoryB.FactoryMethod("D");
            Console.WriteLine(myProduct.producName);
        }
    }
  • 相关阅读:
    数据库连接池系列之——c3p0
    spring配置
    flume ng系列之——flume安装
    flume ng之组件介绍
    flume ng之TailSource
    Flume-NG + HDFS + HIVE日志收集分析
    flume+hadoop
    Flume+kafka+storm+hdfs
    IOS中坐标转换
    tabBarItem是模型,只有控件才有textColor属性
  • 原文地址:https://www.cnblogs.com/sy-liu/p/13143377.html
Copyright © 2020-2023  润新知