设计模式(四)————工厂方法模式(Factory Pattern)与抽象工厂方法模式(Abstract Factory Pattern)

工厂方法模式

定义:定义一个创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。

UML类图:

C++实现:

#include<iostream>
using namespace std;

class Shape
{
public:
	virtual void Draw() = 0;
	virtual ~Shape() = default;
};
class Circle :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Circle!" << endl;
	}
	virtual ~Circle() {}
};
class Sphere :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Sphere!" << endl;
	}
	virtual ~Sphere() {}
};
class Cube :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Cube!" << endl;
	}
	virtual ~Cube() {}
};

class ShapeFactory
{
public:
	Shape* getShape(string shapeType)
	{
		if (shapeType == "")
		{
			return nullptr;
		}
		if (shapeType == "Circle")
		{
			return new Circle();
		}
		if (shapeType == "Sphere")
		{
			return new Sphere();
		}
		if (shapeType == "Cube")
		{
			return new Cube();
		}
		return nullptr;
	}
	~ShapeFactory() {};
};
int main()
{
	ShapeFactory* shapeFactory = new ShapeFactory();

	Shape* shape1 = shapeFactory->getShape("Circle");
	shape1->Draw();

	Shape* shape2 = shapeFactory->getShape("Sphere");
	shape2->Draw();

	Shape* shape3 = shapeFactory->getShape("Cube");
	shape3->Draw();
	return 0;
}

C#实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;

namespace ConsoleApp1
{

    public interface Shape
    {
        void Draw();
    }
    public class Circle:Shape
    {
        public void Draw()
        {
            WriteLine("Draw Circle!");
        }
    }
    public class Sphere : Shape
    {
        public void Draw()
        {
            WriteLine("Draw Sphere!");
        }
    }
    public class Cube : Shape
    {
        public void Draw()
        {
            WriteLine("Draw Cube!");
        }
    }
    public class ShapeFactory
    {
       public Shape getShape(String shapeType)
        {
            if (shapeType == null)
            {
                return null;
            }
            if(shapeType.Equals("Circle"))
            {
                return new Circle();
            }
            if(shapeType.Equals("Sphere"))
            {
                return new Sphere();
            }
            if(shapeType.Equals("Cube"))
            {
                return new Cube();
            }
            return null;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            Shape shape1 = shapeFactory.getShape("Circle");
            shape1.Draw();

            Shape shape2 = shapeFactory.getShape("Sphere");
            shape2.Draw();

            Shape shape3 = shapeFactory.getShape("Cube");
            shape3.Draw();
        }
    }
}

抽象工厂方模式

定义:为创建一组相关或者相互依赖的对象提供一个接口,而无需指定他们的具体类。(又称为工厂的工厂)

UML类图:

C++实现:

#include<iostream>
using namespace std;

class Shape
{
public:
	virtual void Draw() = 0;
	virtual ~Shape() = default;
};
class Color
{
public:
	virtual void Draw() = 0;
	virtual ~Color() = default;
};
class AbstractFactory
{
	virtual Shape* getShape(string shapeType) { return nullptr; }
	virtual Color* getColor(string colorType) { return nullptr; }
public:
	virtual ~AbstractFactory() = default;
};

class Circle :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Circle!" << endl;
	}
	virtual ~Circle(){}
};
class Sphere :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Sphere!" << endl;
	}
	virtual ~Sphere() {}
};
class Cube :public Shape
{
public:
	// 通过 Shape 继承
	virtual void Draw() override
	{
		cout << "Draw Cube!" << endl;
	}
	virtual ~Cube() {}
};

