创建型设计模式

一 . 工厂模式

package DesignPattern;

/**
 * Created by YY on 2018/10/7.
 */
public class FactoryPattern {
     public static People getPeople(String name){
         if (name.equals("man")) {
             return new man(name);
         } else if (name.equals("woman")) {
             return new woman(name);
         } else {
             return new unknow(name);
         }
     }

    public static void main(String[] args) {
        People people = FactoryPattern.getPeople("ma");
        people.run();
    }
}
abstract class  People{
    public abstract void run();
}
class woman extends People{
    private String name;
    public woman(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println(this.name +" 正在跑哇");
    }
}
class man extends People{
    private String name;
    public man(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println(this.name +" 正在跑哇");
    }
}
class unknow extends People{
    private String name;
    public unknow(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println("未知性别的"+ this.name +" 正在跑哇");
    }
}
二. 抽象工厂设计模式
package DesignPattern;

/**
 * Created by YY on 2018/10/7.
 */
public class AbstractFactoryPattern {
    public static abstractFactory getFactory(String type){
        if (type.equals("color")) {
            return new colorFactory();
        } else if (type.equals("shape")) {
            return new shapeFactory();
        } else {
            return null;
        }
    }
    public static void main(String[] args) {
        abstractFactory factory = getFactory("shape");
        shape shape = factory.getShape("circle");
        shape.desc();
    }
}
abstract class color{
    public abstract void draw();
}
class red extends color{
    private String color;
    public red(String color) {
        this.color = color;
    }
    @Override
    public void draw() {
        System.out.println(this.color+"颜色的笔很好看");
    }
}
class black extends color{
    private String color;
    public black(String color) {
        this.color = color;
    }
    @Override
    public void draw() {
        System.out.println(this.color+"颜色的笔很好看");
    }
}
abstract class shape {
    public abstract void desc();
}
class circle extends shape{
    private String circle;
    public circle(String circle) {
        this.circle = circle;
    }
    @Override
    public void desc() {
        System.out.println("我的形状是" + circle);
    }
}

class square extends shape{
    private String circle;
    public square(String circle) {
        this.circle = circle;
    }
    @Override
    public void desc() {
        System.out.println("我的形状是" + circle);
    }
}
abstract class abstractFactory{
    public abstract color getColor(String color);
    public abstract shape getShape(String shape);
}
class colorFactory extends abstractFactory{

    @Override
    public color getColor(String colorType) {
       if (colorType.equals("red")) {
           return  new red(colorType);
       } else  if (colorType.equals("black")) {
           return new black(colorType);
       } else {
           return null;
       }
    }

    @Override
    public shape getShape(String shapeType) {
        return null;
    }
}

class shapeFactory extends abstractFactory{

    @Override
    public color getColor(String colorType) {
      return null;
    }

    @Override
    public shape getShape(String shapeType) {
        if (shapeType.equals("circle")) {
            return new circle(shapeType);
        } else if (shapeType.equals("squre")) {
            return new square(shapeType);
        } else {
            return null;
        }
    }
}

你可能感兴趣的:(创建型设计模式)