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");
             }
         }
     }
    

      

  • 相关阅读:
    ps入门之photoshop文件操作-打开与存储认识 (PS界面介绍)如何新建文档(03)
    ps入门之ps的工作界面的认识学习(界面介绍)界面的组成(02)
    markdown编辑器Typora
    5FAE8F6F96C59ED1字体
    清除浮动float (:after方法)
    SQL中Truncate的用法
    复合索引
    RESTful API 设计指南
    两种js方法发起微信支付:WeixinJSBridge,wx.chooseWXPay区别
    nuxt.js 简述
  • 原文地址:https://www.cnblogs.com/jameslif/p/5005979.html
Copyright © 2011-2022 走看看