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

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

两个最常用的设计模式:工厂方法 & 抽象工厂_第1张图片

 

 

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:

 两个最常用的设计模式:工厂方法 & 抽象工厂_第2张图片

 

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

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

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

 

 

两个最常用的设计模式:工厂方法 & 抽象工厂_第3张图片

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

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)

你可能感兴趣的:(设计模式,object,Class,interface,Instantiation,decoupling)