創(chuàng)建型設(shè)計(jì)模式

一 . 工廠模式

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 +" 正在跑哇");
    }
}
二. 抽象工廠設(shè)計(jì)模式
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;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容