简单工厂模式和空对象模式代码举例(java语言版与go语言版)

JAVA语言版工厂模式和空对象模式

创建接口:

public interface Shape {
   public void draw();
   public boolean isNil();
}

创建接口的实现类:

public class Circle implements Shape{
    @Override
    public void draw() {
        System.out.println("画圆形");
    }
    @Override
    public boolean isNil() {
        return false;
    }
}

public class Rectangle implements Shape{
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
    @Override
    public boolean isNil() {
        return false;
    }
}

public class Square implements Shape{
    @Override
    public void draw() {
        System.out.println("画正方形");
    }
    @Override
    public boolean isNil() {
        return false;
    }
}

public class NullShape implements Shape {
    @Override
    public void draw() {
        System.out.println("没有你想要画得形状");
    }
    @Override
    public boolean isNil() {
        return true;
    }
}

创建工厂,用于生产实体类的对象

public class ShapeFactory {
    public Shape getShape(String name){
        if (name.equals("Circle")) {
            return new Circle();
        }else if(name.equals("Rectangle")){
            return new Rectangle();
        }else if(name.equals("Square")){
            return new Square();
        }else
            return new NullShape();
        }
}

使用该工厂,通过传递类型信息来获取实体类的对象

public class FactoryPatterDemo {
    public static void main(String[] args) {
        ShapeFactory shapeFactory =new ShapeFactory();
        Shape shape1 =shapeFactory.getShape("Circle");
        Shape shape2 =shapeFactory.getShape("Rectangle");
        Shape shape3 =shapeFactory.getShape("Square");
        Shape shape4 =shapeFactory.getShape("Diamond");
        Shape shape5 =shapeFactory.getShape("");

        shape1.draw();
        shape2.draw();
        shape3.draw();
        shape4.draw();
        shape5.draw();
        
        System.out.println("shape1空状态:"+shape1.isNil());
        System.out.println("shape2空状态:"+shape2.isNil());
        System.out.println("shape3空状态:"+shape3.isNil());
        System.out.println("shape4空状态:"+shape4.isNil());
        System.out.println("shape5空状态:"+shape5.isNil());
    }
}

输出结果

画圆形
画矩形
画正方形
没有你想要画得形状
没有你想要画得形状
shape1空状态:false
shape2空状态:false
shape3空状态:false
shape4空状态:true
shape5空状态:true

GO语言版工厂模式和空对象模式

创建接口

type Shape interface{
		Draw ()
	    IsNil() bool
}

创建接口的实现类:

type Circle struct {
}
func (this *Circle)Draw()  {
	fmt.Println("画圆形")

}
func (this *Circle)IsNil() bool {
	return false
}

type Rectangle struct {
}
func (this *Rectangle) Draw()  {
	fmt.Println("画矩形")
}
func (this *Rectangle)IsNil() bool {
	return false
}

type Square struct {
}
func(this *Square)Draw() {
	fmt.Println("画正方形")
}
func (this *Square)IsNil() bool {
	return false
}

type NullShape struct {
}
func (this *NullShape) Draw()  {
	fmt.Println("没有你想要画得形状")
}
func (this *NullShape)IsNil() bool {
	return 	true
}

创建工厂,用于生产实体类的对象

type Shapefactory struct {
	
}
func (this *Shapefactory) GetShape (name string)  Shape {
	if name=="Circle" {
		return &Circle{}
	}
	if name=="Rectangle" {
		return &Rectangle{}
	}
	if name=="Square" {
		return &Square{}
	}
	return &NullShape{}
}

使用该工厂,通过传递类型信息来获取实体类的对象

func main()  {
	 shapeFactory := factory.Shapefactory{}
	 shape1 :=  shapeFactory.GetShape("Circle")
	shape2 :=  shapeFactory.GetShape("Rectangle")
	shape3 :=  shapeFactory.GetShape("Square")
	shape4 :=  shapeFactory.GetShape("Diamond")
	shape5 :=  shapeFactory.GetShape("")
	shape1.Draw()
	shape2.Draw()
	shape3.Draw()
	shape4.Draw()
	shape5.Draw()
	fmt.Println("shape1空状态:",shape1.IsNil())
	fmt.Println("shape2空状态:",shape2.IsNil())
	fmt.Println("shape3空状态:",shape3.IsNil())
	fmt.Println("shape4空状态:",shape4.IsNil())
	fmt.Println("shape5空状态:",shape5.IsNil())
}

输出结果

画圆形
画矩形
画正方形
没有你想要画得形状
没有你想要画得形状
shape1空状态: false
shape2空状态: false
shape3空状态: false
shape4空状态: true
shape5空状态: true

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值