工厂模式

分为三类:

简单工厂模式:一个抽象产品类,若干个实际产品类,一个工厂类(负责根据产品信息生产具体的类)

 

package FactoryPattern;

public class ShapeFactory {
	public Shape getShape(String shapeName){
        //往往用switch case语句区分产品类的类别
		switch (shapeName){
			case "Circle":return new Circle();
			case "Rectangle":return new Rectangle();
			case "Square":return new Square();
			default:return null;
		}	
	}
}
		ShapeFactory myShape = new ShapeFactory();
		Shape shape1 = myShape.getShape("Circle");
		shape1.draw();
		Shape shape2 = myShape.getShape("Square");
		shape2.draw();
		Shape shape3 = myShape.getShape("Rectangle");
		shape3.draw();

工厂方法模式:一个抽象产品类,若干个具体产品类,一个抽象工厂类,若干个与具体产品对应的具体工厂类,每一个具体工厂                           类生产一种具体的产品。

package FactoryPattern;
//抽象产品类
public interface Shape {
	void draw();
}
//具体产品类
class Circle implements Shape{

	@Override
	public void draw() {
		
		System.out.println("Inside Circle::draw() method");
		
	}

}
class Square implements Shape{

	@Override
	public void draw() {
		
		System.out.println("Inside Suqare::draw() method.");
	}
		
}
 class Rectangle implements Shape{

		@Override
		public void draw() {
			System.out.println("Inside Rectangle::draw() method.");
		}
		
}
package FactoryPattern;

//抽象工厂类
public interface FactoryInterface {
	public Shape createShape();
}

//具体工厂类
class CircleFactory implements FactoryInterface{

	@Override
	public Circle createShape() {
		return new Circle();
	}
}
class SquareFactory implements FactoryInterface{

	@Override
	public Square createShape() {
		return new Square();
	}
}
class RectangleFactory implements FactoryInterface{

	@Override
	public Rectangle createShape() {
		return new Rectangle();
	}
}
CircleFactory myCircleFac = new CircleFactory();//创建Circle工厂
Circle myCircle = myCircleFac.createShape();    //Circle工厂调用图形创建类,创建产品
myCircle.draw();                                //产品调用方法

SquareFactory mySquareFac = new SquareFactory();
Square mySquare = mySquareFac.createShape();
mySquare.draw();

抽象工厂模式:若干个抽象产品类,每个抽象产品类对应若干个具体产品类;一个抽象工厂,对应若干个具体工厂类,每一个具                           体工厂对应若干个(可能所有)具体产品类,从而实现这些产品类的捆绑生产。

package FactoryPattern;
//Color类
public interface Color {
	public void paint();
}
class Yellow implements Color{

	@Override
	public void paint() {
		System.out.println("Paint yellow one the shape");
	}
}
class Red implements Color{

	@Override
	public void paint() {
		System.out.println("Paint red one the shape");
	}
}
package FactoryPattern;
//Shape类
public interface Shape {
	void draw();
}

class Circle implements Shape{

	@Override
	public void draw() {
		
		System.out.println("Inside Circle::draw() method");
		
	}

}
class Square implements Shape{

	@Override
	public void draw() {
		
		System.out.println("Inside Suqare::draw() method.");
	}
		
}
 class Rectangle implements Shape{

		@Override
		public void draw() {
			System.out.println("Inside Rectangle::draw() method.");
		}
		
}

package FactoryPattern;
//图案类
public interface Pattern {
	public void carve();
}

class Character implements Pattern{

	@Override
	public void carve() {
		System.out.println("Carve a character");
	}
}
class Boy implements Pattern{

	@Override
	public void carve() {
		System.out.println("Carve a boy");
	}
}
package FactoryPattern;
//抽象工厂类
public interface AbstractFactory {
	public Shape createShape();
	
	public Color paintColor();
	
	public Pattern carvePattern();
}
//具体工厂类:形状,颜色,图案捆绑处理
class CircleFactory implements AbstractFactory{

	@Override
	public Circle createShape() {
		return new Circle();
	}

	@Override
	public Red paintColor() {
		return new Red();
	}

	@Override
	public Character carvePattern() {
		return new Character();
	}
	
}

class SquareFactory implements AbstractFactory{

	@Override
	public Square createShape() {
		return new Square();
	}

	@Override
	public Yellow paintColor() {
		return new Yellow();
	}

	@Override
	public Boy carvePattern() {
		return new Boy();
	}
	
}
	CircleFactory myCircleFac = new CircleFactory();//创建圆球生产工厂

	Circle myCircle = myCircleFac.createShape();    //工厂生产图形
	myCircle.draw();
		
	Red myColor = myCircleFac.paintColor();        //工厂涂色
	myColor.paint();

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值