zoukankan      html  css  js  c++  java
  • 抽象工厂模式

      在公司这几天一直都在看程序设计模式,看完的之后感觉还是似懂非懂,但心得还是颇多,程序设计中使用的原则有一条:面向接口编程,而且在设计过程中,要保证能在可以很方便的对程序进行拓展,而且不用改动现有的程序.下面是我自己设计的一个抽象工作模式,不知道我的理解是否正确.
     
     
     
    using System;
     using System.Collections.Generic;
     using System.Text;
     
    namespace 抽象工厂模式
     {
         class Program
         {
             static void Main(string[] args)
             {
                 CreateHouse newhouse = new CreateHouse(new CreateCountrysideHouse());
                 newhouse.dispaly();
                 Console.ReadKey();
             }
         }
     
        //建造房子的接口
         public interface ICreateHouse
         {
             //创建房子框架
             void createFrame();
             //创建房子的门
             void createDoor(); 
            //创建房子的窗
             void createWindow();
             //创建房子
             void display();
         }
     
        //农村风格
         public class CreateCountrysideHouse : ICreateHouse
         {
             public void createFrame()
             {
                 Console.WriteLine("this is Countryside Frame");
             }
             public void createDoor()
             {
                 Door door = new CountrysideDoor();
                 door.dispaly();
             }
             public void createWindow()
             {
                 Console.WriteLine("this is Countryside Window");
             }
             public void display()
             {
                 this.createFrame();
                 this.createDoor();
                 this.createWindow();
             }
         }
     
        //城市风格
         public class CreateCityHouse : ICreateHouse
         {
             public void createFrame()
             {
                 Console.WriteLine("this is City Frame");
             }
             public void createDoor()
             {
                 //Console.WriteLine("this is City Door");
                 Door door = new CityDoor();
                 door.dispaly();
             }
             public void createWindow()
             {
                 Console.WriteLine("this is City Window");
             }
             public void display()
             {
                 this.createFrame();
                 this.createDoor();
                 this.createWindow();
             }
         }
     
        public class CreateHouse
         {
             private ICreateHouse iCreateHouse;
             public CreateHouse(ICreateHouse iCreateHouse)
             {
                 this.iCreateHouse = iCreateHouse;
             }
             public void dispaly()
             {
                 iCreateHouse.display();
             }
         }
     
        //门的基类
         public abstract class Door
         {
             //锁
             public void Locker()
             {
                 Console.WriteLine("this is Door have Locker");
             }
             //门的形状
             public abstract void shape();
             public void dispaly()
             {
                 this.Locker();
                 this.shape();
             }
         }
     
        //农村门
         public class CountrysideDoor : Door
         {
             public override void shape()
             {
                 Console.WriteLine("this is Countryside Door");
             }
         }
         //城市门
         public class CityDoor : Door
         {
             public override void shape()
             {
                 Console.WriteLine("this is City Door");
             }
         }
     }
    

      

  • 相关阅读:
    vue axios的使用
    html5 css写出一个实心三角形和空心三角行
    弹出新页面并使整个旧页面背景变暗功能的实现代码
    radio 单选按钮 选中多个
    搭建脚手架cli2.x环境
    页面滚动条位置触发事件
    DataGridView行号发生变化 使用的事件
    eclipse git 忽略文件
    eclipse git 分享项目到GitHub上
    eclipse git 创建新分支 合并分支 删除分支
  • 原文地址:https://www.cnblogs.com/jameslif/p/5005979.html
Copyright © 2011-2022 走看看