class ShapeFactory :public AbstractFactory
{
public:
	virtual Shape* getShape(string shapeType) override
	{
		if (shapeType == "")
		{
			return nullptr;
		}
		if (shapeType == "Circle")
		{
			return new Circle();
		}
		if (shapeType == "Sphere")
		{
			return new Sphere();
		}
		if (shapeType == "Cube")
		{
			return new Cube();
		}
		return nullptr;
	}
	virtual ~ShapeFactory() {};
};
class Red :public Color
{
public:
	virtual void Draw()
	{
		cout << "Fill Red!!!" << endl;
	}
	virtual ~Red() {}
};
class Green :public Color
{
public:
	virtual void Draw()
	{
		cout << "Fill Green!!!" << endl;
	}
	virtual ~Green() {}
};
class Blue :public Color
{
public:
	virtual void Draw()
	{
		cout << "Fill Blue!!!" << endl;
	}
	virtual ~Blue() {}
};
class ColorFactory :public AbstractFactory
{
public:
	virtual Color* getColor(string colorType) override
	{
		if (colorType == "")
			return nullptr;
		if (colorType == "Red")
			return new Red();
		if (colorType == "Green")
			return new Green();
		if (colorType == "Blue")
			return new Blue();
		return nullptr;
	}
	virtual ~ColorFactory() {}
};
class FactoryProducer
{
public:
	static AbstractFactory* getFactory(string abstractType)
	{
		if (abstractType == "")
			return nullptr;
		if (abstractType == "SHAPE")
			return new ShapeFactory();
		if (abstractType == "COLOR")
			return new ColorFactory();
		return nullptr;
	}
	~FactoryProducer() {}
};
int main()
{
	FactoryProducer* factoryProducer = new FactoryProducer();

	ShapeFactory* shapeFactory = dynamic_cast<ShapeFactory*>(factoryProducer->getFactory("SHAPE"));
	Shape* shape1 = shapeFactory->getShape("Circle");
	shape1->Draw();

	ColorFactory* colorFactory = dynamic_cast<ColorFactory*>(factoryProducer->getFactory("COLOR"));
	Color* color1 = colorFactory->getColor("Red");	color1->Draw();
	return 0;
}

C#实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;

namespace ConsoleApp1
{

    public interface Shape
    {
        void Draw();
    }
    public interface Color
    {
        void Draw();
    }
    public abstract class AbstractFactory
    {
        public abstract Shape getShape(String shapeType);
        public abstract Color getColor(String colorType);
    }

    public class Circle:Shape
    {
        public void Draw()
        {
            WriteLine("Draw Circle!");
        }
    }
    public class Sphere : Shape
    {
        public void Draw()
        {
            WriteLine("Draw Sphere!");
        }
    }
    public class Cube : Shape
    {
        public void Draw()
        {
            WriteLine("Draw Cube!");
        }
    }
    public class ShapeFactory:AbstractFactory
    {
        public override Shape getShape(String shapeType)
        {
            if (shapeType == null)
            {
                return null;
            }
            if(shapeType.Equals("Circle"))
            {
                return new Circle();
            }
            if(shapeType.Equals("Sphere"))
            {
                return new Sphere();
            }
            if(shapeType.Equals("Cube"))
            {
                return new Cube();
            }
            return null;
        }
        public override Color getColor(string colorType)
        {
            return null;
        }

    }

    public class Red : Color
    {
        public void Draw()
        {
            WriteLine("Fill Red!!!");
        }
    }
    public class Green : Color
    {
        public void Draw()
        {
            WriteLine("Fill Green!!!");
        }
    }
    public class Blue : Color
    {
        public void Draw()
        {
            WriteLine("Fill Blue!!!");
        }
    }
    class ColorFactory:AbstractFactory
    {
        public override Shape getShape(String shapeType)
        {
            return null;
        }
        public override Color getColor(String colorType)
        {
            if (colorType == null)
                return null;
            if(colorType.Equals("Red"))
            {
                return new Red();
            }
            if(colorType.Equals("Green"))
            {
                return new Green();
            }
            if(colorType.Equals("Blue"))
            {
                return new Blue();
            }
            return null;
        }
    }
    public class FactoryProducer
    {
        public static AbstractFactory getAbstractFactory(String abstractType)
        {
if (abstractType == "")
                return null;
            if(abstractType.Equals("SHAPE"))
            {
                return new ShapeFactory();
            }
            if(abstractType.Equals("COLOR"))
            {
                return new ColorFactory();
            }
            return null;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            AbstractFactory shapeFactory = FactoryProducer.getAbstractFactory("SHAPE");

            Shape shape1 = shapeFactory.getShape("Circle");
            shape1.Draw();

            AbstractFactory colorFactory = FactoryProducer.getAbstractFactory("COLOR");

            Color color1 = colorFactory.getColor("Red");
            color1.Draw();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值