Factory Pattern

Factory Pattern

Abstract Factory-Provide an interface for creating families of related or dependent objects without specifying their concrete class.

Factory Method-Define an interface for creating an object but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to the subclass.

Problem:

Duck duck;

if(picni)
    duck 
=new MallardDuck();
else if(hunting)
    duck 
= new DecoyDuck();
else
    duck 
= new RubberDuck(); 

 

Solution:take the creation code and move it out into another object that is only going to be concerned with creating parts.

Example:

//code

import java.lang.String;
abstract class CPU {

    
int cache;
    
int Frequency;
    
int Voltage;
    String CPUName;
    
public void DisplayCPUCapability()
    
{
        System.out.println(
"CPU name is:"+CPUName);
        System.out.println(
"cache:"+cache+";Frequency:"+Frequency+";Voltage:"+Voltage);
        
    }

}

class AMD extends CPU
{
    
public AMD(int cache,int Frequency,int Voltage)
    
{
        
this.cache=cache;
        
this.Frequency=Frequency;
        
this.Voltage = Voltage;
        
this.CPUName = "AMD";
    }

}

class Intel extends CPU
{
    
public Intel(int cache,int Frequency,int Voltage)
    
{
        
this.cache=cache;
        
this.Frequency=Frequency;
        
this.Voltage = Voltage;
        
this.CPUName = "Intel";
    }

}

abstract class Memory
{
    
int Capacity;
    
int Frequency;
    
int type;
    String Name;
    
public void DisplayMemoryParam()
    
{
        System.out.println(
"Memory name is:"+Name);
        System.out.println(
"Capacity:"+Capacity+";Frequency:"+Frequency+";Type:"+type);
        
    }

}

class Kingston extends Memory
{
    
public Kingston(int cache,int Frequency,int Voltage)
    
{
        
this.Capacity=cache;
        
this.Frequency=Frequency;
        
this.type = Voltage;
        
this.Name = "Kingston";
    }

}

class ADATA extends Memory
{
    
public ADATA(int cache,int Frequency,int Voltage)
    
{
        
this.Capacity=cache;
        
this.Frequency=Frequency;
        
this.type = Voltage;
        
this.Name = "ADATA";
    }

}

abstract class Mainboard
{
    
int BusFrequency;
    String Name;
    
public void DisplayMainboard()
    
{
        System.out.println(
"Mainboard name is:"+Name);
        System.out.println(
"Bus frequency is:"+BusFrequency);
    
    }

}

class JingYingMainboard extends Mainboard
{
    
public JingYingMainboard(int BusFrequency)
    
{
        
this.BusFrequency = BusFrequency;
        
this.Name = "JingYingMainboard";
    }

}

 

public interface TraditionHouse {
    
public CPU CreateCPU();
    
public Memory CreateMemory();
    
public Mainboard CreateMainboard();

}

class QuanZhouHouse implements TraditionHouse
{
    
public CPU CreateCPU()
    
{
        
return new Intel(13,14,15);
    }

    
public Memory CreateMemory()
    
{
        
return new Kingston(3,4,5);
    }

    
public Mainboard CreateMainboard()
    
{
        
return new JingYingMainboard(111);
    }

}

class NanJingHouse implements TraditionHouse
{
    
public CPU CreateCPU()
    
{
        
return new AMD(15,14,15);
    }

    
public Memory CreateMemory()
    
{
        
return new ADATA(13,4,5);
    }

    
public Mainboard CreateMainboard()
    
{
        
return new JingYingMainboard(121);
    }

}

 

public class Computer {
    CPU cpu;
    Mainboard mainboard;
    Memory memory;
    TraditionHouse tranHouse;
    
public void SetFactory(TraditionHouse tran)
    
{
        tranHouse
=tran;
    }

    
public void prepareComputer()
    
{
        cpu
=tranHouse.CreateCPU();
        mainboard
=tranHouse.CreateMainboard();
        memory
=tranHouse.CreateMemory();
        
        DisplayComputerParam();
    }

    
private void DisplayComputerParam()
    
{
        cpu.DisplayCPUCapability();
        mainboard.DisplayMainboard();
        memory.DisplayMemoryParam();
    }

}

 

public class Main {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        
//declare two computer factory
        TraditionHouse tran1=new QuanZhouHouse();
        TraditionHouse tran2
=new NanJingHouse();
        
//declare computers in setup
        Computer computer1=new Computer();
        Computer computer2
=new Computer();
        
//select Computer factory which user select 
        computer1.SetFactory(tran1);
        computer2.SetFactory(tran2);
        
        
//setup and display results
        computer1.prepareComputer();
        computer2.prepareComputer();
        
    }


}

//if u find sth interesting,pls contact me. qq:95491590

When u see code like this, u know that when it comes time for changes or extensions, u will have to reopen this code and examine what needs to be added( or deleted). Often this kind of code ends up in several parts of the application making maintenance and  update more difficult and error-prone. So, in other words, when u have code that makes use of lots of concrete classes, u are looking for trouble because that code may have to be changed as new concrete classes are added.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值