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