设计模式六大原则——开闭原则

设计原则:开闭原则
概         念:软件实体应当对外扩展开放,对修改关闭————即可扩展但不必修改


设计模式:工厂模式

设计思路:①Shoe.java ————  设计接口,也就是零件。接口不做修改,供后续扩展出来的部件使用
                    ②MenShoe.java
                        GirlShoe.java ———— 对接口的第一次扩展,由鞋子发展出男鞋和女鞋两类,而不对鞋子接口本身不做修改
                    ③MenShoe_PriceCut.java
                         GirlShoe_PriceCut.java ———— 对扩展部分的额外功能再次扩展,而不修改原来的扩展内容


①Shoe.java 

工厂模型的最基础接口,抽象出工厂中所有零件所共有的属性。本例中以鞋为代表,抽象出鞋的鞋名、类型以及价格三个公有的属性。

public interface Shoe
{
	public String getName();
	public String getType();
	public int getValue();
}


②GirlShoe.java

    MenShoe.java

第一次对接口进行实现,也就是第一次扩展。本例是在对鞋接口不修改的情况下,扩展出男鞋和女鞋两个大的分类,并重载接口中的方法,以及新增每个分类特有的方法。

public class GirlShoe implements Shoe
{
	private String GirlShoe_name;
	private String GirlShoe_type;
    private int GirlShoe_value;
    
    public GirlShoe(String name2, String type2, int value2)
    {
    	this.GirlShoe_name = name2;
    	this.GirlShoe_type = type2;
    	this.GirlShoe_value = value2;
    }
    
	@Override
	public String getName() 
	{
		return this.GirlShoe_name;
	}

	@Override
	public String getType() 
	{
		return this.GirlShoe_type;
	}

	@Override
	public int getValue() 
	{
		return this.GirlShoe_value;
	}
	
	public void setName(String name)
	{
		this.GirlShoe_name = name;
	}
	
	public void setType(String type)
	{
		this.GirlShoe_type = type;
	}
	
	public void setValue(int value)
	{
		this.GirlShoe_value = value;
	}
}

public class MenShoe implements Shoe 
{
	private String MenShoe_name;
	private String MenShoe_type;
	private int MenShoe_value;
	
	public MenShoe(String name1, String type1, int value1)
	{
		this.MenShoe_name = name1;
		this.MenShoe_type = type1;
		this.MenShoe_value = value1;
	}
	
	@Override
	public String getName() 
	{
		return this.MenShoe_name;
	}

	@Override
	public String getType() 
	{
		return this.MenShoe_type;
	}

	@Override
	public int getValue() 
	{
		return this.MenShoe_value;
	}
	
}


③GirlShoe_PriceCut.java
     MenShoe_PriceCut.java

对两个分类进行再次扩展,以鞋为例,如果想给鞋加上一些折扣的新属性,有3种情况需要考虑:

(1)在Shoe接口中新增折扣功能  

(2)在ManShoe和GirlShoe两类中新增折扣功能  

(3)新增两个折扣类继承前两个分类

根据开闭原则,在不修改原代码的情况下,新增折扣类是最符合原则的方式。因此新增两个折扣类继承前两类,扩展出折扣功能。

public class GirlShoe_PriceCut extends GirlShoe
{
	public GirlShoe_PriceCut(String name2, String type2, int value2) 
	{
		super(name2, type2, value2);
	}
	
	public int getValue()
	{
		int primeCost = super.getValue();
		int cutPrice=0;
		
		if(primeCost > 200)
		{//如果原价大于200元,则打7.5折
			cutPrice = primeCost*75/100;
			System.out.println("如果原价大于200元,则打7.5折");
		}
		
		if(primeCost > 150)
		{//如果原价大于150元,则打8折
			cutPrice = primeCost*80/100;
			System.out.println("如果原价大于150元,则打8折");
		}
		else
		{//其他,打8.5折
			cutPrice = primeCost*85/100;
			System.out.println("其他,则打8.5折");
		}
		
		return cutPrice;
	}
	
}

public class MenShoe_PriceCut extends MenShoe
{
	public MenShoe_PriceCut(String name1, String type1, int value1) 
	{
		super(name1, type1, value1);
	}
	
	public int getValue()
	{
		int primeCost = super.getValue();
		int cutPrice = 0;
		
		if(primeCost > 200)
		{//如果原价大于200元,则打8折
			cutPrice = primeCost*80/100;
		}
		
		if(primeCost > 150)
		{//如果原价大于150元,则打8.5折
			cutPrice = primeCost*85/100;
		}
		else
		{//其他,打9折
			cutPrice = primeCost*90/100;
		}
		
		return cutPrice;
	}
}


总结:

根据开闭原则的概念——软件实体应当对外扩展开放,对修改关闭。在设计软件时,需要抽象出某一实体就最基础的属性,作为外界的接口,由具体实现接口的类重载方法。若要新增该实体中某个特殊个体的功能,则可以新增一类继承该特殊个体。这样设计软件,扩展性强,功能单一不复杂,出现问题容易发现。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值