两个最常用的设计模式:工厂方法 & 抽象工厂

来源:互联网 发布:电池修复软件 编辑:程序博客网 时间:2024/04/30 10:39

Design Pattern: 

1.  What is a design Pattern? Abstract Factory's  UML class diagram:

 

From my personal understanding, the design patterns are a summary of software development experience.  You know, the development of software is engineering.  It is similar to construct a building. So someone, I mean GOF team is completed the different design patters according to different software design role.  We can choose different design pattern to guild our work. For example, how to design interface, define class and so on.  Design Patterns represent solutions to problems what arise when developing software within a particular context.  Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. That means we can follow one design patter to develop our software and standardize our work.  We can get a high quality software production.  So using a suitable design pattern, we can improve software production code reusability, maintainability and improve the efficiency of software development.

2.   How many types of design pattern exist?

Basically, there are three categories:

·         Creational Patterns: deal with initializing and configuring classes and objects

·         Structural Patterns: deal with decoupling the interface and implementation of classes and objects

·         Behavioral Patterns: deal with dynamic interactions among societies of classes and objects

 

 

According to different software design role, there are many design pattern generated. For example I know a software design role is Open Closed Principal role.  Open Closed Principal role is means Software entities should be open for extension, but closed for modification. Of course, there is other software design role. 

 

3. What are Factory Design pattern and Abstract Factory design pattern, can you describe them?

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

In Factory Method design pattern, if we further abstract factory class, the Abstract Factory Design Pattern is formed.

For example, in Factory method design pattern, we can image understand that a factory contains many concrete product.  If you need new product, you just add new method into to the factory class. Actually you have new factory instance before, and you need to modify instance name, you just directly to call the method to create new instance.  This is good for coding reusability.

For abstract Factory design pattern, we can image understand that factory contain different production line and factory have different production series.  We can add new production line into factory, so that we can extend the feature that we want.

 

Factory Method's UML class diagram:

 

 

========================================

participants

    The classes and/or objects participating in this pattern are:

  • Product  (Page)
    • defines the interface of objects the factory method creates
  • ConcreteProduct  (SkillsPage, EducationPage, ExperiencePage)
    • implements the Product interface
  • Creator  (Document)
    • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
    • may call the factory method to create a Product object.
  • ConcreteCreator  (Report, Resume)
    • overrides the factory method to return an instance of a ConcreteProduct.

 

Code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


//编写一个程序描述动物界的弱肉强食
//一个工厂,生产狮子和牛
//狮子会跑,牛会跑
//狮子要吃牛
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            AnimalFactory oFactory = new AnimalFactory();
            Animal oLion = oFactory.createAnimalLion();
            Animal oCow = oFactory.createAnimalCow();
           
            oLion.Run();
            oCow.Run();

            Lion oLion1 = (Lion)oLion;
            Cow oCow1 = (Cow)oCow;

            Console.WriteLine(oLion1.AnimalName + "  interacts  " + oCow1.AnimalName);
            Console.ReadLine();
         }
    }

    //动物会跑
    public abstract class Animal
    {
       public abstract void Run();
    
    }
    //狮子
    public class Lion : Animal
    {
       public  string AnimalName = "Lion";
       public override void Run()
        {
            Console.WriteLine("Lion is running!");
        }
    }
    //牛
    public class Cow : Animal
    {
        public string AnimalName = "Cow";
        public override void  Run()
        {
            Console.WriteLine("Cow is running!");
        }

    }

    //动物工厂,生产狮子和牛

     public class AnimalFactory
    {
         public Animal createAnimalLion()
         {
             Animal oLion = new Lion();
             return oLion;
         }

         public Animal createAnimalCow()
         {
             Animal oCow = new Cow();
             return oCow;
         }
     }
  
  }

==============================================

 

 

 ======================================

participants

    The classes and/or objects participating in this pattern are:

  • AbstractFactory  (ContinentFactory)
    • declares an interface for operations that create abstract products
  • ConcreteFactory   (AfricaFactory, AmericaFactory)
    • implements the operations to create concrete product objects
  • AbstractProduct   (Herbivore, Carnivore)
    • declares an interface for a type of product object
  • Product  (Wildebeest, Lion, Bison, Wolf)
    • defines a product object to be created by the corresponding concrete factory
    • implements the AbstractProduct interface
  • Client  (AnimalWorld)
    • uses interfaces declared by AbstractFactory and AbstractProduct classes

======================================

(http://www.codeguru.com/forum/showthread.php?t=327982)

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 没收到货但申请了退货退款怎么办 小米商城预约中德手机没货怎么办 电脑用百度网盘下载速度超慢怎么办 ios网盘下载速度太慢怎么办 小米手机4x卡机了怎么办 小米手机4x屏幕点不动了怎么办 苹果手机连接u盘没反应怎么办 苹果官网储蓄卡分期额度不够怎么办 京东买东西发票信息填写错了怎么办 华为v9手机激活密码忘了怎么办 公司报销发票纸质的丢了怎么办 在京东上买的小天才手表坏了怎么办 美亚买东西过几天降价了怎么办 天猫买东西退款后又不想退了怎么办 自己写错了地址快递被签收了怎么办 在天猫购买东西不发货怎么办 在天猫买了东西不给我发货怎么办 天猫退货卖家收到货不退款怎么办 唯品会的账号找不回来了怎么办 从网上买的沙发物流超级慢怎么办 三星c5手机玩王者荣耀卡怎么办 买手机送话费的卡不想用了怎么办 到银行存钱被骗买保险了怎么办 去银行存钱被骗买了保险怎么办 京东申请退款后卖家又发货了怎么办 苹果6s没有4g网怎么办 花呗分期付款买手机额度不够怎么办 手机天猫不支持购买淘宝商品怎么办 天猫国际购买商品狠盾怎么办 在微信上微拍堂买东西被骗了怎么办 京东自营买到返修手机怎么办? 7p弯了怎么办能修复吗 农商银行app登录密码忘了怎么办 网址上的重庆时时彩不能提现怎么办 天天中彩票自己账户登录不了怎么办 天猫上卖王者荣耀的兑换码是怎么办 身份证借别人开淘宝永久封店怎么办 天猫店铺被扣12分怎么办 天猫法人变更之前的天猫贷款怎么办 在日本旅游遇到精日导游怎么办 银行资金交易异常卡被冻结怎么办