两个最常用的设计模式:工厂方法 & 抽象工厂
来源:互联网 发布:电池修复软件 编辑:程序博客网 时间: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)
- 两个最常用的设计模式:工厂方法 & 抽象工厂
- 最常用的设计模式----工厂模式家族(简单工厂模式, 工厂方法模式, 抽象工厂模式)
- 常用设计模式之工厂方法模式、抽象工厂模式
- 设计模式-工厂模式(简单工厂,工厂方法,抽象工厂)
- 设计模式--工厂方法、抽象工厂模式
- 常用Java设计模式系列(5)- 简单工厂、工厂方法模式和抽象工厂模式
- 设计模式:工厂方法和抽象工厂
- 【设计模式之四:工厂模式对比】简单工厂、工厂方法和抽象工厂的区别
- iOS常用设计模式——工厂方法(简单工厂模式,工厂方法模式, 抽象工厂模式)
- iOS常用设计模式——工厂方法(简单工厂模式,工厂方法模式, 抽象工厂模式)
- 设计模式-抽象工厂方法
- 设计模式--工厂模式(工厂方法、抽象工厂)
- 设计模式 - 工厂模式(简单,工厂方法,抽象工厂)
- 设计模式-工厂方法 简单工厂 抽象工厂 模板方法
- 大话设计模式之简单工厂模式、抽象工厂模式及工厂方法模式的比较
- 大话设计模式之简单工厂模式、抽象工厂模式及工厂方法模式的比较
- 设计模式之-简单工厂、工厂方法、抽象工厂
- 简单工厂、工厂方法和抽象工厂设计模式
- struts2.0的标签库(一)
- VB.Net程序设计:IE收藏夹管理器-增强版
- 做题记录(2010.3~2010.5)
- Etag和Expires
- Linux Command Line 解析
- 两个最常用的设计模式:工厂方法 & 抽象工厂
- SQL中 常用的处理方法(处理空值、更新字段、去掉小数后的0、查询重复列)
- maven构建与使用
- list vector比较
- 双线性插值算法用于图像缩放
- C#源码 备份和恢复数据库
- 数据库表结构设计方法及原则
- Touch001项目实现过程中遇到的一个技术问题
- Can't connect to MySQL server 频繁掉线刷新后正常解决办法