[英]Understanding the code in an Abstract factory pattern
我想在 C# 學習設計模式,我的朋友給我寫了一些抽象工廠模式的代碼(我認為)。
從我所看到的代碼創建一個工廠(Fa),這個工廠(Fa)然后創建另一個基於枚舉的工廠(Fb)然后那個工廠(Fb)創建一個具體的 class 可用於調用 API 等.
我可以創建一個工廠 (Fb) 並創建 class 但是當我從工廠 (fb) 創建的 class 調用方法時,我看不到我的方法並且無法調用它們但只能調用它繼承的 class .
簡而言之,我想做的是創建一個工廠來創建 Jane 娃娃(就像它所做的那樣),它繼承了娃娃 class 的所有內容,它也有自己的所有屬性,很好,但為什么我不能訪問它自己的屬性當我創建一個工廠來創建 Jane Factory 時,它只允許我以這種方式使用繼承的 Doll 方法,但如果我創建另一個工廠來創建聖誕老人玩偶,它就會有我需要使用的不同方法。
**網絡控制器**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Web.Controllers
{
public class InventoryController : Controller
{
private readonly IDollFactory _dollFactory;
private readonly IJaneDollFactory _janeDollFactory;
private readonly IMapper _mapper;
public InventoryController(IJaneDollFactory dollFactory, IMapper mapper, IDollFactory dollFactory1)
{
_janeDollFactory = dollFactory;
_mapper = mapper;
_dollFactory = dollFactory1;
}
public async Task<IActionResult> List()
{
var token = HttpContext.Session.GetObject<OAuthResponse>(SessionConstants.JaneToken);
var doll = _JaneDollFactory.Create(token, JaneScopeConstants.GetAllScopes());
var a = _DollFactory.Create(Doll.Jane, HttpContext);
var ab = await a.LGetProductAsync("TestProduct");
var inventory = await doll.GetInventory();
var ret = inventory.InventoryItems.Select(
inventoryItem => _mapper.Map<InventoryViewModel>(inventoryItem));
return View(ret);
}
}
}
人偶工廠
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Web.Infrastructure;
public class DollFactory : IDollFactory
{
private readonly IJaneDollFactory _JaneDollFactory;
public DollFactory(IJaneDollFactory JaneDollFactory)
{
_JaneDollFactory = JaneDollFactory;
}
public IDoll Create(Doll Doll, HttpContext httpContext)
{
switch (Doll)
{
case Doll.Jane:
var token = httpContext.Session.GetObject<OAuthResponse>(SessionConstants.JaneToken);
return _JaneDollFactory.Create(token, JaneScopeConstants.GetAllScopes());
default:
throw new NotImplementedException();
}
}
}
IJaneDollFactory 接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Jane;
public interface IJaneDollFactory
{
IJaneDoll Create(OAuthResponse oAuthResponse, List<string> scopes, HttpMessageHandler httpMessageHandler = null);
}
**IJaneDoll 界面**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Jane;
public interface IJaneDoll : IDoll
{
//Inventory
Task<Inventory> GetInventory();
Task ListInventoryItem(InventoryItem product);
Task DeleteInventoryItem(string sku);
}
玩偶界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public interface IDoll
{
Task ListProductAsync(Product product);
Task<Product> GetProductAsync(string productId);
}
接口 IDollFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Web.Infrastructure;
public interface IDollFactory
{
IDoll Create(Doll doll, HttpContext httpContext);
}
簡娃娃工廠
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Qqq.Dolls.Jane;
public class JaneDollFactory : IJaneDollFactory
{
private readonly IMapper _mapper;
private readonly JaneApiConfiguration _JaneApiConfiguration;
public JaneDollFactory(IOptions<JaneApiConfiguration> JaneApiConfiguration, IMapper mapper)
{
_mapper = mapper;
_JaneApiConfiguration = JaneApiConfiguration.Value;
}
public IJaneDoll Create(OAuthResponse oAuthResponse, List<string> scopes, HttpMessageHandler httpMessageHandler = null)
{
return new JaneDoll(_mapper, _JaneApiConfiguration, oAuthResponse, scopes, httpMessageHandler);
}
}
發生這種情況是因為在您的工廠創建方法中,您的返回類型為
偶像
所以當你創建一個娃娃時,無論你實例化什么娃娃,它都會被隱式地轉換成一個 IDoll(這是 DollFactory 類中的錯誤)
由於您的調用者知道它希望抽象工廠創建什么玩偶,因此當您檢索 object 時,您可以將其顯式轉換為 IJaneDoll
var a = (IJaneDoll) _DollFactory.Create(Doll.Jane, HttpContext);
這應該允許您訪問 IDoll 和 IJane doll 的成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.