设计模式(一:Abstract Factory模式)
设计模式
  设计模式观念和理论逐渐成为面向对象(OO)领域中最热门的研究之一。本文将探讨如何使用模式?o解决软件的常见问题。中所使用的模式都来自于"Design Patterns: Elements of Reusable Object-Oriented Software"一文中所总结的模式。
什么是模式?
模式是人们遭遇到特定问题时?o大家惯用的应付方式。模式来解决问题?o而且是有效、可靠的。掌握愈多?o运用愈成熟?o就愈是杰出的设计专家。试想我们在工作和生活中总是借助于经验性的、已经被公认的有效的方法来解决现实中得问题。考虑一下,如果我们打靶,三点成一线的瞄准方式是非常有效的,这也是通常我们所采取的方式,可说这就是一种模式,我们可以通过使用这个模式来解决射击问题。
自从1991年以来?o设计模式理论应用于OO软件的设计上?o可解决软件设计上的问题。例如?o软体设计时?o常见下述问题?s 软建模块(Module)之间的耦合性太高?o使得软件的弹性降低了。我们可利用模式来解决这种问题?o求增强模块间的独立性?o提高软体的弹性,降低软体的维护费用?u
设计模式(Design Pattern)
  本文将讲解一些计模式?o来解决软件设计上的特定问题。首先声明本文中所使用的模式都来自于"Design Patterns: Elements of Reusable Object-Oriented Software"一书中所提到到一有的模式类型。
 
I: Abstract Factory模式
public class test{
public static void main(String[] args) { 
   Dsp d=new Dsp(1000,800); 
   d.DisplayData(); 
} 
}
 
class EmployeeSalary{
      private float  salary;
      private float  basesalary; 
      private float  award;
    
       EmployeeSalary() {}
              
    EmployeeSalary(float basesalary,float award)
     {this.basesalary=basesalary;
      this.award=award;
     }
       
    void displaysalary()
     {
         salary=basesalary+award;
      System.out.println(salary);
     }
       }
              
class Dsp {
       private float x;
       private float y;
       EmployeeSalary emp ;
       
   Dsp(float x,float y) { 
              this.x=x;
              this.y=y;
        emp = new EmployeeSalary(this.x,this.y); } 
   
       void  DisplayData() 
            { emp.displaysalary();} 
}
我们可以发现类别间的相互依性很高?oDsp类直接使用EmployeeSalary 字眼?o且使用两次。我们的问题是「若必须将EmployeeSalary 类名称改为ColokEmployeeSalary 时?o得更换 Dsp类别中的EmployeeSalary字眼」。 所以当我们替换EmployeeSalary类时?o会牵连到Dsp类?o替换过程将不会很顺,采用抽象类别?o可解决部分问题,如下?s
 public class test{
public static void main(String[] args) { 
   Dsp d=new Dsp(1000,800); 
   d.DisplayData(); 
} 
}
 
abstract class Salary { 
 void  displaysalary(){}
}
 
 
class EmployeeSalary extends Salary{
      private float  salary;
      private float  basesalary; 
      private float  award;
    
       EmployeeSalary() {}
              
    EmployeeSalary(float basesalary,float award)
     {this.basesalary=basesalary;
      this.award=award;
     }
       
    void displaysalary()
     {
         salary=basesalary+award;
      System.out.println(salary);
     }
       }
              
class Dsp {
       private float x;
       private float y;
       Salary emp ;
       
   Dsp(float x,float y) { 
              this.x=x;
              this.y=y;
        emp = new EmployeeSalary(this.x,this.y); } 
   
       void  DisplayData() 
            { emp.displaysalary();} 
}
正如我们所看到的在Dsp类中,我们替换掉了一个饿EmployeeSalary,但是另一个仍然存在于Dsp类中。
我们如何建立起一种模式来彻底的分离纠缠的类呢,这就要用到Abstract Factory模式,其功能就是隔离类,以利于替换。
类1   类2 模式
 public class test{
public static void main(String[] args) { 
   Dsp d=new Dsp(new SalaryFactory() ,1000,800); 
   d.DisplayData(); 
} 
}
 
abstract class Salary { 
 void  displaysalary(){}
}
 
 
class EmployeeSalary extends Salary{
      private float  salary;
      private float  basesalary; 
      private float  award;
    
       EmployeeSalary() {}
              
    EmployeeSalary(float basesalary,float award)
     {this.basesalary=basesalary;
      this.award=award;
     }
       
    void displaysalary()
     {
         salary=basesalary+award;
      System.out.println(salary);
     }
       }
 
              
class Dsp {
       private float x;
       private float y;
       Salary emp ;
       
   Dsp(Factory fc ,float x,float y) { 
              this.x=x;
              this.y=y;
        emp =fc.CreateDataObject(this.x,this.y); } 
   
       void  DisplayData() 
            { emp.displaysalary();} 
}
 
 
abstract class Factory { 
     abstract Salary CreateDataObject(float x,float y);
 
} 
 
class SalaryFactory extends Factory { 
       private float x;
       private float y;
 Salary CreateDataObject(float x,float y) 
       {   this.x=x;
              this.y=y;
				Tags:
作者:佚名 
			
