package com.zjh.designmodel.creativemethod;
/* 自己写的四不像工厂类
工厂模式:应遵循开闭原则与迪米特法则
开闭原则:对修改关闭 对拓展开放
迪米特法则:思想是低耦合高内聚 对类的使用应最好是数据的返回而不是对整个类变成参数
*/
public class FactoryModel {
public static void main(String[] args) {
FruitsFactory<FruitTemplate> fruitsFactory=new FruitsFactory<FruitTemplate>() {
@Override
FruitTemplate buildNotParameter() {
return new FruitTemplate();
}
@Override
FruitTemplate buildParameter(String fruitName, String describe, Float singlePrice) {
return new FruitTemplate(fruitName,describe,singlePrice);
}
};
FruitTemplate fruitTemplate=fruitsFactory.buildParameter("橘子","鲜美多汁的橘子",15.67f);
fruitTemplate.getFruitInfo();
DefaultTangerineFruit fruitTemplate1 =(DefaultTangerineFruit) fruitsFactory.defaultFruit("DefaultTangerineFruit");
fruitTemplate1.setDescribe("wc");
fruitTemplate1.getFruitInfo();
}
}
abstract class Fruit{//对某类型工厂要生产类的大致定义,使生产的类都具有这个基本信息
private String fruitName=null;
private String describe=null;
private Float singlePrice=0f;
public Fruit(){
}
public Fruit(String fruitName, String describe, Float singlePrice) {
this.fruitName = fruitName;
this.describe = describe;
this.singlePrice = singlePrice;
}
public String getFruitName(){
return this.fruitName;
}
public String getDescribe() {
return describe;
}
public Float getSinglePrice() {
return singlePrice;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public void setSinglePrice(Float singlePrice) {
this.singlePrice = singlePrice;
}
public abstract void getFruitInfo();
}
class FruitTemplate extends Fruit {
public FruitTemplate(){
}
public FruitTemplate(String name, String describe, Float singlePrice) {
super(name, describe, singlePrice);
}
@Override
public void getFruitInfo() {//拓展方法
System.out.println("[水果:" + super.getFruitName() + "|描述:" + super.getDescribe() + "|单价:" + super.getSinglePrice() + "]");
}
}
class DefaultTangerineFruit extends Fruit{
private String productAddress=null;
public DefaultTangerineFruit(){
super.setFruitName("橘子");
}
public DefaultTangerineFruit( String describe, Float singlePrice, String productAddress) {
super.setFruitName("橘子");
super.setDescribe(describe);
super.setSinglePrice(singlePrice);
this.productAddress = productAddress;
}
public String getProductAddress() {
return productAddress;
}
public void setProductAddress(String productAddress) {
this.productAddress = productAddress;
}
@Override
public void getFruitInfo() {
System.out.println("[水果:" + super.getFruitName() + "|描述:" + super.getDescribe() + "|单价:"
+ super.getSinglePrice() + "|产出地址:"+this.productAddress+"]");
}
}
abstract class FruitsFactory<T extends Fruit>{//对创建某类工厂的的定义,他可以拓展每个类(对类详细化)
Object defaultFruit(String type){
if(type.equals("DefaultTangerineFruit"))
{
return new DefaultTangerineFruit();
}
return this.buildNotParameter();
};
Object defaultFruit(String type, String describe, Float singlePrice,String productAddress){
if(type.equals("DefaultTangerineFruit"))
{
return new DefaultTangerineFruit( describe,singlePrice,productAddress);
}
return this.buildParameter("",describe,singlePrice);
};
abstract T buildNotParameter();
abstract T buildParameter(String fruitName, String describe, Float singlePrice);
}
工厂模式(自己写的四不像)
最新推荐文章于 2024-09-10 10:21:38 发布